Path: blob/main/onlyfans_scraper/api/highlights.py
961 views
r"""1_ __2___ _ __ | | _ _ / _| __ _ _ __ ___ ___ ___ _ __ __ _ _ __ ___ _ __3/ _ \ | '_ \ | || | | || |_ / _` || '_ \ / __| _____ / __| / __|| '__| / _` || '_ \ / _ \| '__|4| (_) || | | || || |_| || _|| (_| || | | |\__ \|_____|\__ \| (__ | | | (_| || |_) || __/| |5\___/ |_| |_||_| \__, ||_| \__,_||_| |_||___/ |___/ \___||_| \__,_|| .__/ \___||_|6|___/ |_|7"""89import asyncio10from itertools import chain1112import httpx1314from ..constants import highlightsWithStoriesEP, highlightsWithAStoryEP, storyEP15from ..utils import auth161718def scrape_highlights(headers, user_id) -> list:19with httpx.Client(http2=True, headers=headers) as c:20url_stories = highlightsWithStoriesEP.format(user_id)21url_story = highlightsWithAStoryEP.format(user_id)2223auth.add_cookies(c)24c.headers.update(auth.create_sign(url_stories, headers))25r_multiple = c.get(url_stories, timeout=None)2627c.headers.update(auth.create_sign(url_story, headers))28r_one = c.get(url_story, timeout=None)2930if not r_multiple.is_error and not r_one.is_error:31return r_multiple.json(), r_one.json()3233r_multiple.raise_for_status()34r_one.raise_for_status()3536373839def parse_highlights(highlights: list) -> list:40#This needs further work but will work for now. I was thinking of adding full recurssive ability until all conditions are met.41#This means that whenever onlyfans changes the name of the list containing the highlights it wont matter because the name is variable.42#To break this they would have to change the conditions or in this release the layers.43for item in highlights:44if isinstance(highlights[item],list):45for highlight in highlights[item]:46if 'id' in highlight:47if isinstance(highlight['id'],int):48ids_location = highlights[item]4950if 'hasMore' in highlights:51if not highlights['hasMore']:52return []53else:54print('HasMore error with highlights.')55input("Press Enter to continue.")56return[]57try:58# highlight_ids = [highlight['id'] for highlight in ids_location]59#highlight_ids = [highlight['id'] for highlight in highlights['list']]60return [highlight['id'] for highlight in ids_location]61except Exception as e:62print("{} \n \n \n The above exception was encountered while trying to save highlights.".format(e))63input("Press Enter to continue.")64return[]6566676869async def process_highlights_ids(headers, ids: list) -> list:70if not ids:71return []7273tasks = [scrape_story(headers, id_) for id_ in ids]74results = await asyncio.gather(*tasks)75return list(chain.from_iterable(results))767778async def scrape_story(headers, story_id: int) -> list:79async with httpx.AsyncClient(http2=True, headers=headers) as c:80url = storyEP.format(story_id)8182auth.add_cookies(c)83c.headers.update(auth.create_sign(url, headers))8485r = await c.get(url, timeout=None)86if not r.is_error:87return r.json()['stories']88r.raise_for_status()899091def parse_stories(stories: list):92media = [story['media'] for story in stories]93urls = [(i['files']['source']['url'], i['createdAt'], i['id'], i['type'])94for m in media for i in m if i['canView']]95return urls969798