| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 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, {} |