Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
taux1c
GitHub Repository: taux1c/onlyfans-scraper
Path: blob/main/onlyfans_scraper/api/highlights.py
961 views
1
r"""
2
_ __
3
___ _ __ | | _ _ / _| __ _ _ __ ___ ___ ___ _ __ __ _ _ __ ___ _ __
4
/ _ \ | '_ \ | || | | || |_ / _` || '_ \ / __| _____ / __| / __|| '__| / _` || '_ \ / _ \| '__|
5
| (_) || | | || || |_| || _|| (_| || | | |\__ \|_____|\__ \| (__ | | | (_| || |_) || __/| |
6
\___/ |_| |_||_| \__, ||_| \__,_||_| |_||___/ |___/ \___||_| \__,_|| .__/ \___||_|
7
|___/ |_|
8
"""
9
10
import asyncio
11
from itertools import chain
12
13
import httpx
14
15
from ..constants import highlightsWithStoriesEP, highlightsWithAStoryEP, storyEP
16
from ..utils import auth
17
18
19
def scrape_highlights(headers, user_id) -> list:
20
with httpx.Client(http2=True, headers=headers) as c:
21
url_stories = highlightsWithStoriesEP.format(user_id)
22
url_story = highlightsWithAStoryEP.format(user_id)
23
24
auth.add_cookies(c)
25
c.headers.update(auth.create_sign(url_stories, headers))
26
r_multiple = c.get(url_stories, timeout=None)
27
28
c.headers.update(auth.create_sign(url_story, headers))
29
r_one = c.get(url_story, timeout=None)
30
31
if not r_multiple.is_error and not r_one.is_error:
32
return r_multiple.json(), r_one.json()
33
34
r_multiple.raise_for_status()
35
r_one.raise_for_status()
36
37
38
39
40
def parse_highlights(highlights: list) -> list:
41
#This needs further work but will work for now. I was thinking of adding full recurssive ability until all conditions are met.
42
#This means that whenever onlyfans changes the name of the list containing the highlights it wont matter because the name is variable.
43
#To break this they would have to change the conditions or in this release the layers.
44
for item in highlights:
45
if isinstance(highlights[item],list):
46
for highlight in highlights[item]:
47
if 'id' in highlight:
48
if isinstance(highlight['id'],int):
49
ids_location = highlights[item]
50
51
if 'hasMore' in highlights:
52
if not highlights['hasMore']:
53
return []
54
else:
55
print('HasMore error with highlights.')
56
input("Press Enter to continue.")
57
return[]
58
try:
59
# highlight_ids = [highlight['id'] for highlight in ids_location]
60
#highlight_ids = [highlight['id'] for highlight in highlights['list']]
61
return [highlight['id'] for highlight in ids_location]
62
except Exception as e:
63
print("{} \n \n \n The above exception was encountered while trying to save highlights.".format(e))
64
input("Press Enter to continue.")
65
return[]
66
67
68
69
70
async def process_highlights_ids(headers, ids: list) -> list:
71
if not ids:
72
return []
73
74
tasks = [scrape_story(headers, id_) for id_ in ids]
75
results = await asyncio.gather(*tasks)
76
return list(chain.from_iterable(results))
77
78
79
async def scrape_story(headers, story_id: int) -> list:
80
async with httpx.AsyncClient(http2=True, headers=headers) as c:
81
url = storyEP.format(story_id)
82
83
auth.add_cookies(c)
84
c.headers.update(auth.create_sign(url, headers))
85
86
r = await c.get(url, timeout=None)
87
if not r.is_error:
88
return r.json()['stories']
89
r.raise_for_status()
90
91
92
def parse_stories(stories: list):
93
media = [story['media'] for story in stories]
94
urls = [(i['files']['source']['url'], i['createdAt'], i['id'], i['type'])
95
for m in media for i in m if i['canView']]
96
return urls
97
98