Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf8 -*-
- ########################################
- # はじめに、LaunchStash.pyを起動し、 #
- # ターミナルで以下のコマンド #
- # pip install python-wordpress-xmlrpc #
- # pip install pillow #
- #######################################
- import os #フォルダの中身を探る
- import sys
- import pickle
- from wordpress_xmlrpc import Client, WordPressPost
- from wordpress_xmlrpc.methods.posts import GetPosts, NewPost, EditPost
- from wordpress_xmlrpc.methods.users import GetUserInfo
- from wordpress_xmlrpc.methods import media
- from wordpress_xmlrpc.compat import xmlrpc_client #画像アップロード
- #import urllib.request
- from PIL import Image #画像のリサイズ
- import urllib.request
- import io
- try:
- import photos
- except:
- pass
- class Setup:
- def authorize_account(url,username,password):
- account = [url,username,password]
- wp = Client('%s/xmlrpc.php' % account[0] ,account[1],account[2])
- wp.call(GetPosts({'number':1})) #試しに一つ取得
- return account
- def save_config(account):
- with open('wordpress.config','wb') as f:
- pickle.dump(account,f)
- class Blog():
- def __init__(self):
- with open('wordpress.config','rb') as f:
- account = pickle.load(f)
- self.url = account[0]
- self.username = account[1]
- self.password = account[2]
- def post(self,title,content,status="publish",id=None,attachment_id=None,tag=None,category=None):
- wp = Client('%s/xmlrpc.php' % self.url, self.username, self.password)
- p = WordPressPost()
- p.title = title
- p.content = content
- p.post_status = status
- p.terms_names = {}
- if attachment_id:
- p.thumbnail = attachment_id
- if tag:
- p.terms_names.update({"post_tag":tag})
- if category:
- p.terms_names.update({"category":category})
- # 新規投稿 or 上書き
- if id:
- post_id = id
- wp.call(EditPost(post_id,p))
- else:
- wp.call(NewPost(p))
- def upload_img(self,url):
- wp = Client('%s/xmlrpc.php' % self.url,self.username,self.password)
- try:
- img_read = urllib.request.urlopen(url).read()
- except:
- p = photos.pick_asset()
- img_read = p.read()
- img_bin = io.BytesIO(img_read)
- pil_img = Image.open(img_bin)
- img = io.BytesIO()
- pil_img.save(img,"JPEG")
- data = {'bits':img.getvalue(),'name': os.path.basename("img.jpg"),'type': 'image/jpeg'}
- response = wp.call(media.UploadFile(data))
- return response # response["url"],response["id"]
- class Blog_UI():
- def __init__(self):
- self.line = "\n" + "-"*30 + "\n"
- def setup(self):
- l = self.line
- S = Setup
- print(l + "初回起動時のアカウント設定を行います。" + l)
- while True:
- try:
- account = S.authorize_account(input("Wordpress URL:"),input("USERNAME:"),input("PASSWORD:"))
- print(l+"Login Scceeded."+l)
- S.save_config(account)
- break
- except:
- print(l+"Setup ERROR. Try Again."+l)
- def upload_img(self):
- B = Blog()
- l = self.line
- url = input("画像URL:")
- print(l + "%sから画像を取得しアップロードします..." % url + l)
- response = B.upload_img(url)
- print("アップロードが完了しました。")
- print(l + "attachment_id:%s" % response["id"] + l + response['url'] + l)
- return response
- def post(self):
- l = self.line
- title = input("記事のタイトル:")
- content = input("文章をコピペ:")
- print(l + "公開?下書きならそのままスルー(改行)" + l)
- ask = input("公開しますか?(y):")
- if ask == "y":
- status = "publish"
- else:
- status = "draft"
- print(l + "アイキャッチ画像を設定するならそのままスルー(改行)" + l)
- img_ask = input("アイキャッチ画像を登録しますか?(y)")
- if img_ask == "y":
- response = self.upload_img()
- attachment_id = response["id"]
- else:
- attachment_id = None
- print(l+"ブログにアップロードしています。"+l)
- Blog().post(title,content,status=status,id=None,attachment_id=attachment_id,tag=None,category=None)
- print(l+"アップーロードが完了しました。"+l)
- def main():
- try:
- B = Blog()
- Blog_UI().post()
- except:
- B = Blog_UI()
- B.setup()
- if __name__ == '__main__':
- Blog_UI().post()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement