Advertisement
Guest User

nijiholo-liveviewer-plotter.py

a guest
Dec 25th, 2021
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. import requests
  2. import json
  3. import time
  4. from datetime import datetime
  5. import matplotlib.pyplot as plt, mpld3
  6.  
  7.  
  8. def parse_json(response):
  9.     response =json.loads(response.text)
  10.     total_viewers = 0
  11.     for stream in response:
  12.         if stream['status'] == 'live':
  13.             total_viewers += stream['live_viewers']
  14.     return total_viewers
  15.  
  16. def holo_viewers():
  17.     response = requests.get("https://holodex.net/api/v2/live?org=Hololive")
  18.     return parse_json(response)
  19.  
  20. def niji_viewers():
  21.     response = requests.get("https://holodex.net/api/v2/live?org=Nijisanji")
  22.     return parse_json(response)
  23.  
  24. if __name__ == '__main__':
  25.     holo, niji = 0, 0
  26.     recorded_at = datetime.utcnow()
  27.     output_json = open("records.json", "a+")
  28.     html_file= open("index.html","w")
  29.     holo_array = [holo]
  30.     niji_array = [niji]
  31.     date_array = [recorded_at]
  32.     fig = plt.figure(figsize=(11, 7))
  33.     ax = fig.add_subplot(111)
  34.     holoplot, = ax.plot(date_array, holo_array, label="Hololive", color="blue")
  35.     nijiplot, = ax.plot(date_array, niji_array, label="Nijisanji", color="red")
  36.     plt.xlabel("time in UTC")
  37.     plt.ylabel("total number of concurrent viewers")
  38.     plt.legend()
  39.  
  40.     while True:
  41.         holo, niji = holo_viewers(), niji_viewers()
  42.         recorded_at = datetime.utcnow()
  43.         date_array.append(recorded_at)
  44.         holo_array.append(holo)
  45.         niji_array.append(niji)
  46.         print(" Hololive viewers at time (UTC)", recorded_at)
  47.         print(holo)
  48.         print(" Nijisanji viewers at time (UTC)", recorded_at)
  49.         print(niji)
  50.         output_json.write("{"+str(recorded_at)+":{hololive:"+str(holo)+",nijisanji:"+str(niji)+"}},")
  51.        
  52.         holoplot, = ax.plot(date_array, holo_array, label="Hololive", color="blue")
  53.         nijiplot, = ax.plot(date_array, niji_array, label="Nijisanji", color="red")
  54.         fig.canvas.draw()
  55.         fig.canvas.flush_events()
  56.  
  57.         html_str = mpld3.fig_to_html(fig)
  58.         html_file.seek(0)
  59.         html_file.write(html_str)
  60.         html_file.truncate()
  61.         time.sleep(900)
  62.  
  63.     html_file.close()
  64.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement