Skip to content

Commit

Permalink
[flickr] add 'width-max' option (#16)
Browse files Browse the repository at this point in the history
This option allows for simple format selection by
specifying a maximum image width.
  • Loading branch information
mikf committed Jun 18, 2017
1 parent 48b444e commit e68af4f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
38 changes: 32 additions & 6 deletions gallery_dl/extractor/flickr.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,17 @@ class FlickrAPI():
API_URL = "https://api.flickr.com/services/rest/"
API_KEY = "ac4fd7aa98585b9eee1ba761c209de68"
API_SECRET = "3adb0f568dc68393"
FORMATS = [("o", "Original"), ("k", "Large 2048"),
("h", "Large 1600"), ("l", "Large")]
FORMATS = [
("o", "Original", None),
("k", "Large 2048", 2048),
("h", "Large 1600", 1600),
("l", "Large", 1024),
("c", "Medium 800", 800),
("z", "Medium 640", 640),
("m", "Medium", 500),
("n", "Small 320", 320),
("s", "Small", 240),
]

def __init__(self, extractor):
token = extractor.config("access-token")
Expand All @@ -255,6 +264,14 @@ def __init__(self, extractor):
self.API_KEY = None
else:
self.session = extractor.session

self.maxwidth = extractor.config("width-max")
if self.maxwidth:
self.formats = [fmt for fmt in self.FORMATS
if not fmt[2] or fmt[2] < self.maxwidth]
else:
self.formats = self.FORMATS
self.formats = self.formats[:4]
self.subcategory = extractor.subcategory

def favorites_getList(self, user_id):
Expand Down Expand Up @@ -295,7 +312,13 @@ def photos_getInfo(self, photo_id):
def photos_getSizes(self, photo_id):
"""Returns the available sizes for a photo."""
params = {"photo_id": photo_id}
return self._call("photos.getSizes", params)["sizes"]["size"]
sizes = self._call("photos.getSizes", params)["sizes"]["size"]
if self.maxwidth:
for index, size in enumerate(sizes):
if int(size["width"]) > self.maxwidth and index > 0:
del sizes[index:]
break
return sizes

def photos_search(self, params):
"""Return a list of photos matching some criteria."""
Expand Down Expand Up @@ -334,7 +357,7 @@ def _call(self, method, params):
return data

def _pagination(self, method, params):
params["extras"] = "url_o,url_k,url_h,url_l"
params["extras"] = ",".join("url_" + fmt[0] for fmt in self.formats)
params["page"] = 1

while True:
Expand All @@ -361,13 +384,16 @@ def _listing(self, method, params):
yield photo

def _extract_format(self, photo):
for fmt, fmtname in self.FORMATS:
for fmt, fmtname, fmtwidth in self.formats:
key = "url_" + fmt
if key in photo:
width = photo["width_" + fmt]
if self.maxwidth and int(width) > self.maxwidth:
continue
# generate photo info
photo["photo"] = {
"source": photo[key],
"width" : photo["width_" + fmt],
"width" : width,
"height": photo["height_" + fmt],
"label" : fmtname,
"media" : "photo",
Expand Down
2 changes: 1 addition & 1 deletion test/test_extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test(self):
# dont work on travis-ci
"exhentai", "kissmanga", "mangafox", "dynastyscans", "nijie",
# temporary issues
"imagefap"
"hbrowse",
]
# enable selective testing for direct calls
if __name__ == '__main__' and len(sys.argv) > 1:
Expand Down

0 comments on commit e68af4f

Please sign in to comment.