Saturday, June 8, 2013

[Python] Livestream and -- Check if Live

I recently switched from iOS to Android, and one of the apps I had used on iOS was the mobile Livestream app. One feature was the ability to follow channels, and I liked to follow channels to see how some of my favorite artists draw--techniques and whatnot. (Also some other random channels whose content I might enjoy).

So anyway, I lost the ability to do this, since I couldn't find an equivalent app on Android. Apparently on the PC, one could make a "New Livestream" account to follow, but I don't wanna.

Solution? Make some code!

My computer-science-major friend had once told me about Python, a pretty (really) versatile scripting language. What better way to learn about this awesome language?

My code follows:
#Henry Choi
#checks if livestream is live

import json
import urllib2
import webbrowser

#read some files
read = open('livestream channels.txt','r')
str1 =

read = open('justin-twitch channels.txt','r')
str2 =

#create a 2D array| columns:3, rows:number of streams
info = [['' for j in range(3)] for i in range(len(str1+str2))]
#info = [['']*3]*len(str1)    |doesnt work|

#get the channel information

for i in range(len(str1)):
    #get and parse json
    url3 = urllib2.urlopen('' % str1[i]).read()
    asd = json.loads(url3)
    info[i][0] = str1[i]    #save stream name
    info[i][2] = asd['channel']['currentViewerCount']    #save stream viewer count
    if asd['channel']['isLive']:    #if live
        #mark as live
        info[i][1] = 1
        info[i][1] = 0
for i in range(len(str2)):
    url3 = urllib2.urlopen('' % str2[i]).read()
    asd = json.loads(url3)
    info[i+len(str1)][0] = str2[i]    #save stream name
    if asd:        #if live
        info[i+len(str1)][1] = 1    #mark as live
        info[i+len(str1)][2] = asd[0]['channel_count']    #save stream viewer count
        info[i+len(str1)][2] = 0

#print info
print ('''
Enter the number to view the stream.


for i in range(len(info)):
    print (str(i+1).rjust(5)+':  %s %s \t%s' % ('*' if info[i][1] else ' ', info[i][2], info[i][0]))
print ('')

option = 1
while option != 0:
    option = input('Enter the number: ')
    if option == 0:     #just exit
    elif option in range(len(str1)+1):    #livestream from 1 to n''+info[option-1][0])
    elif option in range(len(str1)+1, len(str1)+1+len(str2)): from n to last''+info[option-1][0])
('Invalid choice. Please choose a correct one.')

And there it is!

txt files named "livestream channels.txt" and "justin-twitch channels.txt" should have the channel names of the appropriate streaming service, separated by new lines. They exist in the same folder as the python script.

No comments:

Post a Comment