Skip to content

Commit f28859c

Browse files
committed
adds different ways of naming the downloaded files
1 parent d6cd38e commit f28859c

File tree

2 files changed

+87
-14
lines changed

2 files changed

+87
-14
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""
2+
Defines a set of functions that handle naming of the downloaded files.
3+
"""
4+
5+
from collections import defaultdict
6+
7+
8+
def title(pset, photo, suffix):
9+
"""
10+
Name file after title.
11+
12+
@param pset: Flickr.Photoset, the photoset
13+
@param photo: Flickr.Photo, the photo
14+
@param suffice: str, optional suffix
15+
@return: str, the filename
16+
"""
17+
return '{0}{1}.jpg'.format(photo.title, suffix)
18+
19+
20+
def idd(pset, photo, suffix):
21+
"""
22+
Name file after id
23+
24+
@param pset: Flickr.Photoset, the photoset
25+
@param photo: Flickr.Photo, the photo
26+
@param suffice: str, optional suffix
27+
@return: str, the filename
28+
"""
29+
return '{0}{1}.jpg'.format(photo.id, suffix)
30+
31+
32+
def title_and_id(pset, photo, suffix):
33+
"""
34+
Name file after title and photo id
35+
36+
@param pset: Flickr.Photoset, the photoset
37+
@param photo: Flickr.Photo, the photo
38+
@param suffice: str, optional suffix
39+
@return: str, the filename
40+
"""
41+
return '{0}-{1}{2}.jpg'.format(photo.title, photo.id, suffix)
42+
43+
44+
INCREMENT_INDEX = defaultdict(lambda: defaultdict(int))
45+
"""Photoset -> filename index for title_increment function duplicate tracking"""
46+
47+
48+
def title_increment(pset, photo, suffix):
49+
"""
50+
Name file after photo title, but add an incrementing counter on duplicates
51+
52+
@param pset: Flickr.Photoset, the photoset
53+
@param photo: Flickr.Photo, the photo
54+
@param suffice: str, optional suffix
55+
@return: str, the filename
56+
"""
57+
extra = ''
58+
photo_index = INCREMENT_INDEX[pset.id][photo.title]
59+
if photo_index:
60+
extra = '({0})'.format(photo_index)
61+
INCREMENT_INDEX[pset.id][photo.title] += 1
62+
return '{0}{1}{2}.jpg'.format(photo.title, suffix, extra)
63+
64+
65+
HANDLERS = {
66+
'title': title,
67+
'id': idd,
68+
'title_and_id': title_and_id,
69+
'title_increment': title_increment,
70+
}
71+
72+
73+
def get_filename_handler(name='title'):
74+
"""
75+
Returns the given filename handler as a function
76+
@param name: str, name of the handler to return
77+
@return: Function, handler
78+
"""
79+
return HANDLERS[name]

flickr_download/flick_download.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from dateutil import parser
1818
import yaml
1919

20+
from flickr_download.filename_handlers import get_filename_handler
21+
2022
CONFIG_FILE = "~/.flickr_download"
2123
OAUTH_TOKEN_FILE = "~/.flickr_token"
2224

@@ -77,23 +79,12 @@ def _load_defaults():
7779
return {}
7880

7981

80-
def get_filename(pset, photo, suffix):
81-
"""
82-
Create the filename to use for a given photo.
83-
84-
@param pset: Flickr.Photoset, the photoset
85-
@param photo: Flickr.Photo, the photo
86-
@param suffice: str, optional suffix
87-
@return: str, the filename
88-
"""
89-
return '{0}{1}.jpg'.format(photo.title, suffix)
90-
91-
92-
def download_set(set_id, size_label=None):
82+
def download_set(set_id, get_filename, size_label=None):
9383
"""
9484
Download the set with 'set_id' to the current directory.
9585
9686
@param set_id: str, id of the photo set
87+
@param get_filename: Function, function that creates a filename for the photo
9788
@param size_label: str|None, size to download (or None for largest available)
9889
"""
9990
suffix = " ({})".format(size_label) if size_label else ""
@@ -157,6 +148,8 @@ def main():
157148
help='Download the given set')
158149
parser.add_argument('-q', '--quality', type=str, metavar='SIZE_LABEL',
159150
default=None, help='Quality of the picture')
151+
parser.add_argument('-n', '--naming', type=str, metavar='NAMING_MODE',
152+
default='title', help='Photo naming mode')
160153
parser.set_defaults(**_load_defaults())
161154

162155
args = parser.parse_args()
@@ -173,7 +166,8 @@ def main():
173166
print_sets(args.list)
174167
elif args.download:
175168
try:
176-
download_set(args.download, args.quality)
169+
get_filename = get_filename_handler(args.naming)
170+
download_set(args.download, get_filename, args.quality)
177171
except KeyboardInterrupt:
178172
print >> sys.stderr, 'Forcefully aborting. Last photo download might be partial :('
179173
else:

0 commit comments

Comments
 (0)