-
Notifications
You must be signed in to change notification settings - Fork 4
/
fetch.py
executable file
·127 lines (108 loc) · 4.01 KB
/
fetch.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
import json
import datetime
from trovebox import Trovebox
# Only the following fields will be exported:
# (from https://github.com/photo/export-openphoto/issues/2)
EXPORT_FIELDS = ["appid",
"dateTaken",
"dateUploaded",
"description",
"filenameOriginal",
"hash",
"latitude",
"longitude",
"license",
"permission",
"rotation",
"status",
"tags",
"timestamp",
"title",
"views",
]
# main program
def fetch(client):
per_page = 100
# we'll paginate through the results
# start at `page` and get `per_page` results at a time
page=1
# store everything in a list or array or whatever python calls this
photos_out=[]
# while True loop till we get no photos back
while True:
# call the photos.list API
# https://trovebox.com/documentation/api/GetPhotos
print "Fetching page %d..." % page,
photo_list = client.photos.list(pageSize=per_page, page=page)
print "OK"
# increment the page number before we forget so we don't endlessly loop
page = page+1;
# if the list of photos is empty we must have reached the end of this user's library and break out of the while True
if len(photo_list) == 0:
break;
# else we loop through the photos
for photo in photo_list:
# get all the data we can
p = {}
fields = photo.get_fields()
for field in fields:
if field in EXPORT_FIELDS:
p[field] = fields[field]
p['photo'] = photo.pathOriginal
t = datetime.datetime.fromtimestamp(float(photo.dateUploaded))
filename = '%s-%s' % (t.strftime('%Y%m%dT%H%M%S'), photo.id)
print " * Storing photo %s to fetched/%s.json" % (photo.id, filename),
f = open("fetched/%s.json" % filename, 'w')
f.write(json.dumps(p))
f.close()
print "OK"
# 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="Backup your Trovebox photos")
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 fetched directory exist else create it
createDirectorySafe('fetched')
fetch(client)