Skip to content

Commit

Permalink
[flickr] update
Browse files Browse the repository at this point in the history
- ensure every photo has an 'owner' (#828)
- change default directories to a more consistent schema
- create directory for each photo
  • Loading branch information
mikf committed Nov 15, 2020
1 parent 9b1bd09 commit ca44111
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions gallery_dl/extractor/flickr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.

"""Extract images from https://www.flickr.com/"""
"""Extractors for https://www.flickr.com/"""

from .common import Extractor, Message
from .. import text, oauth, util, exception
Expand All @@ -16,6 +16,8 @@ class FlickrExtractor(Extractor):
"""Base class for flickr extractors"""
category = "flickr"
filename_fmt = "{category}_{id}.{extension}"
directory_fmt = ("{category}", "{user[username]}")
archive_fmt = "{id}"
cookiedomain = None

def __init__(self, match):
Expand All @@ -27,8 +29,6 @@ def __init__(self, match):
def items(self):
data = self.metadata()
extract = self.api._extract_format
yield Message.Version, 1
yield Message.Directory, data
for photo in self.photos():
try:
photo = extract(photo)
Expand All @@ -39,6 +39,7 @@ def items(self):
else:
photo.update(data)
url = photo["url"]
yield Message.Directory, photo
yield Message.Url, url, text.nameext_from_url(url, photo)

def metadata(self):
Expand All @@ -53,7 +54,6 @@ def photos(self):
class FlickrImageExtractor(FlickrExtractor):
"""Extractor for individual images from flickr.com"""
subcategory = "image"
archive_fmt = "{id}"
pattern = (r"(?:https?://)?(?:"
r"(?:(?:www\.|m\.)?flickr\.com/photos/[^/]+/"
r"|[^.]+\.static\.?flickr\.com/(?:\d+/)+)(\d+)"
Expand Down Expand Up @@ -106,6 +106,7 @@ def items(self):
else:
self.api._extract_photo(photo)

photo["user"] = photo["owner"]
photo["title"] = photo["title"]["_content"]
photo["comments"] = text.parse_int(photo["comments"]["_content"])
photo["description"] = photo["description"]["_content"]
Expand All @@ -121,16 +122,15 @@ def items(self):
location[key] = value["_content"]

url = photo["url"]
yield Message.Version, 1
yield Message.Directory, photo
yield Message.Url, url, text.nameext_from_url(url, photo)


class FlickrAlbumExtractor(FlickrExtractor):
"""Extractor for photo albums from flickr.com"""
subcategory = "album"
directory_fmt = ("{category}", "{subcategory}s",
"{album[id]} - {album[title]}")
directory_fmt = ("{category}", "{user[username]}",
"Albums", "{album[id]} {album[title]}")
archive_fmt = "a_{album[id]}_{id}"
pattern = (r"(?:https?://)?(?:www\.)?flickr\.com/"
r"photos/([^/]+)/(?:album|set)s(?:/(\d+))?")
Expand Down Expand Up @@ -178,8 +178,8 @@ def photos(self):
class FlickrGalleryExtractor(FlickrExtractor):
"""Extractor for photo galleries from flickr.com"""
subcategory = "gallery"
directory_fmt = ("{category}", "galleries",
"{user[username]} {gallery[id]}")
directory_fmt = ("{category}", "{user[username]}",
"Galleries", "{gallery[gallery_id]} {gallery[title]}")
archive_fmt = "g_{gallery[id]}_{id}"
pattern = (r"(?:https?://)?(?:www\.)?flickr\.com/"
r"photos/([^/]+)/galleries/(\d+)")
Expand All @@ -205,7 +205,7 @@ def photos(self):
class FlickrGroupExtractor(FlickrExtractor):
"""Extractor for group pools from flickr.com"""
subcategory = "group"
directory_fmt = ("{category}", "{subcategory}s", "{group[groupname]}")
directory_fmt = ("{category}", "Groups", "{group[groupname]}")
archive_fmt = "G_{group[nsid]}_{id}"
pattern = r"(?:https?://)?(?:www\.)?flickr\.com/groups/([^/]+)"
test = ("https://www.flickr.com/groups/bird_headshots/", {
Expand All @@ -224,7 +224,6 @@ def photos(self):
class FlickrUserExtractor(FlickrExtractor):
"""Extractor for the photostream of a flickr user"""
subcategory = "user"
directory_fmt = ("{category}", "{user[username]}")
archive_fmt = "u_{user[nsid]}_{id}"
pattern = r"(?:https?://)?(?:www\.)?flickr\.com/photos/([^/]+)/?$"
test = ("https://www.flickr.com/photos/shona_s/", {
Expand All @@ -239,7 +238,7 @@ def photos(self):
class FlickrFavoriteExtractor(FlickrExtractor):
"""Extractor for favorite photos of a flickr user"""
subcategory = "favorite"
directory_fmt = ("{category}", "{subcategory}s", "{user[username]}")
directory_fmt = ("{category}", "{user[username]}", "Favorites")
archive_fmt = "f_{user[nsid]}_{id}"
pattern = r"(?:https?://)?(?:www\.)?flickr\.com/photos/([^/]+)/favorites"
test = ("https://www.flickr.com/photos/shona_s/favorites", {
Expand All @@ -254,7 +253,7 @@ def photos(self):
class FlickrSearchExtractor(FlickrExtractor):
"""Extractor for flickr photos based on search results"""
subcategory = "search"
directory_fmt = ("{category}", "{subcategory}", "{search[text]}")
directory_fmt = ("{category}", "Search", "{search[text]}")
archive_fmt = "s_{search}_{id}"
pattern = r"(?:https?://)?(?:www\.)?flickr\.com/search/?\?([^#]+)"
test = (
Expand Down Expand Up @@ -408,9 +407,11 @@ def urls_lookupUser(self, username):
"""Returns a user NSID, given the url to a user's photos or profile."""
params = {"url": "https://www.flickr.com/photos/" + username}
user = self._call("urls.lookupUser", params)["user"]
return {"nsid": user["id"],
"path_alias": username,
"username": user["username"]["_content"]}
return {
"nsid" : user["id"],
"username" : user["username"]["_content"],
"path_alias": username,
}

def video_getStreamInfo(self, video_id, secret=None):
"""Returns all available video streams"""
Expand Down Expand Up @@ -441,7 +442,8 @@ def _call(self, method, params):
return data

def _pagination(self, method, params, key="photos"):
params["extras"] = "description,date_upload,tags,views,media,"
params["extras"] = ("description,date_upload,tags,views,media,"
"path_alias,owner_name,")
params["extras"] += ",".join("url_" + fmt[0] for fmt in self.formats)
params["page"] = 1

Expand Down Expand Up @@ -469,6 +471,17 @@ def _extract_format(self, photo):
photo["tags"] = photo["tags"].split()
photo["id"] = text.parse_int(photo["id"])

if "owner" in photo:
photo["owner"] = {
"nsid" : photo["owner"],
"username" : photo["ownername"],
"path_alias": photo["pathalias"],
}
else:
photo["owner"] = self.extractor.user
del photo["pathalias"]
del photo["ownername"]

if photo["media"] == "video" and self.videos:
return self._extract_video(photo)

Expand Down

0 comments on commit ca44111

Please sign in to comment.