def post_file(wp, f, t, y, m, d): p = WordPressPost() p.title = t p.content = slurp(f) # All 3 sites are configured to UTC if re.search('deutsche', f): p.date = datetime(y, m, d, 4) # 4am UTC is 5am in UTC+1=Berlin else: p.date = datetime(y, m, d, 10) # 10am UTC is 5am eastern, 2am pacific p.date_modified = p.date p.post_status = 'publish' p.comment_status = 'closed' if wp: wp.call(NewPost(p))
def toWordPressPost(self): post = WordPressPost() if self.title: post.title = self.title if self.content: post.content = self.content post.date = self.published post.date_modified = self.updated post.post_status = 'publish' return post
def post_article(title, body, category, tag): """发布一篇文章""" try: post = WordPressPost() post.title = title post.content = body post.post_status = "publish" # draft草稿 post.post_type = "post" # page页面 post.comment_status = 'open' # 允许评论 post.date_modified = datetime.datetime.now() post.terms_names = {'category': category, 'post_tag': tag} wp.call(NewPost(post)) log("发布文章:%s..." % title[0:20]) except Exception as e: log_error("文章发布失败!", e)
def toWordPressPost(self): post = WordPressPost() if self.title: post.title = self.title if self.content: post.content = self.content post.date = self.published post.date_modified = self.updated post.comment_status = True post.post_status = 'publish' return post
def _create_wp_post(song, content): # Create the NewPost object - see docs at # http://python-wordpress-xmlrpc.readthedocs.io/en/latest/ref/wordpress.html # We're missing some fields but ehhhh who knows if they even exist anymore post = WordPressPost() post.title = str(song) post.content = content post.comment_status = 'open' post.ping_status = 'closed' post.post_status = 'publish' post.post_type = 'post' post.excerpt = song.tagline post.date = datetime.now(tz=timezone.utc) post.date_modified = datetime.now(tz=timezone.utc) return NewPost(post)
def create_new_post(wp_instance, title='', content='', author='', tags=[], category=['USAF']): post = WordPressPost() post.title = title content = reformat_url_with_htmltag(content) post.content = content tags.append(author) tags = tags + generate_tags(content) category.append(author) # By far, only classify the 'Kadena' and 'Andersen' AFB if 'Kadena' in tags: category.append('Kadena') if 'Andersen' in tags: category.append('Andersen') post.terms_names = { 'post_tag': tags, 'category': category } post.post_status = 'publish' # insert the new post post_id = wp_instance.call(NewPost(post)) # update the published date to the tweet datetime re_object = re.compile('(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s.*\s\d{4}') original_time = re_object.search(content).group() post.date = parse_post_formated_time(original_time) post.date_modified = post.date # update the post author to the tweet screen name post.post_author = 2 wp_instance.call(EditPost(post_id, post)) return post_id
def _add_blogpost(post_data): ''' Adds a blog post parsed from blog.blogpost Model published status codes are as follows: PUBLISHED = 1, DRAFT = 2, DELETED = 3 :param post_data: Python dict of post structure. See modelsamples/blogpost_sample.txt for structure. :return: ''' post = WordPressPost() post.post_type = 'news' post.title = post_data['fields']['title'] post.content = post_data['fields']['content'] post.date = _return_datetime(post_data['fields']['publish_date']) post.date_modified = _return_datetime(post_data['fields']['updated']) post.slug = post_data['fields']['slug'] if post_data['fields']['status'] == 2: post.post_status = 'publish' # Assign Author if post_data['fields']['user']: wp_userid = _get_wordpress_user_id_by_email( _get_django_user_email_by_id(post_data['fields']['user'])) if wp_userid: post.user = wp_userid # TODO set catagories and tags to proper taxonomy # post.terms_names = { # 'category': ['Blogpost'] # } # if post_data['fields']['categories']: # categories = [] # for category in dp.get_content(DJANGO_DATA, 'blog.blogcategory'): # if category['pk'] in post_data['fields']['categories']: # categories.append(category['fields']['title']) # post.terms_names['post_tag'] = categories try: if post_data['fields']['status'] != 3: post.id = API.call(NewPost(post)) print("created post", post.id, post.title) except Fault as err: pprint(post) print(err.faultCode, err.faultString)
def _add_application(app_data): ''' Adds an application parse from apps.application :param app_data: Python dict of application structure. See modelsamples/application_sample.txt :return: ''' post = WordPressPost() post.post_type = 'application' post.title = app_data['fields']['name'] post.slug = app_data['fields']['slug'] post.content = _format_app_content(app_data) post.date = _return_datetime(app_data['fields']['created']) post.date_modified = _return_datetime(app_data['fields']['updated']) # TODO assign to proper taxonomies once those are in. # Assign Author if app_data['fields']['owner']: wp_userid = _get_wordpress_user_id_by_email( _get_django_user_email_by_id(app_data['fields']['owner'])) if wp_userid: post.user = wp_userid # Assign Categories and Tags post.terms_names = _parse_taxonomies(app_data) # Put the previous page url in a custom field. legacy_url = "https://www.us-ignite.org/apps/%s/" % ( app_data['fields']['slug']) post.custom_fields = [{'key': 'legacy_url', 'value': legacy_url}] # Set publish status and push to site if app_data['fields']['status'] == 1: post.post_status = 'publish' try: if app_data['fields']['status'] != 3: post.id = API.call(NewPost(post)) except Fault as err: pprint(err.faultString)
def post(): #get id post_id = re.search(':wp_id:((.*)|\n)', asc_file_read_str).group(1).strip() #get status post_status = re.search(':wp_status:((.*)|\n)', asc_file_read_str) #get title post_title = re.search(':wp_title:((.*)|\n)', asc_file_read_str) #get slug post_slug = re.search(':wp_slug:((.*)|\n)', asc_file_read_str) #get category post_category = re.search(':wp_category:((.*)|\n)', asc_file_read_str) post_category_str = post_category.group(1).strip().split(", ") if len(post_category_str) == 0: post_category_str = [] elif post_category.group(1).strip() == '': post_category_str = [] elif len(post_category_str) == 1: post_category_str = post_category.group(1).strip(), #get tag post_tag = re.search(':wp_tag:((.*)|\n)', asc_file_read_str) post_tag_str = post_tag.group(1).strip().split(", ") if len(post_tag_str) == 0: post_tag_str = [] elif post_tag.group(1).strip() == '': post_tag_str = [] elif len(post_tag_str) == 1: post_tag_str = post_tag.group(1).strip(), #get excerpt post_excerpt = re.search(':wp_excerpt:((.*)|\n)', asc_file_read_str) #get thumbnail post_thumbnail = re.search(':wp_thumbnail:((.*)|\n)', asc_file_read_str) #post to wordpress from wordpress_xmlrpc import Client, WordPressPost from wordpress_xmlrpc.methods import posts client = Client(xmlrpc_url, username, password) post = WordPressPost() date_ = datetime.now() #id New or Edit if not post_id: post.date = date_.strftime("%s") post.id = client.call(posts.NewPost(post)) mode = "New" asc_file_re = re.sub(r':wp_id:((.*)|\n)', ':wp_id: ' + post.id , asc_file_read_str) asc_file_write = open(filepath, 'w') try: asc_file_write.write( asc_file_re ) finally: asc_file_write.close() else: post.date_modified = date_.strftime("%s") post.id = post_id mode = "Edit" post.post_status = post_status.group(1).strip() try: post.title = post_title.group(1).strip() except: print 'Title is not exist' try: post.slug = post_slug.group(1).strip() except: print 'Slug is not exist' post.content = html try: post.excerpt = post_excerpt.group(1).strip() except: post.excerpt = '' post.terms_names = { 'category': post_category_str, 'post_tag': post_tag_str, } try: post.thumbnail = post_thumbnail.group(1).strip() except: post.thumbnail = '' client.call(posts.EditPost(post.id, post)) post_info = client.call(posts.GetPost(post.id, post)) #get post info from wordpress asc_file_read_slug = open(filepath, 'r') asc_file_read_slug_str = asc_file_read_slug.read() if post_info: asc_file_read_slug_str = re.sub(r':wp_slug:((.*)|\n)', ':wp_slug: ' + post_info.slug, asc_file_read_slug_str) if mode == "New": new_date = int(post_info.date.strftime("%s"))+(timezone___*60*60) new_date = datetime.fromtimestamp(new_date).strftime("%Y-%m-%d %H:%M:%S") asc_file_read_slug_str = re.sub(r':wp_date:((.*)|\n)', ':wp_date: ' + new_date, asc_file_read_slug_str) elif mode == "Edit": edit_date = int(post_info.date_modified.strftime("%s"))+(timezone___*60*60) edit_date = datetime.fromtimestamp(edit_date).strftime("%Y-%m-%d %H:%M:%S") asc_file_read_slug_str = re.sub(r':wp_modified:((.*)|\n)', ':wp_modified: ' + edit_date, asc_file_read_slug_str) asc_file_re_slug_write = open(filepath, 'w') try: asc_file_re_slug_write.write( asc_file_read_slug_str ) finally: asc_file_re_slug_write.close() print '==========================\n' + mode + ' Post ID: ' + post.id + ' \nStatus: ' + post.post_status + '\nTitle: ' + post.title + '\nSlug: ' + post_info.slug + '\nCategory: ' + post_category.group(1).strip() + '\nTag: ' + post_tag.group(1).strip() + '\n'
def post_article(self, wpUrl, wpUserName, wpPassword, articleTitle, articleCategories, articleContent, articleTags, PhotoUrl, date, logo_name, post_name): self.path = os.path.join(os.getcwd(), "00000001.jpg") self.articlePhotoUrl = PhotoUrl self.wpUrl = wpUrl self.wpUserName = wpUserName self.wpPassword = wpPassword #Download File print self.articlePhotoUrl #r = requests.get(self.articlePhotoUrl,headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:16.0) Gecko/20100101 Firefox/16.0,gzip(gfe)'}) #with open('test.jpg', "wb") as f: # f.write(r.content) from PIL import Image from StringIO import StringIO import urllib2 #content = r.content self.articlePhotoUrl = self.articlePhotoUrl.replace( 'www.macappdownload.net', 'www.macbed.com') print self.articlePhotoUrl print 'vvvvvvvvvvvvvvvvvvvvv' r = requests.get( self.articlePhotoUrl, headers={ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:16.0) Gecko/20100101 Firefox/16.0,gzip(gfe)' }) #content = urllib2.urlopen(self.articlePhotoUrl).read() im = Image.open(StringIO(r.content)) #im.seek(0) #im.verify() rgb_im = im.convert('RGB') self.path = os.path.join(os.getcwd(), 'image', logo_name) print self.path rgb_im.save(self.path) #Upload to WordPress client = Client(self.wpUrl, self.wpUserName, self.wpPassword) filename = self.path # prepare metadata data = {'name': logo_name, 'type': 'image/jpeg'} # read the binary file and let the XMLRPC library encode it into base64 print filename with open(filename, 'rb') as img: data['bits'] = xmlrpc_client.Binary(img.read()) response = client.call(media.UploadFile(data)) print 'aaaaaaaaaaaa' attachment_id = response['id'] #Post post = WordPressPost() post.title = articleTitle try: post.date = datetime.datetime.strptime(date, "%Y-%m-%d %H:%M") post.date_modified = datetime.datetime.strptime( date, "%Y-%m-%d %H:%M") except: post.date = datetime.datetime.strptime(date, "%Y-%m-%d") post.date_modified = datetime.datetime.strptime(date, "%Y-%m-%d") post.slug = post_name post.comment_status = "open" post.content = articleContent post.terms_names = { 'post_tag': articleTags, 'category': articleCategories } post.post_status = 'publish' post.thumbnail = attachment_id post.id = client.call(posts.NewPost(post)) print 'Post Successfully posted. Its Id is: ', post.id if post.content.find('https://nmac.to') != -1: image = client.call(media.GetMediaItem(attachment_id)) import re post.content = re.sub(r'https://nmac.to(\S+).png', image.link, post.content) client.call(posts.EditPost(post.id, post)) print 'Post Successfully updated. Its Id is: ', post.id
exit(counter) plist.sort(key=itemgetter('id')) current_year = 0 current_date = None for p in plist: break post = WordPressPost() post.title = p['title'] if p['year'] != current_year: current_year = p['year'] current_date = date(current_year, 1, 1) post.content = p['page'] post.post_status = 'publish' post.date = current_date post.date_modified = current_date post.id = wp.call(NewPost(post)) print post.id #todo: # handle English files that miss year # find matching hebrew files for English files # find the way to tag (Hebrew/English, Archive) # upload... # posts = wp.call(GetPosts()) # print len(posts) # post = WordPressPost() # post.title = 'My post'