Skip to content

Commit

Permalink
[tumblr] add 'tumblr:' to force Tumblr extractor (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Jan 15, 2018
1 parent 40d35c8 commit 421a974
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
1 change: 0 additions & 1 deletion gallery_dl/extractor/pinterest.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ class PinterestBoardExtractor(PinterestExtractor):
("https://www.pinterest.com/g1952849/test-/", {
"url": "705ee521630a5d613b0449d694a5345e684572a9",
"keyword": "1650dd31c4dedd940cef399135e485400625ec0b",
"content": "30897fb5d5616765bb2c9c26cb84f54499424fb4",
}),
("https://www.pinterest.com/g1952848/test/", {
"exception": exception.NotFoundError,
Expand Down
32 changes: 19 additions & 13 deletions gallery_dl/extractor/tumblr.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def _original_video(url):
POST_TYPES = frozenset((
"text", "quote", "link", "answer", "video", "audio", "photo", "chat"))

BASE_PATTERN = (
r"(?:tumblr:(?:https?://)?([^/]+)|"
r"(?:https?://)?([^.]+\.tumblr\.com))")


class TumblrExtractor(Extractor):
"""Base class for tumblr extractors"""
Expand All @@ -44,7 +48,7 @@ class TumblrExtractor(Extractor):

def __init__(self, match):
Extractor.__init__(self)
self.user = match.group(1)
self.blog = match.group(1) or match.group(2)
self.api = TumblrAPI(self)

self.types = self._setup_posttypes()
Expand All @@ -58,7 +62,7 @@ def __init__(self, match):
self.log.warning("no valid post types selected")

def items(self):
blog = self.api.info(self.user)
blog = self.api.info(self.blog)
yield Message.Version, 1
yield Message.Directory, blog

Expand Down Expand Up @@ -139,7 +143,7 @@ def _prepare(url, post):
class TumblrUserExtractor(TumblrExtractor):
"""Extractor for all images from a tumblr-user"""
subcategory = "user"
pattern = [r"(?:https?://)?([^.]+)\.tumblr\.com(?:/page/\d+)?/?$"]
pattern = [BASE_PATTERN + r"(?:/page/\d+)?/?$"]
test = [
("http://demo.tumblr.com/", {
"pattern": (r"https?://\d+\.media\.tumblr\.com"
Expand All @@ -154,28 +158,30 @@ class TumblrUserExtractor(TumblrExtractor):
"options": (("posts", "all"), ("external", True),
("inline", True), ("reblogs", True))
}),
("tumblr:http://www.b-authentique.com/", None),
("tumblr:www.b-authentique.com", None),
]

def posts(self):
return self.api.posts(self.user, {})
return self.api.posts(self.blog, {})


class TumblrPostExtractor(TumblrExtractor):
"""Extractor for images from a single post on tumblr"""
subcategory = "post"
pattern = [r"(?:https?://)?([^.]+)\.tumblr\.com/post/(\d+)"]
pattern = [BASE_PATTERN + r"/post/(\d+)"]
test = [("http://demo.tumblr.com/post/459265350", {
"pattern": r"https://\d+\.media\.tumblr\.com/tumblr_[^/_]+_1280.jpg",
"count": 1,
})]

def __init__(self, match):
TumblrExtractor.__init__(self, match)
self.post_id = match.group(2)
self.post_id = match.group(3)
self.reblogs = True

def posts(self):
return self.api.posts(self.user, {"id": self.post_id})
return self.api.posts(self.blog, {"id": self.post_id})

@staticmethod
def _setup_posttypes():
Expand All @@ -185,31 +191,31 @@ def _setup_posttypes():
class TumblrTagExtractor(TumblrExtractor):
"""Extractor for images from a tumblr-user by tag"""
subcategory = "tag"
pattern = [r"(?:https?://)?([^.]+)\.tumblr\.com/tagged/(.+)"]
pattern = [BASE_PATTERN + r"/tagged/([^/?&#]+)"]
test = [("http://demo.tumblr.com/tagged/Times%20Square", {
"pattern": r"https://\d+\.media\.tumblr\.com/tumblr_[^/_]+_1280.jpg",
"count": 1,
})]

def __init__(self, match):
TumblrExtractor.__init__(self, match)
self.tag = text.unquote(match.group(2))
self.tag = text.unquote(match.group(3))

def posts(self):
return self.api.posts(self.user, {"tag": self.tag})
return self.api.posts(self.blog, {"tag": self.tag})


class TumblrLikesExtractor(TumblrExtractor):
"""Extractor for images from a tumblr-user by tag"""
subcategory = "likes"
directory_fmt = ["{category}", "{name}", "likes"]
pattern = [r"(?:https?://)?([^.]+)\.tumblr\.com/likes"]
pattern = [BASE_PATTERN + r"/likes"]
test = [("http://mikf123.tumblr.com/likes", {
"count": 1,
})]

def posts(self):
return self.api.likes(self.user)
return self.api.likes(self.blog)


class TumblrAPI():
Expand Down Expand Up @@ -262,7 +268,7 @@ def likes(self, blog):
def _call(self, blog, endpoint, params):
if self.api_key:
params["api_key"] = self.api_key
url = "https://api.tumblr.com/v2/blog/{}.tumblr.com/{}".format(
url = "https://api.tumblr.com/v2/blog/{}/{}".format(
blog, endpoint)

response = self.session.get(url, params=params).json()
Expand Down

0 comments on commit 421a974

Please sign in to comment.