Guest User

Untitled

a guest
Jun 28th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. import requests
  2. import tempfile
  3. import os
  4. import pypandoc
  5. import json
  6. from getpass import getpass
  7. from subprocess import call
  8.  
  9. EDITOR = os.environ.get('EDITOR', 'vim')
  10. ARTICLE_URL = 'http://127.0.0.1:8000/api/articles/'
  11. IMAGE_URL = 'http://127.0.0.1:8000/api/media/images/'
  12. TOKEN_URL = 'http://127.0.0.1:8000/api-auth-token/'
  13.  
  14.  
  15. def obtain_token(username, password):
  16. """Authenticate and obtain token"""
  17. data = {'username': username, 'password': password}
  18. req = requests.post(TOKEN_URL, data=data)
  19. res = req.json()
  20. token = res['token']
  21. headers = {'Authorization': 'Token {}'.format(token)}
  22. return headers
  23.  
  24.  
  25. """
  26. Ask for user credentials to set header, not sure how to
  27. place this code in a functional way
  28. """
  29. username = input("Username: ")
  30. password = getpass()
  31.  
  32. headers = obtain_token(username=username, password=password)
  33.  
  34.  
  35. def image_upload(img_file, img_title):
  36. """Upload image with title"""
  37. files = {'image': img_file}
  38. payload = {'title': img_title}
  39. upload = requests.post(IMAGE_URL, files=files, data=payload,
  40. headers=headers)
  41. return upload
  42.  
  43.  
  44. md_urls = bytearray()
  45.  
  46.  
  47. def img_docload():
  48. """
  49. Get the latest uploaded image and convert it
  50. to a html string so that pypandoc again can make
  51. it into markdown, then extend each to the bytearray.
  52. """
  53. get_url = requests.get(IMAGE_URL)
  54. get_json = json.loads(get_url.content)
  55. clean = get_json[-1]['image']
  56. md_html = "<img src='"+clean+"'>"
  57. md = pypandoc.convert_text(md_html, 'md', format='html')
  58. md_urls.extend(md.encode())
  59.  
  60.  
  61. def article(headline, summary):
  62. """
  63. Make a tempfile and write the list of markdown inserts,
  64. then open it in Vim or any default editor. Save and quit
  65. to convert from markdown to html and upload the article.
  66. """
  67. with tempfile.NamedTemporaryFile(suffix='.md') as tmp:
  68. tmp.write(md_urls)
  69. tmp.flush()
  70. call([EDITOR, tmp.name])
  71. tmp.seek(0)
  72. edited = tmp.read()
  73. article = edited.decode('utf-8')
  74. content = pypandoc.convert_text(article, 'html', format='md')
  75. payload = {
  76. 'headline': headline,
  77. 'summary': summary,
  78. 'content': content,
  79. }
  80. upload = requests.post(ARTICLE_URL, json=payload, headers=headers)
  81. return upload
  82.  
  83.  
  84. def main():
  85. while True:
  86. action = input("Upload image? (k): ")
  87. if action == 'k':
  88. img_file = open(input("Image - Filename: ").strip('''), 'rb')
  89. img_title = input("Image - Title: ")
  90. image_upload(img_file=img_file, img_title=img_title)
  91. img_docload()
  92. continue
  93. else:
  94. headline = input("Article - Headline: ")
  95. summary = input("Article - Summary: ")
  96. article(headline=headline, summary=summary)
  97. print("Article is published")
  98. break
  99.  
  100. main()
Add Comment
Please, Sign In to add comment