Last active
January 14, 2022 04:25
-
-
Save SingularReza/36639247a42a4e764d59a7352aee18c6 to your computer and use it in GitHub Desktop.
records the total concurrent viewers over a time (polls every 15 min) and outputs it to a graph in html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from bokeh.plotting import figure, output_file, save | |
| from bokeh.models import BasicTickFormatter | |
| from datetime import datetime | |
| import json | |
| import requests | |
| import time | |
| import os | |
| from bokeh.models.tools import HoverTool | |
| def parse_json(response): | |
| if response is not None: | |
| response =json.loads(response.text) | |
| else: | |
| response = [] | |
| total_viewers = 0 | |
| obj = { | |
| "total_viewers": total_viewers, | |
| "live_streams": [] | |
| } | |
| for stream in response: | |
| if stream['status'] == 'live': | |
| live_stream = { | |
| "name": "", | |
| "viewers": 0 | |
| } | |
| live_stream['name'], live_stream['viewers'] =stream['channel']['name'], stream['live_viewers'] | |
| obj['live_streams'].append(live_stream) | |
| total_viewers += stream['live_viewers'] | |
| obj['total_viewers'] = total_viewers | |
| #print(obj) | |
| return obj | |
| 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__': | |
| recorded_at = datetime.utcnow() | |
| output_json = open("records.json", "a+") | |
| TOOLTIPS = [("date","$x{%F %H:%M}"), ("viewers", "$y{int}")] | |
| output_file(filename="bokeh.html", title="total concurrent viewers graph") | |
| plot = figure(sizing_mode="stretch_width", max_width=1000, height=660, x_axis_type='datetime', x_axis_label='date in UTC', y_axis_label='viewer count') | |
| plot.yaxis[0].formatter = BasicTickFormatter(use_scientific=False) | |
| hover = HoverTool(tooltips = TOOLTIPS, formatters={"$x":"datetime"}, mode="mouse") | |
| #plot.add_tools(hover) | |
| holo_array = [0] | |
| niji_array = [0] | |
| date_array = [recorded_at] | |
| while True: | |
| holo_obj, niji_obj = holo_viewers(), niji_viewers() | |
| recorded_at = datetime.utcnow() | |
| date_array.append(recorded_at) | |
| holo_array.append(holo_obj['total_viewers']) | |
| niji_array.append(niji_obj['total_viewers']) | |
| print(" Hololive viewers at time (UTC)", recorded_at) | |
| print(holo_obj) | |
| print(" Nijisanji viewers at time (UTC)", recorded_at) | |
| #print(niji_obj) """ | |
| plot.line(date_array, holo_array, legend_label="Hololive", line_color="blue") | |
| plot.line(date_array, niji_array, legend_label="Nijisanji", line_color="red") | |
| save(plot) | |
| obj = { | |
| "time": str(recorded_at), | |
| "nijisanji": niji_obj, | |
| "hololive": holo_obj | |
| } | |
| output_json.write(json.dumps(obj)) | |
| output_json.flush() | |
| os.fsync(output_json.fileno()) | |
| time.sleep(600) |
Author
Author
v2 - now records the live channels and their viewer count as well at the time of polling. polling time interval reduced to 10 min
Author
v3 - replaced matplotlib and mpld3 with the lighter bokeh libraries
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Leaves out bilibili because there's no corresponding "viewer" metrics for the site. The data is also recorded in a seperate json file for 3rd party use. Data is from holodex (god bless the devs)