Another python bot for tumblr tumbload3r tumblr upload bot


SUBMITTED BY: alemotta

DATE: Feb. 28, 2017, 10:43 p.m.

FORMAT: Text only

SIZE: 4.7 kB

HITS: 543

  1. Another python bot for tumblr tumbload3r tumblr upload bot
  2. import os
  3. import json
  4. import time
  5. import base64
  6. from requests_oauthlib import OAuth1Session
  7. class Tumbload3r:
  8. def __init__(self, key, secret, blog):
  9. self.key = key
  10. self.secret = secret
  11. self.blog = blog
  12. self.request_token_url = 'http://www.tumblr.com/oauth/request_token'
  13. self.authorization_base_url = 'http://www.tumblr.com/oauth/authorize'
  14. self.access_token_url = 'http://www.tumblr.com/oauth/access_token'
  15. self.callback_uri = 'http://www.tumblr.com/dashboard'
  16. self.details = 'details.txt'
  17. currentDir = os.path.dirname(__file__)
  18. self.images_folder = os.path.join(currentDir, 'images')
  19. self.tumblr = self.auth()
  20. def auth(self):
  21. tumblr = OAuth1Session(self.key, client_secret=self.secret,
  22. callback_uri=self.callback_uri)
  23. tumblr.fetch_request_token(self.request_token_url)
  24. authorization_url = tumblr.authorization_url(self.authorization_base_url)
  25. print('Go to this link and authorize: ', authorization_url)
  26. redirect_response = input('Paste the full redirect URL here: ')
  27. tumblr.parse_authorization_response(redirect_response)
  28. tumblr.fetch_access_token(self.access_token_url)
  29. return tumblr
  30. def postPicture(self, image, caption=None, link=None, state='published', tags=None):
  31. endpoint = 'http://api.tumblr.com/v2/blog/{}/post'.format(blog)
  32. imageBase64 = self.imageToBase64(image)
  33. payload = {'type':'photo',
  34. 'state':state,
  35. 'data64':imageBase64,
  36. 'caption':caption,
  37. 'link':link,
  38. 'tags':tags}
  39. response = self.tumblr.post(endpoint, data=payload)
  40. return response
  41. def imageToBase64(self, imagePath):
  42. with open(imagePath, 'rb') as image_file:
  43. imageBase64 = base64.b64encode(image_file.read())
  44. return imageBase64
  45. def autoMode(self, delay=1):
  46. print('-->Starting automatic upload.')
  47. images = os.listdir(self.images_folder)
  48. with open(self.details) as details_file:
  49. detailsDict = json.load(details_file)
  50. for image in images:
  51. print('-->Start uploading {}.'.format(image))
  52. imagePath = os.path.join(self.images_folder, image)
  53. self.postPicture(imagePath, caption=detailsDict['caption'],
  54. link=detailsDict['link'],
  55. state=detailsDict['state'],
  56. tags=detailsDict['tags'])
  57. print('-->Uploaded {}.'.format(image))
  58. time.sleep(delay)
  59. print('-->All Images uploaded.')
  60. def manualMode(self):
  61. print('-->Starting manual upload.')
  62. images = os.listdir(self.images_folder)
  63. for image in images:
  64. imagePath = os.path.join(self.images_folder, image)
  65. print('Input details for image {}.'.format(image))
  66. input_caption = input('-->Caption: ')
  67. input_link = input('-->link: ')
  68. input_state = input('-->state: ')
  69. input_tags = input('-->tags: ')
  70. print('-->Start uploading {}.'.format(image))
  71. self.postPicture(imagePath, caption=input_caption,
  72. link=input_link,
  73. state=input_state,
  74. tags=input_tags)
  75. print('-->Uploaded {}.'.format(image))
  76. print('-->All Images uploaded.')
  77. if __name__ == '__main__':
  78. with open('auth.txt') as auth_file:
  79. authDict = json.load(auth_file)
  80. key = authDict['key']
  81. secret = authDict['secret']
  82. blog = authDict['blog']
  83. print('Tumbload3r v0.1 made by daveter9@TBN')
  84. print('Please do not exceed the request limit of 1000/hour and 5000/day')
  85. tumbload3r = Tumbload3r(key, secret, blog)
  86. while True:
  87. input_response = input('->What do you want to do?(auto/manual/quit)')
  88. if input_response == 'auto':
  89. delay = input('What should the delay be between requests in seconds(1 is recommended): ')
  90. try:
  91. int(delay)
  92. except:
  93. delay = 1
  94. tumbload3r.autoMode(int(delay))
  95. elif input_response == 'manual':
  96. tumbload3r.manualMode()
  97. elif input_response == 'quit':
  98. break
  99. else:
  100. print('->That is not a valid command.')
  101. pass

comments powered by Disqus