1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
# -*- coding: utf-8 -*-
# Copyright 2020-2023 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
"""Extractors for https://redgifs.com/"""
from .common import Extractor, Message
from .. import text
from ..cache import memcache
class RedgifsExtractor(Extractor):
"""Base class for redgifs extractors"""
category = "redgifs"
filename_fmt = \
"{category}_{gallery:?//[:11]}{num:?_/_/>02}{id}.{extension}"
archive_fmt = "{id}"
root = "https://www.redgifs.com"
def __init__(self, match):
Extractor.__init__(self, match)
self.key = match.group(1)
def _init(self):
self.api = RedgifsAPI(self)
formats = self.config("format")
if formats is None:
formats = ("hd", "sd", "gif")
elif isinstance(formats, str):
formats = (formats, "hd", "sd", "gif")
self.formats = formats
def items(self):
metadata = self.metadata()
for gif in self.gifs():
gallery = gif.get("gallery")
if gallery:
gifs = self.api.gallery(gallery)["gifs"]
enum = 1
cnt = len(gifs)
else:
gifs = (gif,)
enum = 0
cnt = 1
gif.update(metadata)
gif["count"] = cnt
gif["date"] = text.parse_timestamp(gif.get("createDate"))
yield Message.Directory, gif
for num, gif in enumerate(gifs, enum):
gif["_fallback"] = formats = self._formats(gif)
url = next(formats, None)
if not url:
self.log.warning(
"Skipping '%s' (format not available)", gif["id"])
continue
gif["num"] = num
gif["count"] = cnt
yield Message.Url, url, gif
def _formats(self, gif):
urls = gif["urls"]
for fmt in self.formats:
url = urls.get(fmt)
if url:
url = url.replace("//thumbs2.", "//thumbs3.", 1)
text.nameext_from_url(url, gif)
yield url
def metadata(self):
return {}
def gifs(self):
return ()
class RedgifsUserExtractor(RedgifsExtractor):
"""Extractor for redgifs user profiles"""
subcategory = "user"
directory_fmt = ("{category}", "{userName}")
pattern = (r"(?:https?://)?(?:\w+\.)?redgifs\.com/users/([^/?#]+)/?"
r"(?:\?([^#]+))?$")
example = "https://www.redgifs.com/users/USER"
def __init__(self, match):
RedgifsExtractor.__init__(self, match)
self.query = match.group(2)
def metadata(self):
return {"userName": self.key}
def gifs(self):
order = text.parse_query(self.query).get("order")
return self.api.user(self.key, order or "new")
class RedgifsCollectionExtractor(RedgifsExtractor):
"""Extractor for an individual user collection"""
subcategory = "collection"
directory_fmt = (
"{category}", "{collection[userName]}", "{collection[folderName]}")
archive_fmt = "{collection[folderId]}_{id}"
pattern = (r"(?:https?://)?(?:www\.)?redgifs\.com/users"
r"/([^/?#]+)/collections/([^/?#]+)")
example = "https://www.redgifs.com/users/USER/collections/ID"
def __init__(self, match):
RedgifsExtractor.__init__(self, match)
self.collection_id = match.group(2)
def metadata(self):
collection = self.api.collection_info(self.key, self.collection_id)
collection["userName"] = self.key
return {"collection": collection}
def gifs(self):
return self.api.collection(self.key, self.collection_id)
class RedgifsCollectionsExtractor(RedgifsExtractor):
"""Extractor for redgifs user collections"""
subcategory = "collections"
pattern = (r"(?:https?://)?(?:www\.)?redgifs\.com/users"
r"/([^/?#]+)/collections/?$")
example = "https://www.redgifs.com/users/USER/collections"
def items(self):
for collection in self.api.collections(self.key):
url = "{}/users/{}/collections/{}".format(
self.root, self.key, collection["folderId"])
collection["_extractor"] = RedgifsCollectionExtractor
yield Message.Queue, url, collection
class RedgifsNichesExtractor(RedgifsExtractor):
"""Extractor for redgifs niches"""
subcategory = "niches"
pattern = (r"(?:https?://)?(?:www\.)?redgifs\.com/niches/([^/?#]+)/?"
r"(?:\?([^#]+))?$")
example = "https://www.redgifs.com/niches/NAME"
def __init__(self, match):
RedgifsExtractor.__init__(self, match)
self.query = match.group(2)
def gifs(self):
order = text.parse_query(self.query).get("order")
return self.api.niches(self.key, order or "new")
class RedgifsSearchExtractor(RedgifsExtractor):
"""Extractor for redgifs search results"""
subcategory = "search"
directory_fmt = ("{category}", "Search", "{search}")
pattern = (r"(?:https?://)?(?:\w+\.)?redgifs\.com"
r"/(?:gifs/([^/?#]+)|browse)(?:/?\?([^#]+))?")
example = "https://www.redgifs.com/gifs/TAG"
def __init__(self, match):
RedgifsExtractor.__init__(self, match)
self.search, self.query = match.groups()
def metadata(self):
self.params = text.parse_query(self.query)
if self.search:
self.params["tags"] = text.unquote(self.search)
return {"search": (self.params.get("tags") or
self.params.get("order") or
"trending")}
def gifs(self):
return self.api.search(self.params)
class RedgifsImageExtractor(RedgifsExtractor):
"""Extractor for individual gifs from redgifs.com"""
subcategory = "image"
pattern = (r"(?:https?://)?(?:"
r"(?:\w+\.)?redgifs\.com/(?:watch|ifr)|"
r"(?:\w+\.)?gfycat\.com(?:/gifs/detail|/\w+)?|"
r"(?:www\.)?gifdeliverynetwork\.com|"
r"i\.redgifs\.com/i)/([A-Za-z0-9]+)")
example = "https://redgifs.com/watch/ID"
def gifs(self):
return (self.api.gif(self.key),)
class RedgifsAPI():
"""https://api.redgifs.com/docs/index.html"""
API_ROOT = "https://api.redgifs.com"
def __init__(self, extractor):
self.extractor = extractor
self.headers = {
"authorization" : None,
"content-type" : "application/json",
"x-customheader": extractor.root + "/",
"Origin" : extractor.root,
}
def gif(self, gif_id):
endpoint = "/v2/gifs/" + gif_id.lower()
return self._call(endpoint)["gif"]
def gallery(self, gallery_id):
endpoint = "/v2/gallery/" + gallery_id
return self._call(endpoint)
def user(self, user, order="new"):
endpoint = "/v2/users/{}/search".format(user.lower())
params = {"order": order}
return self._pagination(endpoint, params)
def collection(self, user, collection_id):
endpoint = "/v2/users/{}/collections/{}/gifs".format(
user, collection_id)
return self._pagination(endpoint)
def collection_info(self, user, collection_id):
endpoint = "/v2/users/{}/collections/{}".format(user, collection_id)
return self._call(endpoint)
def collections(self, user):
endpoint = "/v2/users/{}/collections".format(user)
return self._pagination(endpoint, key="collections")
def niches(self, niche, order):
endpoint = "/v2/niches/{}/gifs".format(niche)
params = {"count": 30, "order": order}
return self._pagination(endpoint, params)
def search(self, params):
endpoint = "/v2/gifs/search"
params["search_text"] = params.pop("tags", None)
return self._pagination(endpoint, params)
def _call(self, endpoint, params=None):
url = self.API_ROOT + endpoint
self.headers["authorization"] = self._auth()
return self.extractor.request(
url, params=params, headers=self.headers).json()
def _pagination(self, endpoint, params=None, key="gifs"):
if params is None:
params = {}
params["page"] = 1
while True:
data = self._call(endpoint, params)
yield from data[key]
if params["page"] >= data["pages"]:
return
params["page"] += 1
@memcache(maxage=600)
def _auth(self):
# https://github.com/Redgifs/api/wiki/Temporary-tokens
url = self.API_ROOT + "/v2/auth/temporary"
self.headers["authorization"] = None
return "Bearer " + self.extractor.request(
url, headers=self.headers).json()["token"]
|