-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflickr.py
More file actions
executable file
·202 lines (190 loc) · 6.25 KB
/
flickr.py
File metadata and controls
executable file
·202 lines (190 loc) · 6.25 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/python
# -*- coding: utf-8 -*-
import ConfigParser
import sys
import os
import logging
from datetime import datetime
import flickrapi
import webbrowser
import time
cfg = None
localpath = ''
configFilename = ''
logfilename = ''
pics_folder = ''
fileCount = 0
fileSize = 0
flickrObj = None
flickr_key = ''
flickr_secret = ''
flickr_access_token = ''
is_public = 0
is_family = 0
is_friend = 0
tries = 0
allowedfiles = ['jpg', 'jpeg', 'png', 'gif', 'avi', 'mp4', '3gp', 'tif', 'bmp']
def main():
doGreeting()
doLocalSetup()
getConfigs()
checkAuth()
uploadPhotos()
def doGreeting():
sys.stderr.write("\x1b[2J\x1b[H")
greetingStr = ''
greetingStr += '\nFlickr Folder Uploader'
greetingStr += '\n'
greetingStr += '\nA simple Python script to upload a directory of photos to Flickr.'
greetingStr += '\nFor full details see http://freelancewebdev.github.com/FlickrPyUploader'
greetingStr += '\n'
greetingStr += '\nCopyright (C) ' + str(datetime.now().year) + ' Joe Molloy (info[at]hyper-typer.com)'
greetingStr += '\nThis program comes with ABSOLUTELY NO WARRANTY.'
greetingStr += '\nThis is free software, licensed under the GPL V3'
greetingStr += '\nand you are welcome to redistribute it'
greetingStr += '\n'
print greetingStr
def doLocalSetup():
global path, configFileName, logFileName
print 'Setting up basic settings'
path = os.path.dirname(os.path.abspath(__file__))
configFileName = __file__ + '.cfg'
logFileName = __file__ + '.log'
logging.basicConfig(filename=logFileName,level=logging.DEBUG)
logging.info('Logging set up')
print 'Basic setup complete'
def getConfigs():
global cfg, flickr_key, flickr_secret, pics_folder, flickr_access_token, is_public, is_family, is_friend
print '\nChecking configuration data...'
logging.info('Checking config data')
cfg = ConfigParser.ConfigParser()
try:
cfg.read(os.path.join(localpath, configFileName))
logging.info('Config file loaded')
except:
logging.debug('Initial attempt to read config file failed')
try:
os.chmod(os.path.join(localpath,configFileName),0777)
cfg.read(os.path.join(localpath,configFileName))
logging.debug('Config file loaded after changing permissions')
except Exception as e:
logging.critical('Reading config file failed. ' + str(e))
print 'There was a problem reading the config file'
print 'Please ensure you have renamed'
print '\'' + configFileName + '_sample\' to \'' + configFileName + '\''
print 'and added your own values as appropriate'
sys.exit()
try:
flickr_key = cfg.get('flickr','key')
except:
print 'No Flickr application key set in config file. Exiting.'
logging.critical('No flickr application key found in config file')
sys.exit(1)
try:
flickr_secret = cfg.get('flickr','secret')
except:
print 'No Flickr application secret set in config file. Exiting.'
logging.critical('No Flickr application secret found in config file. Exiting.')
sys.exit(1)
try:
flickr_access_token = cfg.get('flickr','access_token')
except:
pass
try:
pics_folder = cfg.get('flickr','pics-folder')
except:
print 'No folder specified to upload from. Exiting.'
logging.critical('No folder specified to upload photos/videos from.')
sys.exit(1)
if not os.path.isdir(pics_folder):
print 'Photos folder not found. Exiting.'
logging.critical('Photo folder not found')
sys.exit(1)
else:
print 'Uplaoding photos from %s.' %picsfolder
try:
is_public = cfg.get('flickr','is-public')
except:
print 'Setting uploads to private'
logging.debug('No configuration setting for is-public found so setting uploads to private')
pass
try:
is_family = cfg.get('flickr','is-family')
except:
print 'Setting uploads to private from family'
logging.debug('No configuration setting for is-family found so setting uploads to private')
pass
try:
is_friend = cfg.get('flickr','is-friend')
except:
print 'Setting uploads to private'
logging.debug('No configuration setting for is-friend found so setting uploads to private')
pass
print 'Configuration values set up'
def checkAuth():
global flickrObj
flickrObj = flickrapi.FlickrAPI(flickr_key, flickr_secret)
try:
(flickr_access_token, frob) = flickrObj.get_token_part_one(perms='write')
except:
print 'We are running purely in the console.'
print 'You will have to authenticate manually'
(flickr_access_token, frob) = flickrObj.get_token_part_one(perms='write', auth_callback=manualAuth)
if not flickr_access_token:
raw_input("Press ENTER after you authorized this program")
else:
cfg.set('flickr','access_token',flickr_access_token)
try:
with os.open(configFilename,'w') as cf:
cfg.write(cf)
except:
print 'Failed to write token to config file'
logging.debug('Failed to write token to config file')
flickrObj.get_token_part_two((flickr_access_token, frob))
print 'We are now authenticated.'
def uploadPhotos():
global fileCount, tries
tries = 0
for root, dirs, files in os.walk(pics_folder):
for f in files:
ext = os.path.splitext(f)[1].lower()
if ext in allowedfiles:
uploadPhoto(f,root)
print 'Uploaded %d photos' % fileCount
def uploadPhoto(f,root):
global tries, fileCount
print 'Uploading ' + f
ffullpath = os.path.join(root,f)
title = os.path.splitext(f)[0]
desc = '%s (uploaded by Python Flickrapi)' % f
tags = 'flickrapi flickrpyuploadr'
try:
response = flickrObj.upload(filename=ffullpath,title=title,description=desc,tags=tags,is_public=is_public,is_family=is_family,is_friend=is_friend,callback=showProgress)
fileCount += 1
except Exception as e:
if tries < 10:
print 'There was an error uploading. %s' % str(e)
print 'Retrying in 30 seconds'
time.sleep(30)
try:
response = flickrObj.upload(ffullpath,title,desc,tags,0,0,0,callback=showProgress)
fileCount += 1
except:
print 'Failed again. (%d tries)' % tries
tries += 1
uploadPhoto(f,root)
else:
logging.debug('Failed to upload ' + os.path.join(root,f))
print 'Giving up on ' + os.path.join(root,f) + '\n\n'
def showProgress(progress, done):
if done:
print "Done uploading\n\n"
else:
print "At %s%%" % progress
def manualAuth(frob,perms):
auth_url = flickrObj.auth_url(perms,frob)
print '1. Go to %s in your browser.' % auth_url
print '2. Give this app permission to operate'
print '3. Return here and press enter'
if __name__ == '__main__':
main()