def postWordpress(item): post = WordPressPost() post.title = item.title post.content = item.movieUrl if item.category == "": post.terms_names = {'post_tag': item.tags} else: category = [] category.append(item.category) post.terms_names = {'post_tag': item.tags, 'category': category} post.slug = '[input your prameter]' # 投稿時間 # 現在時間で投稿 post.date = datetime.now() # 予約投稿の場合(例:2017年2月2日0時0分0秒0マイクロ秒) #month = random.randint(1,10) #day = random.randint(1,22) #post.date = datetime(2018, month, day, 0, 0, 0, 0) # 投稿する。 # ステータスを公開済にする。 post.post_status = 'publish' # これなら下書き指定 # post.post_status = 'draft' post.thumbnail = getMediaId(item) wp.call(NewPost(post))
def wordpress_artice(wppost_status, wp_title, wp_slug_title, wp_content, wp_category, wp_post_tag, wp_host, wp_user, wp_password): # 如果遇到错误就出试5次 success_num = 0 while success_num < 5: try: client = Client(wp_host, wp_user, wp_password) newpost = WordPressPost() # 创建一个类实例,注意,它不是一个函数。只要在一个类名后面加上括号就是一个实例 # newpost.post_status = 'draft' newpost.post_status = wppost_status newpost.slug = wp_slug_title # 文章别名,固定链接形式为文章标题时需要 # 设置发布目录(一篇文章可以属于多个分类目录) newpost.terms_names = { 'category': wp_category, # 目录 'post_tag': wp_post_tag # 标签 } newpost.title = wp_title newpost.content = wp_content client.call(posts.NewPost(newpost)) #发布新建的文章,返回的是文章id print("Wordpress发布成功:", wp_title) except Exception as e: print("正在重试:", e) success_num = success_num + 1 continue
def testUpdate(pid): client = initClient() post = WordPressPost() post.title = 'My new title update 2' post.content = 'This is the body of my new post.' post.slug = 'helloword' post.post_type = 'post' post.post_status = 'draft' print client.call(posts.EditPost(pid, post))
def testUpdate(pid): client = initClient() post = WordPressPost() post.title = 'My new title update 2' post.content = 'This is the body of my new post.' post.slug= 'helloword' post.post_type = 'post' post.post_status = 'draft' print client.call(posts.EditPost(pid, post))
def import_application(data): """ Creates an Application post from a spreadsheet row. :param data: Dict of imported row from csv via DictReader :return: """ for app in data: post = WordPressPost() post.post_status = 'publish' post.title = app['Application'].strip() post.slug = slugify(post.title) post.post_type = 'apps' # Compile post body content = html_format(app['Description']) if app['CCX']: content += html_format('CCX Page:' + app['CCX']) if app['Website']: content += html_format('Website: ' + app['Website']) if app['POC']: content += html_format('Contact: ' + app['POC']) if content: post.content = content terms = {} # Community terms community = [] if app['SGC']: community.append(app['SGC']) if community: terms['communities'] = community # Status Terms status = [] if app['Status']: status.append(app['Status']) if app['Sharing'] == 'Shareable': status.append(app['Sharing']) if status: terms['status'] = status # General Tags tags = [] if app['Priority Area']: tags.append(app['Priority Area']) if tags: terms['post_tag'] = tags if terms: post.terms_names = terms API.call(NewPost(post))
def add_post(): post = WordPressPost() ts = time.time() title = 'Test Title' + datetime.datetime.fromtimestamp(ts).strftime( '%Y-%m-%d %H:%M:%S') post.title = title post.content = 'This is a test post to see if the RPC call works.' post.terms_names = { 'post_tag': ['test', 'firstpost'], 'category': ['Introductions', 'Test'] } post.slug = slugify(title) # note this if final part of slug only API.call(NewPost(post))
def post(wp, old_post, title, content, rtcprof, img_info): from wordpress_xmlrpc import WordPressPost from wordpress_xmlrpc.methods.posts import NewPost, EditPost if old_post: post = old_post else: post = WordPressPost() post.title = title post.content = content post.terms_names = { 'post_tag': [rtcprof.name, 'RTC'], 'category': ['RTComponents', rtcprof.basicInfo.category] } post.slug = rtcprof.name n = datetime.datetime.now() year = n.year month = n.month day = n.day hour = n.hour if n.hour < 9: day = day - 1 hour = hour + 24 if day == 0: month = month - 1 if month == 0: month = 12 year = year -1 if month in [4, 6, 9, 11]: day = 30 elif month == 2: day = 28 else: day = 31 hour = hour - 9 post.date = datetime.datetime(year, month, day, hour, n.minute, n.second) post.post_status = 'publish' if img_info: post.thumbnail = img_info['id'] else: post.thumbnail = old_post.thumbnail if old_post: # Edit Mode wp.call(EditPost(post.id, post)) else: wp.call(NewPost(post))
def process_post(self, content) -> Optional[WordPressPost]: """Create a wordpress post based on pelican content""" if content.status == "draft": return None post = WordPressPost() post.title = content.title post.slug = content.slug post.content = content.content # this conversion is required, as pelican uses a SafeDateTime # that python-wordpress-xmlrpc doesn't recognize as a valid date. post.date = datetime.fromisoformat(content.date.isoformat()) post.term_names = { "category": [content.category.name], } if hasattr(content, "tags"): post.term_names["post_tag"] = [tag.name for tag in content.tags] return post
def parseDocument(filename): lines = open(filename, 'r').readlines() values = {'title':'', 'permalink':'', 'layout':'post', 'tags':'', 'categories':'default', 'published': 'false'} start = False config = False for i in range(len(lines)): line = lines[i].strip() if config == False: if line == '---': if (start == False): start = True else: # end if (values['title'] == '' or values['permalink'] == ''): printf('title and permalink should not be null!\n'); exit() else:# config ok config = True else: try: key = line[:line.find(':')] value = line[line.find(':')+1:] values[key] = value.strip() except: printf('config failed! (key, value) = (' + key + ', ' + value + ')\n');exit() else: #config ok while len(lines[i]) <= 1: #filter first blank lines i+=1 rawcontent = parseMedia(lines[i:]) rawfilename = filename[:-3] + '.raw.id-' open(rawfilename, 'w').writelines(rawcontent) post = WordPressPost() post.title = values['title'] post.slug = values['permalink'] post.content = pandocTool.md2html(rawfilename) post.post_type = values['layout'] post.post_status = 'publish' if values['published'].lower() == 'true' else 'draft' post.comment_status = 'open' #default post.pint_status = 'open' #default post.terms_names = {} #values['tags'] = values['tags'].replace(',', ',') compatible with jekyll, use blank #values['categories'] = values['categories'].replace(',', ',') if len(values['tags']) > 0: post.terms_names['post_tag'] = [ tag.strip() for tag in values['tags'].split() if len(tag) > 0] if len(values['categories']) > 0: post.terms_names['category'] = [ cate.strip() for cate in values['categories'].split() if len(cate) > 0] return post
def article_handle(filename): with open(filename, encoding='utf-8') as input_file: md_file = input_file.readlines() md_content = ''.join(md_file[1:]) # 配置第二行以后内容为正文 if '<!--more-->' not in md_content: if input("文章内无分页符,确认请按‘y’,其他任意键取消: ") != 'y': sys.exit(0) html_content = markdown.markdown(md_content) # 转换markdown为html post_title = ''.join(md_file[:1]) # 配置首行为标题 # 手动输入文章英文地址及标签 post_slug = input('请输入文章%s地址(英文):\n' % (post_title.split())) post_tag = input('请输入文章tag标签,以空格分隔:\n').encode('utf-8').decode( 'utf-8').split() # 分类暂定为“随笔” while True: choice = int(input("请选择文章分类:\n1、随笔;2、产品工作\n")) if choice == 1: post_category = ['随笔'] break elif choice == 2: post_category = ['产品工作'] break else: print('输入错误!请重新输入') # 定义post内容 post = WordPressPost() post.title = post_title post.content = html_content post.terms_names = {'post_tag': post_tag, 'category': post_category} post.slug = post_slug post.post_status = 'publish' print("Article Content: \n%s\n\n\n" "Article Title: %s - %s\n" "Article Category: %s\n" "Article Tag: %s\n" % (post.content, post.title, post.slug, post.terms_names['category'], post.terms_names['post_tag'])) confirm = input("确认请按‘y’,其他任意键取消: ") if confirm == 'y': return post else: return None
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 wordpress_artice(wppost_status, wp_title, wp_slug_title, wp_content, wp_category, wp_post_tag, wp_host, wp_user, wp_password): try: # 检测是否登录成功 client = Client(wp_host, wp_user, wp_password) newpost = WordPressPost() # 创建一个类实例,注意,它不是一个函数。只要在一个类名后面加上括号就是一个实例 # newpost.post_status = 'draft' newpost.post_status = wppost_status newpost.slug = wp_slug_title # 文章别名,固定链接形式为文章标题时需要 # 设置发布目录(一篇文章可以属于多个分类目录) newpost.terms_names = { 'category': wp_category, # 目录 'post_tag': wp_post_tag # 标签 } newpost.title = wp_title newpost.content = wp_content client.call(posts.NewPost(newpost)) #发布新建的文章,返回的是文章id print("Wordpress发布成功:", wp_title) except ServerConnectionError: print('WordPress登录失败')
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 addItemsToWordpress( self ): items = self.items if items: wp = Client('http://' + self.wpinfo['website'] + '/xmlrpc.php', self.wpinfo['user'], self.wpinfo['pass']) pass for item in items: self.log.info("[ Scrapper {} ] - [ Publishing \"{}\" into WP ]".format( self.table, item["title"] )) now = time.strftime("%c") post = WordPressPost() post.terms_names = { 'category': ['Scrapped'] # This need to be changed in next release } post.title = '{}'.format(item['title']) if item['slug']: post.slug = item['slug'] if item['image_url']: call(['curl',item['image_url'].replace(' ','%20'),'-o','image.jpg.scrapper_data']) filename = 'image.jpg.scrapper_data' data = { 'name': 'image.jpg', 'type': 'image/jpeg', # mimetype } with open(filename, 'rb') as img: data['bits'] = xmlrpc_client.Binary(img.read()) response = wp.call(media.UploadFile(data)) attachment_id = response['id'] post.thumbnail = attachment_id content = '' if item['content']: content += '{}'.format(item['content']) content += 'Source: <a href="{}">{}</a>\n\n'.format(item['url'],item['referer']) post.content = content wp.call(NewPost(post))
def add_or_edit_wp_post(title, content, slug, more_info_url, local_img_file): # first upload the image if local_img_file: data = { 'name': local_img_file.split('/')[-1], 'type': 'image/jpg', # mimetype } # read the binary file and let the XMLRPC library encode it into base64 with open(local_img_file, 'rb') as img: data['bits'] = xmlrpc_client.Binary(img.read()) response = wp.call(media.UploadFile(data)) attachment_id = response['id'] # now post the post and the image post = WordPressPost() post.post_type = 'post' # stupid effing theme post.title = title post.content = content post.post_status = 'publish' post.slug = slug if local_img_file: post.thumbnail = attachment_id if not get_wp_post_id(slug): # this is a new post wp.call(NewPost(post)) msg = "posted" else: # this post exists, update it post.id = get_wp_post_id(slug) wp.call(EditPost(post.id, post)) msg = "edited" print "%s %s as %s" % (msg, title, post.slug)
def upload_file(): if request.method == 'POST': url1=request.form['url'] #return(url1) ydl_opts = { 'format': 'bestaudio/best', 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '320', }], 'outtmpl':'%(title)s-%(id)s.%(ext)s' } with youtube_dl.YoutubeDL(ydl_opts) as ydl: info = ydl.extract_info(url1, download=True) mp3FileName = ydl.prepare_filename(info).replace(info['ext'], 'mp3') print(mp3FileName) newfile=mp3FileName.split(" ",1)[0] identify=newfile + "mp3file" newFileName=newfile + ".mp3" os.rename(mp3FileName,newFileName) audiofile=eyed3.load(newFileName) Title= request.form['title'] if not (Title==""): Title=Title else: Title="kanniyam" Artist = request.form['artist'] if not (Artist==""): Artist=Artist else: Artist="Kanniyam" Album = request.form['album'] if not (Album==""): Album=Album else: Album="Kanniyam" Genre = request.form['genre'] if not (Genre==""): Genre=Genre else: Genre="Podcast" Source_url = bytes(request.form['source_url'],'utf-8') if not (Source_url==""): Source_url=Source_url else: Source_url="" License = str(request.form['license']) if not (License==""): License=License else: License="https://creativecommons.org/licenses/by/4.0/" Comments = str(request.form['comments']) if not (Comments==""): Comments=Comments else: Comments="" Language = str(request.form['language']) if not (Language==""): Language=Language else: Language="Tamil" Art_name = request.form['art_name'] if not (Art_name==""): Art_name=Art_name else: Art_name="Kanniyam" Publisher_url = bytes(request.form['publisher_url'],'utf-8') if not (Publisher_url==""): Publisher_url=Publisher_url else: Publisher_url="http://www.kaniyam.com" print (type(Publisher_url)) print ("ssss") audiofile.tag.title = u""+Title audiofile.tag.artist = u""+Artist audiofile.tag.album = u""+Album audiofile.tag.genre = u""+Genre audiofile.tag.source_url = b""+Source_url audiofile.tag.license = u""+License audiofile.tag.comments.set(u""+Comments, description=u"") audiofile.tag.language = u""+Language audiofile.tag.art_name = u""+Art_name audiofile.tag.publisher_url = b""+Publisher_url audiofile.tag.save() print ("after save") ia_upload = "ia upload " + identify + \ " -m collection:opensource -m mediatype:audio -m sponsor:Kaniyam -m language:ta " + \ newFileName os.system(ia_upload) audioURL = "https://archive.org/download/%s/%s" % (identify, newFileName) print ("file uploaded") print("Posting into WordPress") client = Client(blog url,username,password) post = WordPressPost() content = "%s \n %s"% (audioURL, Comments) post.title = Title post.content = content post.post_status = 'publish' post.comment_status = 'open' post.terms_names = {'category': ['Podcast']} post.slug = newfile post.id = client.call(posts.NewPost(post)) print("Posted into WordPress") return "File updated"
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
def upload_text(wp, repo_name, rtcprof, html, img_info = None, test=False, build_report_filename="build_report.yaml"): from wordpress_xmlrpc.methods import posts, taxonomies, media sys.stdout.write(' - Uploading %s\n' % rtcprof.name) editFlag = False post = None for p in all_posts: if p.title == title: editFlag = True post = p break html = update_build_status(html, build_report_filename) if not editFlag: post = WordPressPost() post.title = title post.content = apply_language_setting(html) post.terms_names = { 'post_tag': [rtcprof.name, 'RTC'], 'category': ['RTComponents', rtcprof.basicInfo.category] } post.slug = rtcprof.name n = datetime.datetime.now() year = n.year month = n.month day = n.day hour = n.hour if n.hour < 9: day = day - 1 hour = hour + 24 if day == 0: month = month - 1 if month == 0: month = 12 year = year -1 if month in [4, 6, 9, 11]: day = 30 elif month == 2: day = 28 else: day = 31 hour = hour - 9 post.date = datetime.datetime(year, month, day, hour, n.minute, n.second) post.post_status = 'publish' post.thumbnail = img_info['id'] post.id = wp.call(NewPost(post)) return else: # Edit Flag #post = WordPressPost() post.title = title post.content = apply_language_setting(html) post.terms_names = { 'post_tag': [rtcprof.name, 'RTC'], 'category': ['RTComponents'] } post.slug = rtcprof.name n = datetime.datetime.now() year = n.year month = n.month day = n.day hour = n.hour if n.hour < 9: day = day - 1 hour = hour + 24 if day == 0: month = month - 1 if month == 0: month = 12 year = year -1 if month in [4, 6, 9, 11]: day = 30 elif month == 2: day = 28 else: day = 31 hour = hour - 9 post.date = datetime.datetime(year, month, day, hour, n.minute, n.second) post.post_status = 'publish' post.thumbnail = img_info['id'] wp.call(posts.EditPost(post.id, post))
"""!!! EDIT THESE VALUES, MAKE SURE TO LEAVE /XMLRPC.PHP AT THE END !!!""" xmlrpc_endpoint = 'http://eidmantas.com/xmlrpc.php' username = '******' password = '******' wp = Client(xmlrpc_endpoint, username, password) """!!! DONT FORGET TO UPLOAD YOUR FILE AND CHANGE ITS NAME, OR THE VARIABLE HERE !!!""" filename = 'your-ghost-export-file.json' with open(filename) as f: text = f.read() data = json.loads(text) for p in data['db'][0]['data']['posts']: print p['title'] date = p.get('published_at', None) if date is None: p.get('created_at') post = WordPressPost() post.slug = p['slug'] post.content = p['html'] post.title = p['title'] post.post_status = 'publish' try: post.date = parse(date) except: exit wp.call(NewPost(post))
#!/usr/bin/env python3 from wordpress_xmlrpc import Client, WordPressPost from wordpress_xmlrpc.methods.posts import GetPosts, NewPost from config import blog_url, username, password wp = Client(blog_url, username, password) post = WordPressPost() post.title = 'Testing post.title.' post.content = 'Testing post.content. This is a test.' post.slug = 'Testing post.slug' post.terms_names = { 'post_tag': ['IPA', 'UK'], 'category': ['BeerBods cheat sheets'] } wp.call(NewPost(post))
import sys import markdown2 from markdown2 import Markdown import frontmatter from wordpress_xmlrpc import Client, WordPressPost from wordpress_xmlrpc.methods.posts import GetPosts, NewPost from wordpress_xmlrpc.methods.users import GetUserInfo dir = sys.argv[1] post = frontmatter.load(dir) post_title = post.metadata['title'] markdowner = Markdown() post_content_html = markdowner.convert(post.content) post_content_html = post_content_html.encode("utf-8") wp = Client('https://finrodchen.net/xmlrpc.php', 's02260441', 'simontp6nm06') post = WordPressPost() post.post_status = 'draft' post.title = post_title post.content = post_content_html post.slug = post_title post.terms_names = {'category': ['特管辦法']} wp.call(NewPost(post))
xmlrpc_endpoint = 'http://eidmantas.com/xmlrpc.php' username = '******' password = '******' wp = Client(xmlrpc_endpoint, username, password) """!!! DONT FORGET TO UPLOAD YOUR FILE AND CHANGE ITS NAME, OR THE VARIABLE HERE !!!""" filename = 'your-ghost-export-file.json' with open(filename) as f: text = f.read() data = json.loads(text) for p in data['db'][0]['data']['posts']: print p['title'] date = p.get('published_at', None) if date is None: p.get('created_at') post = WordPressPost() post.slug = p['slug'] post.content = p['html'] post.title = p['title'] post.post_status = 'publish' try: post.date = parse(date) except: exit wp.call(NewPost(post))
பிற வடிவங்களில் படிக்க – ''' + book_url + ''' புத்தக எண் - ''' + str(book_number) + ''' ''' print("Publising the Ebook") content = content + extra post.title = book_title + " - " + category + " - " + author post.slug = book_title_in_english post.post_type = 'ebooks' post.content = content post.thumbnail = attachment_id post.comment_status = 'open' post.post_status = 'publish' #post.post_status = 'draft' if artist: post.terms_names = { 'genres': [category], 'contributors': [artist, ebook_maker], 'authors': [author]
# 認証する from wordpress_xmlrpc import Client, WordPressPost from wordpress_xmlrpc.methods.posts import GetPosts, NewPost from wordpress_xmlrpc.methods.users import GetUserInfo wp = Client('http://thisissample.wp.xdomain.jp/xmlrpc.php', 'sample007', 'vvq1meoczwc7') # 投稿する post = WordPressPost() # タイトル post.title = 'My new title' post.content = 'This is the body of my new post.' # タグ post.terms_names = { 'post_tag': ['test', 'firstpost'], 'category': ['Introductions', 'Tests'] } # 投稿URL post.slug = 'sample' # 投稿日時 # 投稿状態 指定なしで下書き post.post_status="publish" # 投稿する。 wp.call(NewPost(post))
def parseDocument(filename): lines = open(filename, 'r').readlines() values = { 'title': '', 'permalink': '', 'layout': 'post', 'tags': '', 'categories': 'default', 'published': 'false' } start = False config = False for i in range(len(lines)): line = lines[i].strip() if config == False: if line == '---': if (start == False): start = True else: # end if (values['title'] == '' or values['permalink'] == ''): printf('title and permalink should not be null!\n') exit() else: # config ok config = True else: try: key = line[:line.find(':')] value = line[line.find(':') + 1:] values[key] = value.strip() except: printf('config failed! (key, value) = (' + key + ', ' + value + ')\n') exit() else: #config ok while len(lines[i]) <= 1: #filter first blank lines i += 1 rawcontent = parseMedia(lines[i:]) rawfilename = filename[:-3] + '.raw.id-' open(rawfilename, 'w').writelines(rawcontent) post = WordPressPost() post.title = values['title'] post.slug = values['permalink'] post.content = pandocTool.md2html(rawfilename) post.post_type = values['layout'] post.post_status = 'publish' if values['published'].lower( ) == 'true' else 'draft' post.comment_status = 'open' #default post.pint_status = 'open' #default post.terms_names = {} #values['tags'] = values['tags'].replace(',', ',') compatible with jekyll, use blank #values['categories'] = values['categories'].replace(',', ',') if len(values['tags']) > 0: post.terms_names['post_tag'] = [ tag.strip() for tag in values['tags'].split() if len(tag) > 0 ] if len(values['categories']) > 0: post.terms_names['category'] = [ cate.strip() for cate in values['categories'].split() if len(cate) > 0 ] return post
os.system(ia_upload) audioURL = "https://archive.org/download/%s/%s" % ( ia_identifier, audioTitleInEnglish + ".mp3") print("Uploaded to " + audioURL) print("Posting into WordPress") wp_username = audio_info['wp_username'] wp_password = audio_info['wp_password'] wpBlogUrl = audio_info['wp_blog_url'] + '/xmlrpc.php' client = Client(wpBlogUrl, wp_username, wp_password) post = WordPressPost() content = "%s \n %s" % (audioURL, audioComments) post.title = title post.content = content post.post_status = 'publish' post.comment_status = 'open' post.terms_names = {'category': ['Podcast']} post.slug = audioTitleInEnglish if "kaniyam.com" in wpBlogUrl: post.post_type = 'podcast' post.terms_names = "" print("Publishing to Kaniyam") post.id = client.call(posts.NewPost(post)) print("Posted into WordPress")
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 main(): fileName = '2000.txt' base_url = 'https://law.justia.com' url_list = [] # Source file with links to scrape and upload with open(fileName, 'r') as f: url_list = f.readlines() # Main loop for page in url_list[:]: full_link = base_url + page.strip() soup = connect(full_link) cat_list = [] # Navigation elements: nav = soup.find('nav', {'class': 'breadcrumbs small-font font-helvetica'}) print '\nStarting with url:', base_url + page.strip() if not nav.contents[-1].strip().encode('utf-8'): c = nav.prettify().split('<span class="breadcrumb-separator">') soup2 = BeautifulSoup( ''.join(c).replace("\n", '').replace('<em>', ''), 'html.parser') tmp = [] for i in soup2: tmp.append(i) tag = tmp[-1].replace(' ', ' ').strip().encode('utf-8') print 'Tag:', tag else: tag = nav.contents[-1].strip().encode('utf-8') print 'Tag:', tag # Getting categories: for i in nav.findAll('a'): if not i.text == None and not i.text == 'Justia': cat_list.append(i.text) print 'Category:', cat_list # Getting Title title = soup.find('h1').get_text(' ') title2 = soup.find('h1').get_text('<br>') print 'Title:', title.encode('utf-8') # Getting rid of Title(h1), Metadata links, Download PDF links, Front Matter links [x.extract() for x in soup.findAll('h1')] try: [x.extract() for x in soup.find('a', {'id': 'metadata-link'})] [x.extract() for x in soup.find('div', {'id': 'metadata'})] except TypeError: 'Possible type error when trying to remove "metadata" references.' try: [x.extract() for x in soup('li', text=re.compile('Front Matter'))] except AttributeError: "Probably element 'Front Matter' wasn't found" try: [x.extract() for x in soup.find('div', {'class': 'downloadlink'})] except TypeError: 'Possible type error when trying to remove "downloadLink"' [x.extract() for x in soup('a', text=re.compile('Download PDF'))] # Remove tags that don't contain any text [x.extract() for x in soup.findAll() if x.text.isspace()] results = soup.find('div', {'class': 'wrapper jcard has-padding-30 blocks'}) # Body of page body = unicode(results).replace('\\n', '').replace('<br>', '').replace("</br>", "") # Dealing with internal navigation try: for a in soup.findAll('a'): a['href'] = a['href'].replace('index.html', '') a['href'] = a['href'].replace('/', '') except KeyError: print 'Some key error when dealing with internal link navigation;' cat_list.append(tag) for i in range(len(cat_list)): cat_list[i] = cat_list[i][:180] # Wordpress procedures: # Used for retrying if there was an error while uploading retries = 0 retries_bool = False while retries < 4: try: client = Client('https://www.statutebase.com/xmlrpc.php', '<USERNAME>', '<PASSWORD') body = unicode(results).replace('\\n', '').replace( '<br>', '').replace("</br>", "") post = WordPressPost() post.title = title2[:100] post.slug = page.replace('/', '') post.post_status = 'publish' post.content = body post.terms_names = { 'post_tag': cat_list[2:], 'category': cat_list[:2], } post.id = client.call(posts.NewPost(post)) retries = 5 retries_bool = True print 'Article uploaded!' except Exception as e: time.sleep(5) retries = retries + 1 retries_bool = False if '500' in e: print 'Error 500 detected. Skipping current article' retries = True print 'Error while trying to upload:', e pass if retries_bool == False: with open(fileName[:fileName.find('.txt')] + '_errors.txt', 'a') as f: f.write(page.strip() + '\n') # Write url on which error occured time.sleep(0.5)