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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
|
# -*- coding: utf-8 -*-
# Copyright 2018-2025 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://www.behance.net/"""
from .common import Extractor, Message
from .. import text, util, exception
class BehanceExtractor(Extractor):
"""Base class for behance extractors"""
category = "behance"
root = "https://www.behance.net"
request_interval = (2.0, 4.0)
browser = "firefox"
tls12 = False
def _init(self):
self._bcp = self.cookies.get("bcp", domain="www.behance.net")
if not self._bcp:
self._bcp = "4c34489d-914c-46cd-b44c-dfd0e661136d"
self.cookies.set("bcp", self._bcp, domain="www.behance.net")
def items(self):
for gallery in self.galleries():
gallery["_extractor"] = BehanceGalleryExtractor
yield Message.Queue, gallery["url"], self._update(gallery)
def galleries(self):
"""Return all relevant gallery URLs"""
def _request_graphql(self, endpoint, variables):
url = self.root + "/v3/graphql"
headers = {
"Origin": self.root,
"X-BCP" : self._bcp,
"X-Requested-With": "XMLHttpRequest",
}
data = {
"query" : GRAPHQL_QUERIES[endpoint],
"variables": variables,
}
return self.request_json(
url, method="POST", headers=headers, json=data)["data"]
def _update(self, data):
# compress data to simple lists
if (fields := data.get("fields")) and isinstance(fields[0], dict):
data["fields"] = [
field.get("name") or field.get("label")
for field in fields
]
data["owners"] = [
owner.get("display_name") or owner.get("displayName")
for owner in data["owners"]
]
tags = data.get("tags") or ()
if tags and isinstance(tags[0], dict):
tags = [tag["title"] for tag in tags]
data["tags"] = tags
data["date"] = text.parse_timestamp(
data.get("publishedOn") or data.get("conceived_on") or 0)
if creator := data.get("creator"):
creator["name"] = creator["url"].rpartition("/")[2]
# backwards compatibility
data["gallery_id"] = data["id"]
data["title"] = data["name"]
data["user"] = ", ".join(data["owners"])
return data
class BehanceGalleryExtractor(BehanceExtractor):
"""Extractor for image galleries from www.behance.net"""
subcategory = "gallery"
directory_fmt = ("{category}", "{owners:J, }", "{id} {name}")
filename_fmt = "{category}_{id}_{num:>02}.{extension}"
archive_fmt = "{id}_{num}"
pattern = r"(?:https?://)?(?:www\.)?behance\.net/gallery/(\d+)"
example = "https://www.behance.net/gallery/12345/TITLE"
def __init__(self, match):
BehanceExtractor.__init__(self, match)
self.gallery_id = match[1]
def _init(self):
BehanceExtractor._init(self)
if modules := self.config("modules"):
if isinstance(modules, str):
modules = modules.split(",")
self.modules = set(modules)
else:
self.modules = {"image", "video", "mediacollection", "embed"}
def items(self):
data = self.get_gallery_data()
imgs = self.get_images(data)
data["count"] = len(imgs)
yield Message.Directory, data
for data["num"], (url, module) in enumerate(imgs, 1):
data["module"] = module
data["extension"] = (module.get("extension") or
text.ext_from_url(url))
yield Message.Url, url, data
def get_gallery_data(self):
"""Collect gallery info dict"""
url = f"{self.root}/gallery/{self.gallery_id}/a"
cookies = {
"gk_suid": "14118261",
"gki": "feature_3_in_1_checkout_test:false,hire_browse_get_quote_c"
"ta_ab_test:false,feature_hire_dashboard_services_ab_test:f"
"alse,feature_show_details_jobs_row_ab_test:false,feature_a"
"i_freelance_project_create_flow:false,",
"ilo0": "true",
"originalReferrer": "",
}
page = self.request(url, cookies=cookies).text
data = util.json_loads(text.extr(
page, 'id="beconfig-store_state">', '</script>'))
return self._update(data["project"]["project"])
def get_images(self, data):
"""Extract image results from an API response"""
if not data["modules"]:
access = data.get("matureAccess")
if access == "logged-out":
raise exception.AuthorizationError(
"Mature content galleries require logged-in cookies")
if access == "restricted-safe":
raise exception.AuthorizationError(
"Mature content blocked in account settings")
if access and access != "allowed":
raise exception.AuthorizationError()
return ()
results = []
for module in data["modules"]:
mtype = module["__typename"][:-6].lower()
if mtype not in self.modules:
self.log.debug("Skipping '%s' module", mtype)
continue
if mtype == "image":
sizes = {
size["url"].rsplit("/", 2)[1]: size
for size in module["imageSizes"]["allAvailable"]
}
size = (sizes.get("source") or
sizes.get("max_3840") or
sizes.get("fs") or
sizes.get("hd") or
sizes.get("disp"))
results.append((size["url"], module))
elif mtype == "video":
try:
url = text.extr(module["embed"], 'src="', '"')
page = self.request(text.unescape(url)).text
url = text.extr(page, '<source src="', '"')
if text.ext_from_url(url) == "m3u8":
url = "ytdl:" + url
module["_ytdl_manifest"] = "hls"
module["extension"] = "mp4"
results.append((url, module))
continue
except Exception as exc:
self.log.debug("%s: %s", exc.__class__.__name__, exc)
try:
renditions = module["videoData"]["renditions"]
except Exception:
self.log.warning("No download URLs for video %s",
module.get("id") or "???")
continue
try:
url = [
r["url"] for r in renditions
if text.ext_from_url(r["url"]) != "m3u8"
][-1]
except Exception as exc:
self.log.debug("%s: %s", exc.__class__.__name__, exc)
url = "ytdl:" + renditions[-1]["url"]
results.append((url, module))
elif mtype == "mediacollection":
for component in module["components"]:
for size in component["imageSizes"].values():
if size:
parts = size["url"].split("/")
parts[4] = "source"
results.append(("/".join(parts), module))
break
elif mtype == "embed":
if embed := (module.get("originalEmbed") or
module.get("fluidEmbed")):
embed = text.unescape(text.extr(embed, 'src="', '"'))
module["extension"] = "mp4"
results.append(("ytdl:" + embed, module))
elif mtype == "text":
module["extension"] = "txt"
results.append(("text:" + module["text"], module))
return results
class BehanceUserExtractor(BehanceExtractor):
"""Extractor for a user's galleries from www.behance.net"""
subcategory = "user"
categorytransfer = True
pattern = r"(?:https?://)?(?:www\.)?behance\.net/([^/?#]+)/?$"
example = "https://www.behance.net/USER"
def __init__(self, match):
BehanceExtractor.__init__(self, match)
self.user = match[1]
def galleries(self):
endpoint = "GetProfileProjects"
variables = {
"username": self.user,
"after" : "MAo=", # "0" in base64
}
while True:
data = self._request_graphql(endpoint, variables)
items = data["user"]["profileProjects"]
yield from items["nodes"]
if not items["pageInfo"]["hasNextPage"]:
return
variables["after"] = items["pageInfo"]["endCursor"]
class BehanceCollectionExtractor(BehanceExtractor):
"""Extractor for a collection's galleries from www.behance.net"""
subcategory = "collection"
categorytransfer = True
pattern = r"(?:https?://)?(?:www\.)?behance\.net/collection/(\d+)"
example = "https://www.behance.net/collection/12345/TITLE"
def __init__(self, match):
BehanceExtractor.__init__(self, match)
self.collection_id = match[1]
def galleries(self):
endpoint = "GetMoodboardItemsAndRecommendations"
variables = {
"afterItem": "MAo=", # "0" in base64
"firstItem": 40,
"id" : int(self.collection_id),
"shouldGetItems" : True,
"shouldGetMoodboardFields": False,
"shouldGetRecommendations": False,
}
while True:
data = self._request_graphql(endpoint, variables)
items = data["moodboard"]["items"]
for node in items["nodes"]:
yield node["entity"]
if not items["pageInfo"]["hasNextPage"]:
return
variables["afterItem"] = items["pageInfo"]["endCursor"]
GRAPHQL_QUERIES = {
"GetProfileProjects": """\
query GetProfileProjects($username: String, $after: String) {
user(username: $username) {
profileProjects(first: 12, after: $after) {
pageInfo {
endCursor
hasNextPage
}
nodes {
__typename
adminFlags {
mature_lock
privacy_lock
dmca_lock
flagged_lock
privacy_violation_lock
trademark_lock
spam_lock
eu_ip_lock
}
colors {
r
g
b
}
covers {
size_202 {
url
}
size_404 {
url
}
size_808 {
url
}
}
features {
url
name
featuredOn
ribbon {
image
image2x
image3x
}
}
fields {
id
label
slug
url
}
hasMatureContent
id
isFeatured
isHiddenFromWorkTab
isMatureReviewSubmitted
isOwner
isFounder
isPinnedToSubscriptionOverview
isPrivate
linkedAssets {
...sourceLinkFields
}
linkedAssetsCount
sourceFiles {
...sourceFileFields
}
matureAccess
modifiedOn
name
owners {
...OwnerFields
images {
size_50 {
url
}
}
}
premium
publishedOn
stats {
appreciations {
all
}
views {
all
}
comments {
all
}
}
slug
tools {
id
title
category
categoryLabel
categoryId
approved
url
backgroundColor
}
url
}
}
}
}
fragment sourceFileFields on SourceFile {
__typename
sourceFileId
projectId
userId
title
assetId
renditionUrl
mimeType
size
category
licenseType
unitAmount
currency
tier
hidden
extension
hasUserPurchased
}
fragment sourceLinkFields on LinkedAsset {
__typename
name
premium
url
category
licenseType
}
fragment OwnerFields on User {
displayName
hasPremiumAccess
id
isFollowing
isProfileOwner
location
locationUrl
url
username
availabilityInfo {
availabilityTimeline
isAvailableFullTime
isAvailableFreelance
}
}
""",
"GetMoodboardItemsAndRecommendations": """\
query GetMoodboardItemsAndRecommendations(
$id: Int!
$firstItem: Int!
$afterItem: String
$shouldGetRecommendations: Boolean!
$shouldGetItems: Boolean!
$shouldGetMoodboardFields: Boolean!
) {
viewer @include(if: $shouldGetMoodboardFields) {
isOptedOutOfRecommendations
isAdmin
}
moodboard(id: $id) {
...moodboardFields @include(if: $shouldGetMoodboardFields)
items(first: $firstItem, after: $afterItem) @include(if: $shouldGetItems) {
pageInfo {
endCursor
hasNextPage
}
nodes {
...nodesFields
}
}
recommendedItems(first: 80) @include(if: $shouldGetRecommendations) {
nodes {
...nodesFields
fetchSource
}
}
}
}
fragment moodboardFields on Moodboard {
id
label
privacy
followerCount
isFollowing
projectCount
url
isOwner
owners {
...OwnerFields
images {
size_50 {
url
}
size_100 {
url
}
size_115 {
url
}
size_230 {
url
}
size_138 {
url
}
size_276 {
url
}
}
}
}
fragment projectFields on Project {
__typename
id
isOwner
publishedOn
matureAccess
hasMatureContent
modifiedOn
name
url
isPrivate
slug
license {
license
description
id
label
url
text
images
}
fields {
label
}
colors {
r
g
b
}
owners {
...OwnerFields
images {
size_50 {
url
}
size_100 {
url
}
size_115 {
url
}
size_230 {
url
}
size_138 {
url
}
size_276 {
url
}
}
}
covers {
size_original {
url
}
size_max_808 {
url
}
size_808 {
url
}
size_404 {
url
}
size_202 {
url
}
size_230 {
url
}
size_115 {
url
}
}
stats {
views {
all
}
appreciations {
all
}
comments {
all
}
}
}
fragment exifDataValueFields on exifDataValue {
id
label
value
searchValue
}
fragment nodesFields on MoodboardItem {
id
entityType
width
height
flexWidth
flexHeight
images {
size
url
}
entity {
... on Project {
...projectFields
}
... on ImageModule {
project {
...projectFields
}
colors {
r
g
b
}
exifData {
lens {
...exifDataValueFields
}
software {
...exifDataValueFields
}
makeAndModel {
...exifDataValueFields
}
focalLength {
...exifDataValueFields
}
iso {
...exifDataValueFields
}
location {
...exifDataValueFields
}
flash {
...exifDataValueFields
}
exposureMode {
...exifDataValueFields
}
shutterSpeed {
...exifDataValueFields
}
aperture {
...exifDataValueFields
}
}
}
... on MediaCollectionComponent {
project {
...projectFields
}
}
}
}
fragment OwnerFields on User {
displayName
hasPremiumAccess
id
isFollowing
isProfileOwner
location
locationUrl
url
username
availabilityInfo {
availabilityTimeline
isAvailableFullTime
isAvailableFreelance
}
}
""",
}
|