Debian Codegraph @

gallery_dl/extractor/urlshortener.py

raw
1 # -*- coding: utf-8 -*-
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License version 2 as
5 # published by the Free Software Foundation.
6
7 """Extractors for general-purpose URL shorteners"""
8
9 from .common import BaseExtractor, Message
10
11
12 class UrlshortenerExtractor(BaseExtractor):
13 """Base class for URL shortener extractors"""
14 basecategory = "urlshortener"
15
16
17 BASE_PATTERN = UrlshortenerExtractor.update({
18 "bitly": {
19 "root": "https://bit.ly",
20 "pattern": r"bit\.ly",
21 },
22 "tco": {
23 # t.co sends 'http-equiv="refresh"' (200) when using browser UA
24 "headers": {"User-Agent": None},
25 "root": "https://t.co",
26 "pattern": r"t\.co",
27 },
28 })
29
30
31 class UrlshortenerLinkExtractor(UrlshortenerExtractor):
32 """Extractor for general-purpose URL shorteners"""
33 subcategory = "link"
34 pattern = BASE_PATTERN + r"(/[^/?#]+)"
35 example = "https://bit.ly/abcde"
36
37 def items(self):
38 url = self.root + self.groups[-1]
39 location = self.request_location(
40 url, headers=self.config_instance("headers"), notfound="URL")
41 if not location:
42 raise self.exc.AbortExtraction("Unable to resolve short URL")
43 yield Message.Queue, location, {}