Sclafus

Twitch Data-Analysis v1.1 (CSV)

May 9th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.70 KB | None | 0 0
  1. '''
  2.  
  3. v1.0 (9/5/19):  twitch data-analysis from the csv file you can download from their website
  4.                You can download the file you need here: https://www.twitch.tv/CHANNELNAME/dashboard/channel-analytics/
  5.                The csv file is formatted in this way:
  6.                Start Time,End Time,Average Viewers,Max. Viewers,Total Views,Unique Views,Minutes Watched,New Followers,Chatters,Chat Messages,Clips Created,Clip Views,Ad Breaks (Minutes),Minutes Streamed
  7.                But in the future, it might vary. I'll try to keep this code updated.
  8. v1.1 (10/5/19): added matplotlib graphs for each category
  9.  
  10. '''
  11.  
  12. import matplotlib.pyplot as plt
  13. import numpy as np
  14. version = "1.1"
  15.  
  16.  
  17. def main():
  18.     flag = True
  19.     while(flag):
  20.         graph_flag = True
  21.         avg_viewers = []
  22.         max_viewers = []
  23.         minutes_streamed = []
  24.         new_followers = []
  25.         clips = []
  26.         days = []
  27.         months = []
  28.         print("Twitch Analytics from csv")
  29.         print("Version: " + version)
  30.         filename = input("Insert filename or path:")
  31.         if filename[-4:] != '.csv':
  32.             filename = filename + '.csv'
  33.         tmp = input("Would you like to see a graph of your stats?(y/n)")
  34.         if tmp != 'y':
  35.             graph_flag = False
  36.        
  37.         #leggo dal file
  38.         with open(filename, "r") as f1:
  39.             next(f1)
  40.             for line in f1:
  41.                 a = line.split(',')
  42.  
  43.                 #metto tutto in liste d'appoggio per chiarezza
  44.                 avg_viewers.append(float(a[2]))
  45.                 max_viewers.append(int(a[3]))
  46.                 minutes_streamed.append(int(a[-1]))
  47.                 new_followers.append(int(a[7]))
  48.                 clips.append(int(a[10]))
  49.  
  50.                 #giorni
  51.                 b = a[1].split(" ")
  52.                 months.append(b[1])
  53.                 days.append(b[2])
  54.  
  55.         #stampo a console i dati
  56.    
  57.         print("\nStats for the period between " + days[0] + " " + months[0] + " and " + days[-1] + " " + months[-1])
  58.         print("Average viewers: " + str(round(sum(avg_viewers)/len(avg_viewers), 3)))
  59.         print("Viewers record: " + str(max(max_viewers)))
  60.         print("Time you spent live-steaming: " + str(sum(minutes_streamed)) + " minutes (" + str(round(sum(minutes_streamed) / 60.0, 2)) + " hours)")
  61.         print("Longest live-stream: " + str(max(minutes_streamed)) + "minutes (" + str(round(max(minutes_streamed) / 60.0 , 2)) + " hours)")
  62.         print("New followers: " + str(sum(new_followers)))
  63.         print("Clips generated: " + str(sum(clips)))
  64.  
  65.         if graph_flag:
  66.             fig = plt.figure()
  67.             avg = fig.add_subplot(711)
  68.             tim = fig.add_subplot(713)
  69.             fol = fig.add_subplot(715)
  70.             clp = fig.add_subplot(717)
  71.  
  72.             #avg viewers
  73.             avg.plot(avg_viewers , 'o', avg_viewers, '-')
  74.             avg.plot(max_viewers, '--')
  75.             avg.minorticks_on()
  76.             avg.set_title("Average Viewers")
  77.             avg.set_ylabel("Viewers")
  78.            
  79.             #minutes
  80.             tim.plot(minutes_streamed , 'o', minutes_streamed, '-')
  81.             tim.set_title("Minutes")
  82.             tim.set_ylabel("Minutes")
  83.            
  84.  
  85.             #followers
  86.             fol.plot(new_followers)
  87.             fol.set_title("New followers")
  88.             fol.set_ylabel("Followers")
  89.            
  90.             #clips
  91.             clp.plot(clips , 'o', clips, '-')
  92.             clp.set_title("Clips Generated")
  93.             clp.set_ylabel("Clips")
  94.            
  95.             plt.show()
  96.        
  97.         #flag e loop
  98.         x = input("\nDo you have more files? (y/n)")
  99.         print("")
  100.         if (x != "y"):
  101.             flag = False
  102.  
  103. main()
Advertisement
Add Comment
Please, Sign In to add comment