from models import Channel, Video
import re
import requests
from django.utils import timezone

import logging
logging.basicConfig()

def update_channels():
    print "Updating channels"
    livestream = re.compile("http://api.new.livestream.com/accounts/(\d+)")
    for channel in Channel.objects.all():
        try:
            print channel
            channel.lastRetrieved = timezone.now()
            videos = Video.objects.filter(channel=channel)
            existing_videos = []
            res = livestream.search(channel.url)
            if res != None:
                url = "http://api.new.livestream.com/accounts/%s/events?newer=9" % res.groups(1)
                data = requests.get(url).json()
                for item in data["data"]:
                    video_url = "http://livestream.com/%s/%s" % (item["owner"]["short_name"], item["short_name"])
                    print video_url
                    filtered = videos.filter(url=video_url)
                    if filtered.exists():
                        existing_videos.append(filtered.first().id)
                    else:
                        existing_videos.append(Video.objects.create(url=video_url, channel=channel).id)
            else:
                print "Don't know %s channel" % channel.url
                channel.working = False
                channel.save()
                continue
            missing = videos.exclude(id__in = Video.objects.filter(id__in=existing_videos).values_list('id', flat=True))
            for m in missing:
                m.delete()
            channel.working = True
        except Exception, e:
            print e
            channel.working = False
        print "Updated", channel
        channel.save()

@@ -86,16 +86,39 @@
 timezone
+%0Afrom os import environ
 %0A%0Aimport
@@ -153,65 +153,26 @@
 ()%0A%0A
-def update_channels():%0A    print %22Updating channels%22%0A    
+channels = %7B%0A    %22
 live
@@ -177,18 +177,18 @@
 vestream
- =
+%22:
  re.comp
@@ -238,16 +238,352 @@
 /(%5Cd+)%22)
+,%0A    %22youtube%22: re.compile(%22https://www.youtube.com/channel/(.*)%22)%0A%7D%0A%0Adef get_channel_type(channel_url):%0A    for key in channels:%0A        pattern = channels%5Bkey%5D%0A        res = pattern.search(channel_url)%0A        if res != None:%0A            return (key, res)%0A    return (None, None)%0A%0Adef update_channels():%0A    print %22Updating channels%22
 %0A    for
@@ -647,16 +647,27 @@
    print
+ %22Channel%22,
  channel
@@ -825,32 +825,39 @@
     
+ (kind,
  res
+)
  = 
-livestream.search
+get_channel_type
 (cha
@@ -874,38 +874,75 @@
             
-if re
+video_url
 s 
-!
 = 
-None
+%5B%5D%0A            if kind == %22livestream%22
 :%0A          
@@ -1252,22 +1252,34 @@
         
-print 
+video_urls.append(
 video_ur
@@ -1279,16 +1279,17 @@
 ideo_url
+)
 %0A       
@@ -1297,58 +1297,95 @@
     
-        filtered = videos.filter(url=video_url)%0A  
+elif kind == %22youtube%22:%0A                api_key = environ.get(%22YOUTUBE_API_KEY%22, None)%0A
     
@@ -1400,30 +1400,26 @@
     
-  
 if 
-filtered.exists()
+api_key == None
 :%0A  
@@ -1440,54 +1440,196 @@
     
-    existing_videos.append(filtered.first().id
+raise Exception, %22No YOUTUBE_API_KEY%22%0A                url = %22https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=%25s&eventType=live&type=video&key=%25s%22%25(res.group(1), api_key
 )%0A  
@@ -1638,33 +1638,33 @@
             
-    else:
+print url
 %0A           
@@ -1672,269 +1672,751 @@
     
-        existing_videos.append(Video.objects.create(url=video_url, channel=channel).id)%0A            else:%0A                print %22Don't know %25s channel%22 %25 channel.url%0A                channel.working = False%0A                channel.save()%0A                continue
+data = requests.get(url).json()%0A                for item in data%5B%22items%22%5D:%0A                    video_url = %22https://www.youtube.com/embed/%25s%22 %25 (item%5B%22id%22%5D%5B%22videoId%22%5D)%0A                    video_urls.append(video_url)%0A            else:%0A                print %22Don't know %25s channel%22 %25 channel.url%0A                channel.working = False%0A                channel.save()%0A                continue%0A            for video_url in video_urls:%0A                print video_url%0A                filtered = videos.filter(url=video_url)%0A                if filtered.exists():%0A                    existing_videos.append(filtered.first().id)%0A                else:%0A                    existing_videos.append(Video.objects.create(url=video_url, channel=channel).id)
 %0A   

Add Youtube channel support (fixes #15)