Another python bot for tumblr tumbload3r tumblr upload bot
import os
import json
import time
import base64
from requests_oauthlib import OAuth1Session
class Tumbload3r:
def __init__(self, key, secret, blog):
self.key = key
self.secret = secret
self.blog = blog
self.request_token_url = 'http://www.tumblr.com/oauth/request_token'
self.authorization_base_url = 'http://www.tumblr.com/oauth/authorize'
self.access_token_url = 'http://www.tumblr.com/oauth/access_token'
self.callback_uri = 'http://www.tumblr.com/dashboard'
self.details = 'details.txt'
currentDir = os.path.dirname(__file__)
self.images_folder = os.path.join(currentDir, 'images')
self.tumblr = self.auth()
def auth(self):
tumblr = OAuth1Session(self.key, client_secret=self.secret,
callback_uri=self.callback_uri)
tumblr.fetch_request_token(self.request_token_url)
authorization_url = tumblr.authorization_url(self.authorization_base_url)
print('Go to this link and authorize: ', authorization_url)
redirect_response = input('Paste the full redirect URL here: ')
tumblr.parse_authorization_response(redirect_response)
tumblr.fetch_access_token(self.access_token_url)
return tumblr
def postPicture(self, image, caption=None, link=None, state='published', tags=None):
endpoint = 'http://api.tumblr.com/v2/blog/{}/post'.format(blog)
imageBase64 = self.imageToBase64(image)
payload = {'type':'photo',
'state':state,
'data64':imageBase64,
'caption':caption,
'link':link,
'tags':tags}
response = self.tumblr.post(endpoint, data=payload)
return response
def imageToBase64(self, imagePath):
with open(imagePath, 'rb') as image_file:
imageBase64 = base64.b64encode(image_file.read())
return imageBase64
def autoMode(self, delay=1):
print('-->Starting automatic upload.')
images = os.listdir(self.images_folder)
with open(self.details) as details_file:
detailsDict = json.load(details_file)
for image in images:
print('-->Start uploading {}.'.format(image))
imagePath = os.path.join(self.images_folder, image)
self.postPicture(imagePath, caption=detailsDict['caption'],
link=detailsDict['link'],
state=detailsDict['state'],
tags=detailsDict['tags'])
print('-->Uploaded {}.'.format(image))
time.sleep(delay)
print('-->All Images uploaded.')
def manualMode(self):
print('-->Starting manual upload.')
images = os.listdir(self.images_folder)
for image in images:
imagePath = os.path.join(self.images_folder, image)
print('Input details for image {}.'.format(image))
input_caption = input('-->Caption: ')
input_link = input('-->link: ')
input_state = input('-->state: ')
input_tags = input('-->tags: ')
print('-->Start uploading {}.'.format(image))
self.postPicture(imagePath, caption=input_caption,
link=input_link,
state=input_state,
tags=input_tags)
print('-->Uploaded {}.'.format(image))
print('-->All Images uploaded.')
if __name__ == '__main__':
with open('auth.txt') as auth_file:
authDict = json.load(auth_file)
key = authDict['key']
secret = authDict['secret']
blog = authDict['blog']
print('Tumbload3r v0.1 made by daveter9@TBN')
print('Please do not exceed the request limit of 1000/hour and 5000/day')
tumbload3r = Tumbload3r(key, secret, blog)
while True:
input_response = input('->What do you want to do?(auto/manual/quit)')
if input_response == 'auto':
delay = input('What should the delay be between requests in seconds(1 is recommended): ')
try:
int(delay)
except:
delay = 1
tumbload3r.autoMode(int(delay))
elif input_response == 'manual':
tumbload3r.manualMode()
elif input_response == 'quit':
break
else:
print('->That is not a valid command.')
pass