Advertisement
Guest User

Untitled

a guest
Apr 5th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. # -*- coding: utf8 -*-
  2. ########################################
  3. # はじめに、LaunchStash.pyを起動し、 #
  4. # ターミナルで以下のコマンド #
  5. # pip install python-wordpress-xmlrpc #
  6. # pip install pillow #
  7. #######################################
  8. import os #フォルダの中身を探る
  9. import sys
  10. import pickle
  11. from wordpress_xmlrpc import Client, WordPressPost
  12. from wordpress_xmlrpc.methods.posts import GetPosts, NewPost, EditPost
  13. from wordpress_xmlrpc.methods.users import GetUserInfo
  14. from wordpress_xmlrpc.methods import media
  15. from wordpress_xmlrpc.compat import xmlrpc_client #画像アップロード
  16. #import urllib.request
  17. from PIL import Image #画像のリサイズ
  18. import urllib.request
  19. import io
  20. try:
  21. import photos
  22. except:
  23. pass
  24.  
  25. class Setup:
  26. def authorize_account(url,username,password):
  27. account = [url,username,password]
  28. wp = Client('%s/xmlrpc.php' % account[0] ,account[1],account[2])
  29. wp.call(GetPosts({'number':1})) #試しに一つ取得
  30. return account
  31. def save_config(account):
  32. with open('wordpress.config','wb') as f:
  33. pickle.dump(account,f)
  34.  
  35. class Blog():
  36. def __init__(self):
  37. with open('wordpress.config','rb') as f:
  38. account = pickle.load(f)
  39. self.url = account[0]
  40. self.username = account[1]
  41. self.password = account[2]
  42.  
  43. def post(self,title,content,status="publish",id=None,attachment_id=None,tag=None,category=None):
  44. wp = Client('%s/xmlrpc.php' % self.url, self.username, self.password)
  45. p = WordPressPost()
  46. p.title = title
  47. p.content = content
  48. p.post_status = status
  49. p.terms_names = {}
  50. if attachment_id:
  51. p.thumbnail = attachment_id
  52. if tag:
  53. p.terms_names.update({"post_tag":tag})
  54. if category:
  55. p.terms_names.update({"category":category})
  56. # 新規投稿 or 上書き
  57. if id:
  58. post_id = id
  59. wp.call(EditPost(post_id,p))
  60. else:
  61. wp.call(NewPost(p))
  62.  
  63. def upload_img(self,url):
  64. wp = Client('%s/xmlrpc.php' % self.url,self.username,self.password)
  65. try:
  66. img_read = urllib.request.urlopen(url).read()
  67. except:
  68. p = photos.pick_asset()
  69. img_read = p.read()
  70. img_bin = io.BytesIO(img_read)
  71. pil_img = Image.open(img_bin)
  72. img = io.BytesIO()
  73. pil_img.save(img,"JPEG")
  74. data = {'bits':img.getvalue(),'name': os.path.basename("img.jpg"),'type': 'image/jpeg'}
  75. response = wp.call(media.UploadFile(data))
  76. return response # response["url"],response["id"]
  77.  
  78. class Blog_UI():
  79. def __init__(self):
  80. self.line = "\n" + "-"*30 + "\n"
  81.  
  82. def setup(self):
  83. l = self.line
  84. S = Setup
  85. print(l + "初回起動時のアカウント設定を行います。" + l)
  86. while True:
  87. try:
  88. account = S.authorize_account(input("Wordpress URL:"),input("USERNAME:"),input("PASSWORD:"))
  89. print(l+"Login Scceeded."+l)
  90. S.save_config(account)
  91. break
  92. except:
  93. print(l+"Setup ERROR. Try Again."+l)
  94.  
  95. def upload_img(self):
  96. B = Blog()
  97. l = self.line
  98. url = input("画像URL:")
  99. print(l + "%sから画像を取得しアップロードします..." % url + l)
  100. response = B.upload_img(url)
  101. print("アップロードが完了しました。")
  102. print(l + "attachment_id:%s" % response["id"] + l + response['url'] + l)
  103. return response
  104.  
  105. def post(self):
  106. l = self.line
  107. title = input("記事のタイトル:")
  108. content = input("文章をコピペ:")
  109. print(l + "公開?下書きならそのままスルー(改行)" + l)
  110. ask = input("公開しますか?(y):")
  111. if ask == "y":
  112. status = "publish"
  113. else:
  114. status = "draft"
  115. print(l + "アイキャッチ画像を設定するならそのままスルー(改行)" + l)
  116. img_ask = input("アイキャッチ画像を登録しますか?(y)")
  117. if img_ask == "y":
  118. response = self.upload_img()
  119. attachment_id = response["id"]
  120. else:
  121. attachment_id = None
  122. print(l+"ブログにアップロードしています。"+l)
  123. Blog().post(title,content,status=status,id=None,attachment_id=attachment_id,tag=None,category=None)
  124. print(l+"アップーロードが完了しました。"+l)
  125.  
  126. def main():
  127. try:
  128. B = Blog()
  129. Blog_UI().post()
  130. except:
  131. B = Blog_UI()
  132. B.setup()
  133.  
  134. if __name__ == '__main__':
  135. Blog_UI().post()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement