1717import yaml
1818
1919CONFIG_FILE = "~/.flickr_download"
20+ OAUTH_TOKEN_FILE = "~/.flickr_token"
2021
2122
22- def _init (key , secret ):
23+ def _init (key , secret , oauth ):
2324 """
2425 Initialize API.
2526
@@ -29,6 +30,27 @@ def _init(key, secret):
2930 @param secret: str, API secret
3031 """
3132 Flickr .set_keys (key , secret )
33+ if not oauth :
34+ return True
35+
36+ if os .path .exists (os .path .expanduser (OAUTH_TOKEN_FILE )):
37+ Flickr .set_auth_handler (os .path .expanduser (OAUTH_TOKEN_FILE ))
38+ return True
39+
40+ # Get new OAuth credentials
41+ auth = Flickr .auth .AuthHandler () # creates the AuthHandler object
42+ perms = "read" # set the required permissions
43+ url = auth .get_authorization_url (perms )
44+ print
45+ print "Enter the following url in a browser to authorize the application:"
46+ print url
47+ print "Copy and paste the <oauth_verifier> value from XML here and press return:"
48+ Flickr .set_auth_handler (auth )
49+ token = raw_input ()
50+ auth .set_verifier (token )
51+ auth .save (os .path .expanduser (OAUTH_TOKEN_FILE ))
52+ print "OAuth token was saved, re-run script to use it."
53+ return False
3254
3355
3456def _load_defaults ():
@@ -99,18 +121,24 @@ def main():
99121 help = 'Flickr API key' )
100122 parser .add_argument ('-s' , '--api_secret' , type = str ,
101123 help = 'Flickr API secret' )
124+ parser .add_argument ('-t' , '--user_auth' , action = 'store_true' ,
125+ help = 'Enable user authentication' )
102126 parser .add_argument ('-l' , '--list' , type = str , metavar = 'USER' ,
103127 help = 'List photosets for a user' )
104128 parser .add_argument ('-d' , '--download' , type = str , metavar = 'SET_ID' ,
105129 help = 'Download the given set' )
106130 parser .set_defaults (** _load_defaults ())
107131
108132 args = parser .parse_args ()
133+
109134 if not args .api_key or not args .api_secret :
110135 print >> sys .stderr , 'You need to pass in both "api_key" and "api_secret" arguments'
111136 return 1
112137
113- _init (args .api_key , args .api_secret )
138+ ret = _init (args .api_key , args .api_secret , args .user_auth )
139+ if not ret :
140+ return 1
141+
114142 if args .list :
115143 print_sets (args .list )
116144 elif args .download :
0 commit comments