#! /usr/bin/env python import sys sys.path.insert(0, '.') from datetime import datetime import time import requests from tracker.sheet import Sheet, safe_value # Google oauth stuff is complicated, and sometimes fails due to libs # being out of date, this checks, using the docker image, that things are working. GOOGLE_SHEET_URL = 'https://docs.google.com/spreadsheets/d/1GuMmb9KmR9l1oWbE8cyGxW5qwzahhweDzIcTfKnvtJY' # powfu GOOGLE_SHEET_URL = 'https://docs.google.com/spreadsheets/d/1wwc7XFRc1q8YvxyBKwFywxZC2IpRqPt44jFBBOPY_LU' def write_to_sheet(sheet_name: str, values=[]): try: print("Instantiating Sheet") sheet = Sheet() print("Opening workbook") ss = sheet.client.open_by_url(GOOGLE_SHEET_URL) ws = ss.worksheet(sheet_name) ws.append_row([safe_value(datetime.utcnow())] + values) except Exception as e: print("Something went wrong.", e) raise e import json from bs4 import BeautifulSoup def get_toks(url, sheet_name): print(f"Starting import {url}") req = requests.get(url, headers={'User-Agent': 'chrome'}) try: if url not in req.url: raise RuntimeError(f"Not on expected page, url {req.url}, {req.status_code}, {req.reason}") soup = BeautifulSoup(req.content, 'html5lib') num_toks = int(next(filter(None, [json.loads(d.text).get('props',{}).get('pageProps') for d in soup.select('script[type*="json"]')])).get('musicData', {}).get('posts')) except Exception as e: print(f"Failed to get number of tiktoks {e}") import traceback traceback.print_exc() else: print(f"Found {num_toks} tiktoks") write_to_sheet(sheet_name, [num_toks]) def get_yt_views(ytid, sheet_name): print(f"Starting import {ytid}") try: from tracker.youtube import youtube results = youtube._fetch_video_stats(video_ids=[ytid]) stat_item = results['items'][0] stats = [ stat_item["statistics"][field] for field in ['viewCount', 'likeCount', 'dislikeCount', 'commentCount'] ] print(f"Found {stats} for {ytid}") write_to_sheet(sheet_name, stats) except Exception as e: import traceback traceback.print_exc() def get_soundcloud_views(scid, sheet_name): print(f"Starting sc import {scid} / {sheet_name}") from tracker import soundcloud try: data = list( soundcloud.client.get("/tracks", limit=1, ids=str(scid)) )[0] stats = [ data.playback_count, data.comment_count, data.favoritings_count, ] print(f"Found {stats} for {scid}") write_to_sheet(sheet_name, stats) except Exception as e: import traceback traceback.print_exc() if __name__ == '__main__': while True: get_soundcloud_views(728372680, 'Big Drippa SC') get_toks('https://www.tiktok.com/music/death-bed-6703427142346083074', 'death bed tiktoks') get_yt_views('GgCNdUmsDYE', 'powfu death bed') # get_yt_views('I6nqiTIKdL4', 'powfu death bed lyrics') get_yt_views('e0erorjT6Eo', 'e0erorjT6Eo') print("Sleeping for an hour.") time.sleep(3600-5)