Saturday, June 8, 2013

[Python] Livestream and Justin.tv -- 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
#06/02/13
#http://www.livestream.com/userguide/index.php?title=Channel_API#Channel_Live_Status
#06/09/13
#http://apiwiki.justin.tv/mediawiki/index.php/Stream/list



import json
import urllib2
import webbrowser

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

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

#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
#livestream

for i in range(len(str1)):
    #get and parse json
    url3 = urllib2.urlopen('http://x%sx.api.channel.livestream.com/2.0/livestatus.json' % 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
    else:
        info[i][1] = 0
   
#justin/twitch
for i in range(len(str2)):
    url3 = urllib2.urlopen('http://api.justin.tv/api/stream/list.json?channel=%s' % 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
    else:
        info[i+len(str1)][2] = 0

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

    0:\t\tExit'''
)

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
        pass
    elif option in range(len(str1)+1):    #livestream from 1 to n
        webbrowser.open('http://www.livestream.com/'+info[option-1][0])
    elif option in range(len(str1)+1, len(str1)+1+len(str2)):    #justin.tv from n to last
        webbrowser.open('http://www.justin.tv/'+info[option-1][0])
    else:
        print
('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