forked from photo/import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.py
executable file
·82 lines (66 loc) · 2.34 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
75
76
77
78
79
80
81
82
#!/usr/bin/env python
# import os for file system functions
import os
# import json
import json
# shutil for file renaming
import shutil
import sys
import time
# import flickrapi
# `easy_install flickrapi` or `pip install flickrapi`
from openphoto import OpenPhoto
from os.path import join, getsize
# main program
def import_into_openphoto(client):
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),
sys.stdout.flush()
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: %d - %s" % (result['code'], result['message'])
errored = errored + 1
shutil.move(infile, "errored/%s" % i)
sys.stdout.flush()
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)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Import photos into an OpenPhoto instance')
parser.add_argument('--host', required=True)
parser.add_argument('--consumer-key', required=True)
parser.add_argument('--consumer-secret', required=True)
parser.add_argument('--token', required=True)
parser.add_argument('--token-secret', required=True)
config = parser.parse_args()
client = OpenPhoto(config.host, config.consumer_key, config.consumer_secret, config.token, config.token_secret)
# check if a processed and errored directories exist else create them
createDirectorySafe('processed')
createDirectorySafe('errored')
import_into_openphoto(client)