Skip to content

Commit c229a9d

Browse files
toxygenbeaufour
authored andcommitted
Added user authentication via OAuth
1 parent 9fa8215 commit c229a9d

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ You can also set your API key and secret in `~/.flickr_download`:
1313
api_key: my_key
1414
api_secret: my_secret
1515

16+
User Authentication Support
17+
===========================
18+
19+
The script also allows you to authenticate as your user account. That way you can download sets that
20+
are private and public photos that are restricted. To use this mode, pass in `-t` to the script too.
21+
22+
The setup the first time is slightly hacky, but it works :)
23+
1624
Requirements
1725
============
1826

flickr_download/flick_download.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
import yaml
1818

1919
CONFIG_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

3456
def _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:

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ def readme():
66
return f.read()
77

88
setup(name='flickr_download',
9-
version='0.2.2',
9+
version='0.2.3',
1010
description='Download a Flickr set',
1111
long_description=readme(),
1212
url='https://github.com/beaufour/flickr-download.git',
13-
author='Allan Beaufour',
14-
author_email='[email protected]',
13+
author='Allan Beaufour, Marian Such',
14+
1515
license='MIT',
1616
packages=['flickr_download'],
1717
install_requires=[

0 commit comments

Comments
 (0)