def test_post_comments(self): # create a bunch of comments comment_list = [] counter = 0 for i in range(1, random.randint(6, 15)): comment = WordPressComment() comment.content = 'Comment #%s' % counter comment.user = self.userid comment_id = self.client.call( comments.NewComment(self.post_id, comment)) comment_list.append(comment_id) counter += 1 # retrieve comment count comment_counts = self.client.call( comments.GetCommentCount(self.post_id)) self.assertEqual(comment_counts['total_comments'], counter) # fetch a subset of the comments num_comments = 5 post_comments = self.client.call( comments.GetComments({ 'post_id': self.post_id, 'number': num_comments })) self.assert_list_of_classes(post_comments, WordPressComment) self.assertEqual(num_comments, len(post_comments)) # cleanup for comment in comment_list: self.client.call(comments.DeleteComment(comment))
def wordpress_comment(post_id,wp_content, wp_host, wp_user, wp_password): try: # 检测是否登录成功 client = Client(wp_host, wp_user, wp_password) comment = WordPressComment() comment.content = wp_content comment.status = 'open' client.call(comments.NewComment(post_id, comment)) print("Wordpress发布成功:", wp_content) except ServerConnectionError: print('WordPress登录失败')
def test_comment_lifecycle(self): comment = WordPressComment() comment.content = 'This is a test comment.' comment.user = self.userid # create comment comment_id = self.client.call( comments.NewComment(self.post_id, comment)) self.assertTrue(comment_id) # edit comment comment.content = 'This is an edited comment.' response = self.client.call(comments.EditComment(comment_id, comment)) self.assertTrue(response) # delete comment response = self.client.call(comments.DeleteComment(comment_id)) self.assertTrue(response)
def add_comments(self, post_id, parent, fb_comments, ul_resources=False): for fb_dict in fb_comments: comment = wordpress_xmlrpc.WordPressComment() comment.parent = parent comment.date_created = facebook_timestamp_to_datetime( fb_dict['created_time']) comment.status = 'approve' comment.content = fb_dict['message'] if 'attachment' in fb_dict: if ul_resources: fb_dict['attachment'] = self.upload(fb_dict['attachment']) if self._debug: print('image {}'.format(fb_dict['attachment'])) comment.content += '\n\n{}'.format(fb_dict['attachment']) if self._debug: if parent == post_id: print('comment') else: print('- comment') try: # save comment comment_id = self._client.call( comments.NewComment(post_id, comment)) # rename author (has to be done separately) rec = wordpress_xmlrpc.WordPressComment() rec.author = fb_dict['from']['name'] self._client.call(comments.EditComment(comment_id, rec)) # handle replies self.add_comments(post_id, comment_id, fb_dict['comments'], ul_resources) except xmlrpc_client.Fault as e: if e.faultCode == 409: if self._debug: print('(duplicate)') pass else: raise
def add_movies(page_num, choice): while True: if (choice == 'a' or choice == 'upcoming'): term_tag = 'upcoming' term_cat = 'upcoming' get_url = "/3/movie/upcoming?page=" + str( page_num ) + "&language=en-US&api_key=eedcd0a42e1921c7f3da84ad5fdeaa2a" if (choice == 'b' or choice == 'nowplaying'): term_tag = 'Now playing' term_cat = 'now_playing' get_url = "/3/movie/now_playing?page=" + str( page_num ) + "&language=en-US&api_key=eedcd0a42e1921c7f3da84ad5fdeaa2a" if (choice == 'c' or choice == 'archives' or choice == 'archive'): term_tag = 'archives' term_cat = 'archives' get_url = "/3/discover/movie?page=" + str( page_num) + "&language=en-US&primary_release_date.lte=" + str( date) + "&api_key=eedcd0a42e1921c7f3da84ad5fdeaa2a" try: #making the connection to the api print("making the connection") conn = http.client.HTTPSConnection("api.themoviedb.org") payload = "{}" print("getting all the movies from page " + str(page_num)) #getting the movies data , key = now playing print("making the api req") #print(get_url , term_cat , term_tag) conn.request("GET", get_url, payload) res = conn.getresponse() data = res.read() response = json.loads(data.decode("utf-8")) #connecting to wordpress wp = Client('http://172.104.189.102/xmlrpc.php', 'octraves', 'octraves') # THE MAIN LOOP FOR ADDING ALL THE MOVIES TO THE WP DATABASE counter = 1 for movie in response['results']: try: print("Adding movie number = " + str(counter) + "id : " + str(movie['id'])) #download and upload the image to wordpress url = "http://image.tmdb.org/t/p/w200" + movie[ 'poster_path'] image_name = str(movie['id']) + '.jpg' image_location = '/Users/user/Desktop/wordpress_images/' + image_name urllib.request.urlretrieve( url, image_location) # download the image image = image_location imageType = mimetypes.guess_type(str(image))[0] img_data = { 'name': image_name, 'type': imageType, } with open(image, 'rb') as img: img_data['bits'] = xmlrpc_client.Binary(img.read()) img_response = wp.call(media.UploadFile(img_data)) attachment_id = img_response[ 'id'] # id to be appended in post #getting the trailer try: link = details.get_trailer(movie['id']) except: link = "" pass # Creating custom fields fields = [['Languages', movie['original_language']], ['Release Date', movie['release_date']]] # Creating the post post = WordPressPost() post.title = movie['title'] post.post_type = 'post' post.mime_type = "text/html" post.content = genres.get_primary_info( movie['id'] ) + """<h3> Overview </h3> """ + movie[ 'overview'] + """<h3> Trailer </h3> """ + " \n" + link + " \n" + " \n" + " \n" + """<h3> Credits </h3> """ + " \n" + " \n" + " \n" + str( details.get_credits(movie['id'], 'cast')) + details.get_poster( movie['id']) post.thumbnail = attachment_id post.terms_names = { 'post_tag': [term_tag], term_cat: genres.get_genres(movie['genre_ids']), 'category': genres.get_genres(movie['genre_ids']) } post.custom_fields = [] for field in fields: post.custom_fields.append({ 'key': field[0], 'value': field[1] }) post.post_status = 'publish' post.comment_status = 'open' #finally publish the post !! post.id = wp.call(posts.NewPost(post)) #add comments reviews = details.get_reviews(movie['id']) for review in reviews: comment = WordPressComment() comment.author = review['author'] comment.content = review['content'] wp.call(comments.NewComment(post.id, comment)) counter += 1 except Exception as e: print( "All the pages have been processed or there is a error!! " + str(e)) pass print("Page number " + str(page_num) + " is completed ! Loading another page.") page_num += 1 except Exception as e: print(str(e))
# duplication post_id = found.id if FLAGS.verbose: print "Publishing new comment to " + post_id #wp.call(NewAnonymousComment(post_id, publishable_comment)) # See # https://github.com/maxcutler/python-wordpress-xmlrpc/pull/35 if config.WORDPRESS_COMMENT_STYLE == 'anonymous': wp.call( comments.NewAnonymousComment( found.id, publishable_comment)) else: wp.call( comments.NewComment(found.id, publishable_comment)) post_request = service.activities().list_next( post_request, activities_doc) break except AccessTokenRefreshError: print( "The credentials have been revoked or expired, please re-run" "the application to re-authorize") if __name__ == '__main__': main(sys.argv)