Guest User

op-helper.py v0.3 by C-Anon

a guest
Sep 29th, 2022
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.64 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # op-helper.py for making threads on /vt/
  3. # Released under MIT License, software is provided as-is and all that crap.
  4. # v0.3 by C-Anon
  5.  
  6. import re, json, urllib.request
  7. import datetime, time, os
  8.  
  9.  
  10. ##########################
  11. #   CONFIGURATION
  12.  
  13. # Ignore streams if they're set this amount of days from now (or more)
  14. ignore_days = 10
  15.  
  16. # Sort streams by starting timestamp
  17. sort_streams = False
  18.  
  19. # Show stream titles
  20. show_live_titles = False
  21. show_upcoming_titles = False
  22.  
  23. # Show timezones
  24. show_timezones = False
  25.  
  26. # If true, data is output to a file instead of the terminal
  27. output_to_file = True
  28. outfname = 'op-helper.txt'
  29.  
  30. # Channel data
  31. #   The format is channel ID followed by a space, then followed
  32. #   by the name of the channel. Empty lines are ignored
  33. channel_data = """
  34. UC8P3OkWBUsrk93f1pV0b5bQ Ageha Himeragi
  35. UCEnhASxlFG-ZPCpYDTYIQZA Charo Nemurime
  36. UCAx0YWXJgyvXx5oDvrDaN_A Himari Inumaki
  37. UCvm34tgJ0ZaKzTinWVyXpcA Himea D'Almaria
  38. UC6tSB9TnO0f01OBeo9UEJZA Hina Misora
  39. UCVtuciDzkjxCP_O7r-0QhXQ Ito Shinonome
  40. UCJePO0Zl-zZTqjpHO82RNNA Lia Mitsurugi
  41. UCN3mosAMYBdogyQovOhPrxA Luna Rurine
  42. UClXfBZMVxt-JgNMaHGr5NMQ Mahiru Kumaboshi
  43. UCUUjykb68Lf85CbWX6gAmog Mireille Kuuma
  44. UCM6iy_rSgSMbFjx10Z6VVGA Miu Hizuki
  45. UCIm8pnnTNhCgGAtNxrQQv-g Yue Saohime
  46. """
  47.  
  48. # Timezone data
  49. #   You can specify timezones to display along with the other information.
  50. #   The format is the name of the timezone followed by a space, then followed
  51. #   by the number of hours from UTC (e.g. Mexico City timezone is GMT-5 in summer)
  52. #
  53. #   This might need to be adjusted as countries adopt and abandon daylight savings time
  54. #   throughout the year.
  55. timezone_data = """
  56. MEX -5
  57. ARG -3
  58. ESP 2
  59. JAP 9
  60. """
  61.  
  62. #   END OF CONFIGURATION
  63. ##########################
  64.  
  65. now = time.time
  66.  
  67. def getStreams(channel_id, channel_owner):
  68.     url = 'https://www.youtube.com/channel/%s/' % (channel_id)
  69.     headers = {
  70.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) Gecko/20100101 Firefox/104.0',
  71.         'Accept-Language': 'en-US,en;q=0.5'
  72.     }
  73.  
  74.     try:
  75.         req = urllib.request.Request(url, headers=headers)
  76.         conn = urllib.request.urlopen(req)
  77.     except (urllib.error.URLError, ValueError) as e:
  78.         print("Error! Couldn't retrieve URL. ", str(e).capitalize())
  79.         return
  80.     response = conn.read().decode('utf-8')
  81.     temp = re.findall('<script nonce="[^"]+">var ytInitialData = (.*?);</script>', response)
  82.     if not temp:
  83.         return
  84.  
  85.     streams = {}
  86.     streamIds = []
  87.     json_obj = json.loads(temp[0])
  88.     first_tab = json_obj['contents']['twoColumnBrowseResultsRenderer']['tabs'][0]
  89.     for section in first_tab['tabRenderer']['content']['sectionListRenderer']['contents']:
  90.         if 'shelfRenderer' not in section['itemSectionRenderer']['contents'][0] and 'channelFeaturedContentRenderer' not in section['itemSectionRenderer']['contents'][0]:
  91.             continue
  92.         if 'shelfRenderer' in section['itemSectionRenderer']['contents'][0]:
  93.             shelf_type = section['itemSectionRenderer']['contents'][0]['shelfRenderer']['title']['runs'][0]['text']
  94.         else:
  95.             shelf_type = 'Featured'
  96.  
  97.         if  shelf_type not in ['Featured', 'Live now', 'Upcoming live streams']:
  98.             continue
  99.  
  100.         if shelf_type == 'Featured':
  101.             row = section['itemSectionRenderer']['contents'][0]['channelFeaturedContentRenderer']['items']
  102.             vrname = 'videoRenderer'
  103.         else:
  104.             if 'expandedShelfContentsRenderer' not in section['itemSectionRenderer']['contents'][0]['shelfRenderer']['content']:
  105.                 row = section['itemSectionRenderer']['contents'][0]['shelfRenderer']['content']['horizontalListRenderer']['items']
  106.                 vrname = 'gridVideoRenderer'
  107.             else:
  108.                 row = section['itemSectionRenderer']['contents'][0]['shelfRenderer']['content']['expandedShelfContentsRenderer']['items']
  109.                 vrname = 'videoRenderer'
  110.  
  111.         for elem in row:
  112.             if elem[vrname]['videoId'] in streamIds:
  113.                 continue
  114.             new_stream = {'videoId': '', 'type'   : '', 'start'  : '', 'title'  : '', 'owner':''}
  115.             if shelf_type == "Live now":
  116.                 new_stream['videoId'] = elem[vrname]['videoId']
  117.                 new_stream['type']    = "live"
  118.                 new_stream['start']   = ""
  119.                 new_stream['owner']   = channel_owner
  120.                 new_stream['title']   = elem[vrname]['title']['simpleText']
  121.                 if 'live' not in streams:
  122.                     streams['live'] = [new_stream]
  123.                 else:
  124.                     streams['live'].append(new_stream)
  125.             elif shelf_type == "Upcoming live streams":
  126.                 if 'upcomingEventData' not in elem[vrname]:
  127.                     continue
  128.                 new_stream['videoId'] = elem[vrname]['videoId']
  129.                 new_stream['type']    = "upcoming"
  130.                 new_stream['start']   = elem[vrname]['upcomingEventData']['startTime']
  131.                 new_stream['owner']   = channel_owner
  132.                 new_stream['title']   = elem[vrname]['title']['simpleText']
  133.                 if 'upcoming' not in streams:
  134.                     streams['upcoming'] = [new_stream]
  135.                 else:
  136.                     streams['upcoming'].append(new_stream)
  137.             elif shelf_type == "Featured":
  138.                 new_stream['videoId'] = elem[vrname]['videoId']
  139.                 new_stream['type']    = "live"
  140.                 new_stream['start']   = ""
  141.                 new_stream['owner']   = channel_owner
  142.                 new_stream['title']   = elem[vrname]['title']['runs'][0]['text']
  143.                 if 'live' not in streams:
  144.                     streams['live'] = [new_stream]
  145.                 else:
  146.                     streams['live'].append(new_stream)
  147.             streamIds.append(elem[vrname]['videoId'])
  148.     return streams
  149.  
  150. def getTimeString(ts):
  151.     ret = ""
  152.     last_date = ""
  153.     for tz in timezones:
  154.         dts = ts + (tz['delta'] * 3600)
  155.         dts = datetime.datetime.utcfromtimestamp(dts)
  156.         date = datetime.datetime.strftime(dts, '%Y-%m-%d')
  157.         if last_date != date:
  158.             ret += date + " "
  159.         ret += datetime.datetime.strftime(dts, '%H:%M') + " " + tz['name'] + " "
  160.         last_date = date
  161.     return ret
  162.  
  163. def log(txt):
  164.     if output_to_file and fp:
  165.         fp.write(txt + "\n")
  166.     else:
  167.         print(txt)
  168.  
  169. channels = []
  170. for line in channel_data.splitlines():
  171.     if not line or line == "":
  172.         continue
  173.     tmp = line.split(' ')
  174.     channels.append({'channelId': tmp[0], 'channelOwner': " ".join(tmp[1:])})
  175.  
  176. timezones = []
  177. for line in timezone_data.splitlines():
  178.     if not line or line == "":
  179.         continue
  180.     tmp = line.split(' ')
  181.     timezones.append({'name': tmp[0], 'delta': float(tmp[1])})
  182.  
  183. # Sort timezones
  184. timezones = sorted(timezones, key=lambda kv: kv['delta'])
  185.  
  186. live_streams = []
  187. upcoming_streams = []
  188. # Retrieve information from channels
  189. for chan in channels:
  190.     print("Retrieving %s channel (%s)" % (chan['channelOwner'], chan['channelId']))
  191.     streams = getStreams(chan['channelId'], chan['channelOwner'])
  192.     if not streams:
  193.         continue
  194.     if 'live' in streams:
  195.         live_streams += streams['live']
  196.     if 'upcoming' in streams:
  197.         upcoming_streams += streams['upcoming']
  198.  
  199. print("")
  200. if output_to_file:
  201.     fp = open(outfname, 'w', encoding='utf-8')
  202.  
  203. live_streams = sorted(live_streams, key=lambda kv: kv['owner'])
  204. if live_streams:
  205.     log('LIVE STREAMS:')
  206.     last_owner = ""
  207.     for stream in live_streams:
  208.         if stream['owner'] != last_owner:
  209.             log("\n" + stream['owner'])
  210.         if show_live_titles:
  211.             log(stream['title'])
  212.         log("https://youtu.be/%s" % stream['videoId'])
  213.         last_owner = stream['owner']
  214. log("")
  215. if sort_streams:
  216.     upcoming_streams = sorted(upcoming_streams, key=lambda kv: kv['start'])
  217. if upcoming_streams:
  218.     log('UPCOMING STREAMS:')
  219.     last_owner = ''
  220.     for stream in upcoming_streams:
  221.         # If upcoming stream is more than 'ignore_days' days away, ignore it
  222.         if now() + (60 * 60 * 24 * ignore_days) < int(stream['start']):
  223.             continue
  224.  
  225.         timestr = getTimeString(int(stream['start']))
  226.         if stream['owner'] != last_owner:
  227.             log("\n" + stream['owner'])
  228.         if show_upcoming_titles:
  229.             log(stream['title'])
  230.         log("https://youtu.be/%s" % (stream['videoId']))
  231.         if show_timezones:
  232.             log(timestr)
  233.         last_owner = stream['owner']
  234. if output_to_file:
  235.     print("Output to file %s" % outfname)
  236.  
  237. if "PROMPT" not in os.environ:
  238.     input("\nPress ENTER to exit")
  239.  
Advertisement
Add Comment
Please, Sign In to add comment