Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import json
- import time
- from datetime import datetime
- import matplotlib.pyplot as plt, mpld3
- def parse_json(response):
- response =json.loads(response.text)
- total_viewers = 0
- for stream in response:
- if stream['status'] == 'live':
- total_viewers += stream['live_viewers']
- return total_viewers
- def holo_viewers():
- response = requests.get("https://holodex.net/api/v2/live?org=Hololive")
- return parse_json(response)
- def niji_viewers():
- response = requests.get("https://holodex.net/api/v2/live?org=Nijisanji")
- return parse_json(response)
- if __name__ == '__main__':
- holo, niji = 0, 0
- recorded_at = datetime.utcnow()
- output_json = open("records.json", "a+")
- html_file= open("index.html","w")
- holo_array = [holo]
- niji_array = [niji]
- date_array = [recorded_at]
- fig = plt.figure(figsize=(11, 7))
- ax = fig.add_subplot(111)
- holoplot, = ax.plot(date_array, holo_array, label="Hololive", color="blue")
- nijiplot, = ax.plot(date_array, niji_array, label="Nijisanji", color="red")
- plt.xlabel("time in UTC")
- plt.ylabel("total number of concurrent viewers")
- plt.legend()
- while True:
- holo, niji = holo_viewers(), niji_viewers()
- recorded_at = datetime.utcnow()
- date_array.append(recorded_at)
- holo_array.append(holo)
- niji_array.append(niji)
- print(" Hololive viewers at time (UTC)", recorded_at)
- print(holo)
- print(" Nijisanji viewers at time (UTC)", recorded_at)
- print(niji)
- output_json.write("{"+str(recorded_at)+":{hololive:"+str(holo)+",nijisanji:"+str(niji)+"}},")
- holoplot, = ax.plot(date_array, holo_array, label="Hololive", color="blue")
- nijiplot, = ax.plot(date_array, niji_array, label="Nijisanji", color="red")
- fig.canvas.draw()
- fig.canvas.flush_events()
- html_str = mpld3.fig_to_html(fig)
- html_file.seek(0)
- html_file.write(html_str)
- html_file.truncate()
- time.sleep(900)
- html_file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement