-
Notifications
You must be signed in to change notification settings - Fork 98
/
batch_upload.py
executable file
·83 lines (69 loc) · 2.41 KB
/
batch_upload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
# created by [email protected]
# Batch upload photos post event, if there was no internet connection at the event
# Note, there are limits to how often/much you can upload to Tumblr via their API.
# If you have issues, try to upload in smaller batches or less frequently.
import argparse
import os
import glob
import stat
import sys
import time
import pytumblr # https://github.com/tumblr/pytumblr
import config # this is the config file config.py
# global variables
delay = 1 # delay between uploads. Don't abuse the Tumblr server.
# Setup the tumblr OAuth Client
client = pytumblr.TumblrRestClient(
config.consumer_key,
config.consumer_secret,
config.oath_token,
config.oath_secret,
)
def main():
if config.make_gifs:
files = glob.glob(config.file_path + '*.gif')
files.sort() # organize the list
for f in files:
uploadOne(f)
else:
files = glob.glob(config.file_path + '*.jpg')
files.sort() # organize the list
previous_group = "0000-00-00-00-00-00" # used to not duplicate groups
for f in files:
current_group = f[len(config.file_path):]
current_group = current_group[:19]
if (current_group != previous_group): # remove duplicates
uploadMultiple(current_group) #upload a group of pics
print "Uploaded: " + current_group
previous_group = current_group # remember for next time through loop
print "Completed uploading pics"
def uploadOne(pic):
while True:
try:
file_to_upload = pic
print "Uploading " + file_to_upload
client.create_photo(config.tumblr_blog, state="published", tags=[config.tagsForTumblr], data=file_to_upload)
time.sleep(delay) # wait a bit
# cmd = "mv " + file_to_upload + " " + config.file_path + "/batch_uploaded"
# os.system(cmd) # once uploaded, move to sub directory
break
except ValueError:
print pic + " not uploaded. Error."
def uploadMultiple(group_name):
while True:
try:
# create an array and populate with file paths to our jpgs
myJpgs=[0 for i in range(4)]
for i in range(4):
myJpgs[i]=config.file_path + group_name + "-0" + str(i+1) + ".jpg"
# upload files into one post
client.create_photo(config.tumblr_blog, state="published", tags=[config.tagsForTumblr], format="markdown", data=myJpgs)
# wait a bit
time.sleep(delay)
break
except ValueError:
print group_name + " not uploaded. Error."
print 'Batch Upload Start'
# run the main program
main()