]> git.frykholm.com Git - svtplaydump.git/blobdiff - svtplaydump.py
Added progress display in mirror mode.
[svtplaydump.git] / svtplaydump.py
index 80ee0b21b745384078c7bda63729f420ef95431f..3fbb16af8a2e870fc3acedc8c78c4fa45ceaabaa 100755 (executable)
@@ -41,7 +41,7 @@ except ImportError:
     pass
 import sys, os
 
-def main(url, title):
+def scrape_player_page(url, title):
     """
     Try to scrape the site for video and download. 
     """
@@ -51,7 +51,11 @@ def main(url, title):
     page = urllib2.urlopen(url).read()
     soup = BeautifulSoup(page,convertEntities=BeautifulSoup.HTML_ENTITIES)
     video_player = soup.body('a',{'data-json-href':True})[0]
-    flashvars = json.loads(urllib2.urlopen("http://www.svtplay.se/%s"%video_player.attrMap['data-json-href']+"?output=json").read())
+    if video_player.attrMap['data-json-href'].startswith("/wd"):
+        flashvars = json.loads(urllib2.urlopen("http://www.svt.se/%s"%video_player.attrMap['data-json-href']).read())
+    else:    
+        flashvars = json.loads(urllib2.urlopen("http://www.svtplay.se/%s"%video_player.attrMap['data-json-href']+"?output=json").read())
+    video['duration'] = video_player.attrMap.get('data-length',0)
     video['title'] = title
     if not title:
         video['title'] = soup.find('meta',{'property':'og:title'}).attrMap['content'].replace('|','_').replace('/','_')
@@ -142,48 +146,66 @@ def parse_segment_playlist(playlist):
              metadata["EXT-X-KEY"] = dict([part.split('=',1) for part in parts if '=' in part]) #throw away the commas and make dict of the pairs
     return(segments, metadata)   
 def parse_videolist():
-    page = urllib2.urlopen("http://www.svtplay.se/ajax/videos?antal=100").read()
+    page_num = 1
+    page = urllib2.urlopen("http://www.svtplay.se/ajax/videospager").read() #this call does not work for getting the pages, we use it for the page totals only
     soup = BeautifulSoup(page,convertEntities=BeautifulSoup.HTML_ENTITIES)
-    videos = []
-    for article in soup.findAll('article'):
-        meta = dict(article.attrs)
-        video = {}
-        video['title'] = meta['data-title']
-        video['description'] = meta['data-description']
-        video['url'] = dict(article.find('a').attrs)['href']
-        videos.append(video)
-    return videos
+    page_tot = int(soup.find('a',{'data-currentpage':True}).attrMap['data-lastpage'])
+    videos_per_page = 8
+    video_num = 0
+    while(page_num <= page_tot):
+        base_url = "http://www.svtplay.se/ajax/videos?sida={}".format(page_num)
+        page = urllib2.urlopen(base_url).read()
+        soup = BeautifulSoup(page,convertEntities=BeautifulSoup.HTML_ENTITIES)
+        for article in soup.findAll('article'):
+            meta = dict(article.attrs)
+            video = {}
+            video['title'] = meta['data-title']
+            video['description'] = meta['data-description']
+            video['url'] = dict(article.find('a').attrs)['href']
+            video['thumb-url'] = dict(article.find('img',{}).attrs)['src']
+            video['num'] = video_num
+            video['total'] = page_tot * videos_per_page
+            video_num += 1
+            yield video
+        page_num += 1
+
 
 if __name__ == "__main__":
     parser = argparse.ArgumentParser()
-    parser.add_argument("-r", "--rss", help="Download all files in rss")
-    parser.add_argument("-u", "--url", help="Download video in url")
-    parser.add_argument("-m", "--mirror", help="Mirror all files", action="store_true")
-
+    group = parser.add_mutually_exclusive_group(required=True)
+    group.add_argument("-r", "--rss", help="Download all files in rss")
+    group.add_argument("-u", "--url", help="Download video in url")
+    group.add_argument("-m", "--mirror", help="Mirror all files", action="store_true")
+    parser.add_argument("-n", "--no_act", help="Just print what would be done, don't do any downloading.", action="store_true")
     args = parser.parse_args()
-
     if args.rss: 
-        d = feedparser.parse(args.url)
+        d = feedparser.parse(args.rss)
         for e in d.entries:
             print("Downloading: %s"%e.title)
-            filename = main(e.link, e.title)
+            if args.no_act:
+                continue
+            filename = scrape_player_page(e.link, e.title)
             print Popen(["avconv","-i",filename,"-vcodec","copy","-acodec","copy", filename+'.mkv'], stdout=PIPE).communicate()[0]
         #print(e.description)
     if args.mirror:
         for video in parse_videolist():
             video['title'] = video['title'].replace('/','_')
             print video['title']+'.mkv',
+            print u"{} of {}".format(video['num'], video['total'])
             if os.path.exists(video['title']+'.mkv'):
                 print "Skipping" 
                 continue
             print("Downloading...")
-            ret = main(video['url'], video['title'])
-
+            if args.no_act:
+                continue
+            ret = scrape_player_page(video['url'], video['title'])
+            print ret
             print Popen(["avconv","-i",video['title']+'.ts',"-vcodec","copy","-acodec","copy", video['title']+'.mkv'], stdout=PIPE).communicate()[0]
             try:
                 os.unlink(video['title']+'.ts')
             except:
                 import pdb;pdb.set_trace()
     else:
-        video = main(args.url, None)
-        print("Downloaded {}".format(video['title']))   
\ No newline at end of file
+        if not args.no_act:
+            video = scrape_player_page(args.url, None)
+        print(u"Downloaded {}".format(args.url))   
\ No newline at end of file