def create_template_files(file_name): #1. create scss file #assets/scss/templates/_[name].scss try: fh = open(sass_directory + '_'+ file_name + '.scss','r') except: # if file does not exist, create it fh = open(sass_directory + '_'+ file_name + '.scss','w') #2. Import newly created Sass file to global one #assets/scss/templates/app.scss with open('assets/scss/foundation.scss', 'a') as f: f.write('\n@import "templates/' + file_name + '";') #f.writelines(lines) #3. create image folder #assets/images/[name] if not(os.path.exists(image_directory + file_name)): os.makedirs(image_directory + file_name) #4a. Create page template if not(os.path.exists(page_template_directory + file_name) ): with open(page_template_directory + file_name + '.php', 'a') as f: f.writelines(['<?php \n', '/* Template Name: '+ file_name.capitalize() +' */\n', ' get_header();\n', '?>', '\n\n', '<?php get_footer();']) #4b. Create page in wordpress and connect to the template page client = Client(site_host + 'xmlrpc.php', username, password) page = WordPressPage() page.title = file_name.capitalize() page.content = '' page.post_status = 'publish' page.template = 'page-templates/about.php' page.id = client.call(posts.NewPost(page))
def write_page(survey_id): survey = Survey(survey_id) pages = survey.wordpress_client.call(posts.GetPosts({'post_type': 'page'}, results_class=WordPressPage)) template = Template(filename='/home/tom/src/airbnb/web_page_template.mako') content = template.render( city=survey.city, city_underbar=survey.city_underbar, date = survey.date, listings = survey.listings, survey_id=survey.survey_id, url_root = URL_ROOT, ) page = WordPressPage() page.title = ('Airbnb survey ' + str(survey.survey_id) + ', for ' + survey.city + ', collected on ' + survey.date) page.content = content page.post_status = 'publish' # Find if the page exists for a_page in pages: if page.title==a_page.title: page.id = a_page.id survey.wordpress_client.call(posts.EditPost(page.id, page)) return page.id = survey.wordpress_client.call(posts.NewPost(page))
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 post_page(wpUrl, wpUserName, wpPassword, articleTitle, articleContent): client = Client(wpUrl, wpUserName, wpPassword) # Page page = WordPressPage() page.title = articleTitle page.content = articleContent # page.terms_names = {'post_tag': articleTags, 'category': articleCategories} page.post_status = 'publish' # post.thumbnail = attachment_id page.id = client.call(posts.NewPost(page)) print('Post Successfully posted. Its Id is: ', page.id) return page.id
def post_page(wpUrl, wpUserName, wpPassword, articleTitle, articleContent, pageId): # This function updates/creates a page in a wordpress site client = Client(wpUrl, wpUserName, wpPassword) # Page page = WordPressPage() page.title = articleTitle page.content = articleContent # page.terms_names = {'post_tag': articleTags, 'category': articleCategories} page.post_status = 'publish' # post.thumbnail = attachment_id client.call(posts.EditPost(int(pageId), page)) return pageId
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 upload(utype, title, categories, tags, content): logging.info("uploading post \"%s\"" % title) if utype == UploadType.POST: p = posts.WordPressPost() else: p = WordPressPage() p.title = title p.content = content p.post_status = 'publish' if utype == UploadType.POST: p.terms_names = { 'post_tag': tags, 'category': categories } wp_client.call(posts.NewPost(p))
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'
page.custom_fields.append({ 'key': 'staging_dashboard_link', 'value': "<script type='text/javascript' src='https://stg-tableau.kipp.org/javascripts/api/viz_v1.js'></script><div class='tableauPlaceholder' style='width: 1200px; height: 100px;'><object class='tableauViz' width='1200' height='1000' style='display:none;'><param name='host_url' value='https%3A%2F%2Fstg-tableau.kipp.org%2F' /> <param name='embed_code_version' value='3' /> <param name='site_root' value='/t/KIPPLA' /><param name='name' value='" + user_wb.replace(' ', '').replace('&', '').replace( '/', '').replace('(', '').replace(')', '') + "/" + user_v.replace(' ', '').replace('&', '').replace( '/', '').replace('(', '').replace(')', '') + "' /><param name='tabs' value='no' /><param name='toolbar' value='yes' /><param name='showAppBanner' value='false' /></object></div>" }) # thumbnail id of the image we just uploaded page.thumbnail = thumbnail_id # publish page page.post_status = 'publish' # create page page.id = client.call(posts.NewPost(page)) print(Fore.RED + '\nAdded ' + category + ': ' + view.name + '\n' + 'Owner: ' + owner.fullname + '\n') else: # add wordpress page info # get screenshot saved on computer and upload to wordpress # windows #image_path = 'View Thumbnails//' + user_wb+ '_' + user_v + '.jpg' # mac image_path = 'View Thumbnails/' + user_wb + '_' + user_v + '.jpg' filename = os.path.join(fileDir, image_path) data = { 'name': user_wb + '_' + user_v + '.jpg',