-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_upload.py
executable file
·94 lines (78 loc) · 2.89 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
84
85
86
87
88
89
90
91
92
93
94
#!/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
import base64
import requests
# 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,
#)
# Setup the WordPress client
wptoken = base64.standard_b64encode(config.wpuser + ':' + config.wppass)
wpheaders = {'Authorization': 'Basic ' + wptoken}
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)
wpupload = requests.post(url + '/media', headers=headers, files=file_to_upload)
print "Your image is on " + wpupload.headers['link']
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)
wpupload = requests.post(url + '/media', headers=headers, files=myJpgs)
print "Your image is on " + wpupload.headers['link']
# wait a bit
time.sleep(delay)
break
except ValueError:
print group_name + " not uploaded. Error."
print 'Batch Upload Start'
# run the main program
main()