Skip to content

Instantly share code, notes, and snippets.

@SingularReza
Last active January 14, 2022 04:25
Show Gist options
  • Select an option

  • Save SingularReza/36639247a42a4e764d59a7352aee18c6 to your computer and use it in GitHub Desktop.

Select an option

Save SingularReza/36639247a42a4e764d59a7352aee18c6 to your computer and use it in GitHub Desktop.

Revisions

  1. SingularReza revised this gist Jan 14, 2022. 1 changed file with 24 additions and 28 deletions.
    52 changes: 24 additions & 28 deletions nijiholo-liveviewer-plotter.py
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,17 @@
    from numpy import record
    import requests
    from bokeh.plotting import figure, output_file, save
    from bokeh.models import BasicTickFormatter
    from datetime import datetime
    import json
    import requests
    import time
    from datetime import datetime
    import matplotlib.pyplot as plt, mpld3
    import os

    from bokeh.models.tools import HoverTool

    def parse_json(response):
    response =json.loads(response.text)
    if response is not None:
    response =json.loads(response.text)
    else:
    response = []
    total_viewers = 0
    obj = {
    "total_viewers": total_viewers,
    @@ -26,7 +29,7 @@ def parse_json(response):
    total_viewers += stream['live_viewers']

    obj['total_viewers'] = total_viewers
    print(obj)
    #print(obj)
    return obj

    def holo_viewers():
    @@ -40,19 +43,17 @@ def niji_viewers():
    if __name__ == '__main__':
    recorded_at = datetime.utcnow()
    output_json = open("records.json", "a+")
    html_file= open("index.html","w")
    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]

    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_obj, niji_obj = holo_viewers(), niji_viewers()
    recorded_at = datetime.utcnow()
    @@ -64,27 +65,22 @@ def niji_viewers():
    print(" Hololive viewers at time (UTC)", recorded_at)
    print(holo_obj)
    print(" Nijisanji viewers at time (UTC)", recorded_at)
    print(niji_obj)
    #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())

    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(600)

    time.sleep(600)
  2. SingularReza revised this gist Dec 28, 2021. 1 changed file with 35 additions and 13 deletions.
    48 changes: 35 additions & 13 deletions nijiholo-liveviewer-plotter.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    from numpy import record
    import requests
    import json
    import time
    @@ -9,10 +10,24 @@
    def parse_json(response):
    response =json.loads(response.text)
    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']
    return total_viewers

    obj['total_viewers'] = total_viewers
    print(obj)
    return obj

    def holo_viewers():
    response = requests.get("https://holodex.net/api/v2/live?org=Hololive")
    @@ -23,13 +38,13 @@ def niji_viewers():
    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]
    holo_array = [0]
    niji_array = [0]
    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")
    @@ -39,16 +54,25 @@ def niji_viewers():
    plt.legend()

    while True:
    holo, niji = holo_viewers(), niji_viewers()
    holo_obj, niji_obj = holo_viewers(), niji_viewers()
    recorded_at = datetime.utcnow()

    date_array.append(recorded_at)
    holo_array.append(holo)
    niji_array.append(niji)
    holo_array.append(holo_obj['total_viewers'])
    niji_array.append(niji_obj['total_viewers'])

    print(" Hololive viewers at time (UTC)", recorded_at)
    print(holo)
    print(holo_obj)
    print(" Nijisanji viewers at time (UTC)", recorded_at)
    print(niji)
    output_json.write("{"+str(recorded_at)+":{hololive:"+str(holo)+",nijisanji:"+str(niji)+"}},")
    print(niji_obj)

    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())

    @@ -61,8 +85,6 @@ def niji_viewers():
    html_file.seek(0)
    html_file.write(html_str)
    html_file.truncate()
    time.sleep(900)

    output_json.close()
    html_file.close()
    time.sleep(600)

  3. SingularReza revised this gist Dec 25, 2021. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions nijiholo-liveviewer-plotter.py
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    import time
    from datetime import datetime
    import matplotlib.pyplot as plt, mpld3
    import os


    def parse_json(response):
    @@ -48,6 +49,8 @@ def niji_viewers():
    print(" Nijisanji viewers at time (UTC)", recorded_at)
    print(niji)
    output_json.write("{"+str(recorded_at)+":{hololive:"+str(holo)+",nijisanji:"+str(niji)+"}},")
    output_json.flush()
    os.fsync(output_json.fileno())

    holoplot, = ax.plot(date_array, holo_array, label="Hololive", color="blue")
    nijiplot, = ax.plot(date_array, niji_array, label="Nijisanji", color="red")
    @@ -60,5 +63,6 @@ def niji_viewers():
    html_file.truncate()
    time.sleep(900)

    output_json.close()
    html_file.close()

  4. SingularReza created this gist Dec 25, 2021.
    64 changes: 64 additions & 0 deletions nijiholo-liveviewer-plotter.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    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()