-
Notifications
You must be signed in to change notification settings - Fork 11
/
import.py
executable file
·127 lines (105 loc) · 3.98 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/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 logging
# import flickrapi
# `easy_install flickrapi` or `pip install flickrapi`
from trovebox import Trovebox, TroveboxError, TroveboxDuplicateError
from os.path import join, getsize
# main program
def import_into_trovebox(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()
shutil.move(infile, "errored/%s" % i)
params = json.loads(json_str)
# If the upload fails for any reason, an exception will be raised
try:
resp = client.post('/photo/upload.json', **params)
if resp['code'] != 201:
raise TroveboxError("Unexpected response code: %d: %s"%
(resp['code'], resp['message']))
print "OK"
processed = processed + 1
shutil.move("errored/%s" % i, "processed/%s" % i)
except TroveboxDuplicateError:
print "DUPLICATE: This photo already exists on the server"
shutil.move("errored/%s" % i, "duplicates/%s" % i)
errored = errored + 1
except TroveboxError as error:
print "FAILED: %s" % error
errored = errored + 1
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 Trovebox instance')
parser.add_argument('--config', help="Configuration file to use")
parser.add_argument('--host', help="Hostname of the Trovebox server (overrides config_file)")
parser.add_argument('--consumer-key')
parser.add_argument('--consumer-secret')
parser.add_argument('--token')
parser.add_argument('--token-secret')
parser.add_argument('--debug', help="Print extra debug information", action="store_true")
config = parser.parse_args()
if config.debug:
logging.basicConfig(level=logging.DEBUG)
# Host option overrides config file settings
if config.host:
client = Trovebox(host=config.host, consumer_key=config.consumer_key,
consumer_secret=config.consumer_secret,
token=config.token, token_secret=config.token_secret)
else:
try:
client = Trovebox(config_file=config.config)
except IOError as error:
print error
print
print "You must create a configuration file in ~/.config/trovebox/default"
print "with the following contents:"
print " host = your.host.com"
print " consumerKey = your_consumer_key"
print " consumerSecret = your_consumer_secret"
print " token = your_access_token"
print " tokenSecret = your_access_token_secret"
print
print "To get your credentials:"
print " * Log into your Trovebox site"
print " * Click the arrow on the top-right and select 'Settings'."
print " * Click the 'Create a new app' button."
print " * Click the 'View' link beside the newly created app."
print
print error
sys.exit(1)
# check if a processed and errored directories exist else create them
createDirectorySafe('processed')
createDirectorySafe('errored')
createDirectorySafe('duplicates')
import_into_trovebox(client)