forked from photo/import
/
import.py
executable file
·74 lines (63 loc) · 2.01 KB
/
import.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
# import os for file system functions
import os
# import json
import json
# shutil for file renaming
import shutil
import time
# import flickrapi
# `easy_install flickrapi` or `pip install flickrapi`
from openphoto import OpenPhoto
from os.path import join, getsize
# main program
def main():
print "Enter your OpenPhoto host: ",
host = raw_input()
print "Enter your consumer key: ",
consumerKey = raw_input()
print "Enter your consumer secret: ",
consumerSecret = raw_input()
print "Enter your token: ",
token = raw_input()
print "Enter your token secret: ",
tokenSecret = raw_input()
client = OpenPhoto(host, consumerKey, consumerSecret, token, tokenSecret)
for root, dirs, files in os.walk('fetched/'):
total = len(files)
current = 1
processed = 0
errored = 0
start_time = time.time()
print "Found a total of %d files to process" % total
for i in files:
print "Processing %d of %d %s ..." % (current, total, i),
current = current + 1
infile = "fetched/%s" % i
f = open(infile, 'r')
json_str = f.read()
f.close()
params = json.loads(json_str)
resp = client.post('/photo/upload.json', params)
result = json.loads(resp)
if result['code'] == 201:
print "OK"
processed = processed + 1
shutil.move(infile, "processed/%s" % i)
else:
print "FAILED"
errored = errored + 1
shutil.move(infile, "errored/%s" % i)
end_time = time.time()
total_time = (end_time - start_time) / 60.0
photos_minute = int(total / total_time)
if total > 0:
print "Results. Processed: %d. Errored: %d." % (processed, errored)
print "Imported %d photos at %d photos/minute." % (total, photos_minute)
# create a directory only if it doesn't already exist
def createDirectorySafe( name ):
if not os.path.exists(name):
os.makedirs(name)
# check if a processed and errored directories exist else create them
createDirectorySafe('processed')
createDirectorySafe('errored')
main()