def _update_a_draft(self): postid = self.get_postid() if not postid: slog.warning('Please provide a post id!') return afile, aname = self.conf.get_draft(postid) # If output is provided, write the html to a file and abort. if self.args.output: self._write_html_file(afile) return html, meta, txt, medias = self._get_and_update_article_content(afile) if not html: return if meta.poststatus == 'draft': slog.warning( 'The post status of draft "%s" is "draft", ' 'please modify it to "publish".'%postid) return # Update all taxonomy before create a new article. self.get_terms_from_wp(['category']) self.get_terms_from_wp(['post_tag']) if meta.posttype == 'page': post = WordPressPage() else: post = WordPressPost() post.terms = self.cache.get_terms_from_meta(meta.category, meta.tags) if not post.terms: slog.warning('Please provide some terms.') return post.content= html post.title = meta.title post.slug = meta.nicename post.date = meta.date post.user = meta.author post.date_modified = meta.modified post.post_status = meta.poststatus post.comment_status = "open" post.ping_status = "open" postid = self.wpcall(NewPost(post)) if postid: write_by_templ(afile, afile, {'POSTID':postid, 'SLUG':postid}, True) else: return newfile, newname = None, None if meta.posttype == 'page': newfile, newname = self.conf.get_article(meta.nicename, meta.posttype) else: newfile, newname = self.conf.get_article(postid, meta.posttype) slog.info('Move "%s" to "%s".'%(afile, newfile)) shutil.move(afile, newfile)
def _add_page(page_data): page = WordPressPage() page.title = page_data['fields']['title'] page.slug = page_data['fields']['slug'] page.order = page_data['fields']['_order'] page.date = _return_datetime(page_data['fields']['publish_date']) page.date_modified = _return_datetime(page_data['fields']['updated']) page.content = page_data['fields']['content'] if page_data['fields']['status'] == 1: page.publish_status = 'publish' try: page.id = API.call(NewPost(page)) print("created page", page.id, page.title) except Fault as err: pprint(page) print(err.faultCode, err.faultString)
def create_year_page(self, year: int) -> None: date_from, date_to = self._api.get_year_date_range(year=year) posts = self._api.get_items(item_type='posts', date_from=date_from, date_to=date_to) posts = sorted(posts, key=lambda item: item['date']) categories = self._api.get_items(item_type='categories', search='{}_'.format(year)) category_planned = _get_planned_category(categories=categories) category_not_planned = _get_not_planned_category(categories=categories) posts_planned = [ post for post in posts if category_planned in post['categories'] ] posts_not_planned = [] if not category_not_planned \ else [post for post in posts if category_not_planned in post['categories']] events_planned = [ EventLink(event_number=i + 1, title=post['title']['rendered'], link=post['link'], date=format_iso_date(post['date'])) for i, post in enumerate(posts_planned) ] events_not_planned = [ EventLink(event_number=i + 1, title=post['title']['rendered'], link=post['link'], date=format_iso_date(post['date'])) for i, post in enumerate(posts_not_planned) ] page = WordPressPage() page.post_status = 'publish' page.title = 'Akcie {}'.format(year) page.content = get_year_page_content( events_planned=events_planned, events_non_planned=events_not_planned) page.date = datetime(year=year, month=1, day=1) page_id = self._client.call(NewPost(page)) notice('Created page {} for year {}'.format(page_id, year))
def page(): #get id page_id = re.search(':wp_id:((.*)|\n)', asc_file_read_str).group(1).strip() #get page status page_status = re.search(':wp_status:((.*)|\n)', asc_file_read_str).group(1).strip() #get title page_title = re.search(':wp_title:((.*)|\n)', asc_file_read_str).group(1).strip() #get slug page_slug = re.search(':wp_slug:((.*)|\n)', asc_file_read_str).group(1).strip() page_thumbnail = re.search(':wp_thumbnail:((.*)|\n)', asc_file_read_str).group(1).strip() #post to wordpress from wordpress_xmlrpc import Client, WordPressPage, WordPressPost from wordpress_xmlrpc.methods import posts client = Client(xmlrpc_url, username, password) page = WordPressPage() post = WordPressPost() #id New or Edit if not page_id: page.date = datetime.now().strftime("%s") page.id = client.call(posts.NewPost(page)) mode = "New" asc_file_re = re.sub(r':wp_id:((.*)|\n)', ':wp_id: ' + page.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: page.date_modified = datetime.now().strftime("%s") page.id = page_id mode = "Edit" #page status page.post_status = page_status #page title try: page.title = page_title except: print 'Title is not exist' #page slug try: page.slug = page_slug except: print 'Slug is not exist' #page content page.content = html #page thumbnail try: page.thumbnail = page_thumbnail except: page.thumbnail = '' client.call(posts.EditPost(page.id, page)) page_info = client.call(posts.GetPost(page.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 page_info: asc_file_read_slug_str = re.sub(r':wp_slug:((.*)|\n)', ':wp_slug: ' + page_info.slug, asc_file_read_slug_str) if mode == "New": new_date = int(page_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(page_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 + ' Page ID: ' + page.id + ' \nStatus: ' + page.post_status + '\nTitle: ' + page.title + '\nSlug: ' + page_info.slug + '\n'