]> git.frykholm.com Git - svtplaydump.git/blobdiff - svtplaydump.py
Quick and dirty tv4 downloader
[svtplaydump.git] / svtplaydump.py
index 3fbb16af8a2e870fc3acedc8c78c4fa45ceaabaa..e9a4a3b9c0d6cf29f57539c01670de268570a64c 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 #
 #   (C) Copyright 2010 Mikael Frykholm <mikael@frykholm.com>
 # 0.2 added python 2.4 urlparse compatibility
 # 0.1 initial release
 
-from BeautifulSoup import BeautifulSoup
+from bs4 import BeautifulSoup, Doctype
 from subprocess import *
 import re
-import json
 from Crypto.Cipher import AES
 import struct
 import argparse
-import feedparser 
-try:
-    import urlparse
-except ImportError:
-    pass
-import urllib2
-try:
-    import urllib2.urlparse as urlparse
-except ImportError:
-    pass
+import requests
 import sys, os
+import socket
 
-def scrape_player_page(url, title):
+class Video(dict):
+    def __init__(self, *args, **kwargs):
+        self.update(dict(*args, **kwargs))  # use the free update to set keys
+
+    def __setattr__(self, name, value):
+        return self.__setitem__(name,value)
+        
+    def __getattr__(self, name):
+        return self.__getitem__(name)
+        
+    def is_downloaded(self):
+        raise("NotImplemented")
+
+def scrape_player_page(video):
     """
     Try to scrape the site for video and download. 
     """
-    if not url.startswith('http'):
-        url = "http://www.svtplay.se" + url
-    video = {}
-    page = urllib2.urlopen(url).read()
-    soup = BeautifulSoup(page,convertEntities=BeautifulSoup.HTML_ENTITIES)
+    if not video['url'].startswith('http'):
+        video['url'] = "http://www.svtplay.se" + video['url']
+    soup = BeautifulSoup(requests.get(video['url']).text)
     video_player = soup.body('a',{'data-json-href':True})[0]
-    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())
+    if 'oppetarkiv.se' in video['url']:
+        flashvars = requests.get("http://www.oppetarkiv.se/%s"%video_player.attrs['data-json-href']+"?output=json").json()
     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('/','_')
+        if video_player.attrs['data-json-href'].startswith("/wd"):
+            flashvars = requests.get("http://www.svt.se/%s"%video_player.attrs['data-json-href']).json()
+        else:    
+            flashvars = requests.get("http://www.svtplay.se/%s"%video_player.attrs['data-json-href']+"?output=json").json()
+    video['duration'] = video_player.attrs.get('data-length',0)
+    if not video['title']:
+        video['title'] = soup.find('meta',{'property':'og:title'}).attrs['content'].replace('|','_').replace('/','_')
+    if not 'genre' in video:
+        if soup.find(text='Kategori:'):
+            video['genre'] = soup.find(text='Kategori:').parent.parent.a.text
+        else:
+            video['genre'] = 'Ingen Genre' 
     if 'dynamicStreams' in flashvars:
         video['url'] = flashvars['dynamicStreams'][0].split('url:')[1].split('.mp4,')[0] +'.mp4'
         filename = video['title']+".mp4"
-        print Popen(["rtmpdump",u"-o"+filename,"-r", url], stdout=PIPE).communicate()[0]
+        print(Popen(["rtmpdump","-o"+filename,"-r", url], stdout=PIPE).communicate()[0])
     if 'pathflv' in flashvars:
         rtmp = flashvars['pathflv'][0]
         filename = video['title']+".flv"
-        print Popen(["mplayer","-dumpstream","-dumpfile",filename, rtmp], stdout=PIPE).communicate()[0]
+        print(Popen(["mplayer","-dumpstream","-dumpfile",filename, rtmp], stdout=PIPE).communicate()[0])
     if 'video' in flashvars:
         for reference in flashvars['video']['videoReferences']:
-            if reference['url'].endswith("m3u8"):
+            if 'm3u8' in reference['url']:
                 video['url']=reference['url']
                 video['filename'] = video['title']+'.ts'
                 if 'statistics' in flashvars:
                     video['category'] = flashvars['statistics']['category']
         download_from_playlist(video)
-    else:
-        print "Could not find any streams"
-        return
+    if not 'url' in video:
+        print("Could not find any streams")
+        return False
     return video
 
 def download_from_playlist(video):
-    playlist = parse_playlist(urllib2.urlopen(video['url']).read())
+    playlist = parse_playlist(requests.get(video['url']).text)
+    if not playlist:
+        return
     videourl = sorted(playlist, key=lambda k: int(k['BANDWIDTH']))[-1]['url']
-    segments, metadata = parse_segment_playlist(urllib2.urlopen(videourl).read())
+    if not videourl.startswith('http'): #if relative path
+        videourl = "{}/{}".format(os.path.dirname(video['url']), videourl) 
+    segments, metadata = parse_segment_playlist(videourl)
     if "EXT-X-KEY" in metadata:
-        key = urllib2.urlopen(metadata["EXT-X-KEY"]['URI'].strip('"')).read()
+        key = requests.get(metadata["EXT-X-KEY"]['URI'].strip('"')).text
         decrypt=True
     else:
         decrypt=False
-    with open("%s"%video['filename'],"w") as ofile:
+    with open("%s"%video['filename'],"wb") as ofile:
         segment=0
         size = 0
         for url in segments:
-            ufile = urllib2.urlopen(url)
-            print "\r{} MB".format(size/1024/1024),
+            ufile = requests.get(url, stream=True).raw
+            print("\r{0:.2f} MB".format(size/1024/1024),end="")
             sys.stdout.flush()
             if decrypt:
                 iv=struct.pack("IIII",segment,0,0,0)
                 decryptor = AES.new(key, AES.MODE_CBC, iv)
             while(True):
-                buf = ufile.read(1024)
-                if buf:
-                    if decrypt:
-                        buf = decryptor.decrypt(buf)
-                    ofile.write(buf)
-                    size += len(buf)
-                else:
-                    ufile.close()
+                try:
+                    buf = ufile.read(4096)
+                except socket.error as e:
+                    print("Error reading, skipping file")
+                    print(e)
+                    return
+                if not buf:
                     break
+                if decrypt:
+                    buf = decryptor.decrypt(buf)
+                ofile.write(buf)
+                size += len(buf)
             segment += 1
 
+    if 'thumb-url' in video:
+        video['thumb'] = requests.get(video['thumb-url'],stream=True).raw
+
 def parse_playlist(playlist):
     if not playlist.startswith("#EXTM3U"):
-        print playlist
+        print(playlist)
         return False
-    playlist = playlist.splitlines()[1:]
+    playlist = playlist.splitlines()
+    while not 'EXT-X-STREAM-INF' in playlist[0]:
+        playlist = playlist[1:]
     items=[]
     for (metadata_string,url) in zip(playlist[0::2], playlist[1::2]):
-        md = dict()
-        assert 'EXT-X-STREAM-INF' in metadata_string.split(':')[0]
+        md = Video()
+        if not 'EXT-X-STREAM-INF' in metadata_string.split(':')[0]:
+            continue
         for item in metadata_string.split(':')[1].split(','):
             if '=' in item:
                 md.update([item.split('='),]) 
@@ -127,7 +149,8 @@ def parse_playlist(playlist):
         items.append(md)
     return items 
 
-def parse_segment_playlist(playlist):
+def parse_segment_playlist(playlisturl):
+    playlist = requests.get(playlisturl).text
     assert playlist.startswith("#EXTM3U")
     PATTERN = re.compile(r'''((?:[^,"']|"[^"]*"|'[^']*')+)''')
     segments = []
@@ -135,6 +158,8 @@ def parse_segment_playlist(playlist):
     metadata = {}
     for row in playlist.splitlines():
         if next_is_url:
+            if not row.startswith('http'): #if relative path
+                row = "{}/{}".format(os.path.dirname(playlisturl), row) 
             segments.append(row)
             next_is_url=False
             continue
@@ -144,21 +169,20 @@ def parse_segment_playlist(playlist):
              row = row.split(':',1)[1] #skip first part
              parts = PATTERN.split(row)[1:-1] #do magic re split and keep quotes
              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)   
+    return(segments, metadata)
+
 def parse_videolist():
     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)
-    page_tot = int(soup.find('a',{'data-currentpage':True}).attrMap['data-lastpage'])
+    soup = BeautifulSoup(requests.get("http://www.svtplay.se/ajax/videospager").text)#this call does not work for getting the pages, we use it for the page totals only
+    page_tot = int(soup.find('a',{'data-currentpage':True}).attrs['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)
+        soup = BeautifulSoup(requests.get(base_url).text)
         for article in soup.findAll('article'):
             meta = dict(article.attrs)
-            video = {}
+            video = Video()
             video['title'] = meta['data-title']
             video['description'] = meta['data-description']
             video['url'] = dict(article.find('a').attrs)['href']
@@ -169,6 +193,59 @@ def parse_videolist():
             yield video
         page_num += 1
 
+def remux(video, xml=None):
+    basename = video['filename'].split('.ts')[0]
+    if 'genre' in video:
+        if not os.path.exists(video['genre']):
+            os.mkdir(video['genre'])
+        video['path'] = os.path.join(video['genre'],basename+'.mkv')
+    else:
+        video['path'] = basename+'.mkv'
+    command = ["mkvmerge","-o",video['path'], '--title',video['title']]
+
+    if xml:
+        with open(basename+'.xml','w') as f:
+            f.write(xml)
+            command.extend(['--global-tags',basename+'.xml'])           
+    if 'thumb' in video:
+        with open('thumbnail.jpg','wb') as f: #FIXME use title instead for many downloaders
+            f.write(video['thumb'].read())
+            command.extend(['--attachment-description', "Thumbnail",
+                 '--attachment-mime-type', 'image/jpeg',
+                 '--attach-file', 'thumbnail.jpg'])
+    command.append(video['filename'])
+    print(Popen(command, stdout=PIPE).communicate()[0])
+    for fname in (video['filename'], basename+'.xml','thumbnail.jpg'):
+        try:
+            os.unlink(fname)
+        except:
+            pass
+    
+def mkv_metadata(video):
+    root = BeautifulSoup(features='xml')
+    root.append(Doctype('Tags SYSTEM "matroskatags.dtd"'))
+    tags = root.new_tag("Tags")
+    tag = root.new_tag("Tag")
+    tags.append(tag)
+    root.append(tags)
+    keep = ('title','description', 'url','genre')
+    targets = root.new_tag("Targets")
+    ttv = root.new_tag("TargetTypeValue")
+    ttv.string = str(50)
+    targets.append(ttv)
+    tag.append(targets)
+    for key in video:
+        if not key in keep:
+            continue
+        simple = root.new_tag('Simple')
+        name = root.new_tag('Name')
+        name.string=key.upper()
+        simple.append(name)
+        sstring = root.new_tag('String')
+        sstring.string=video[key]
+        simple.append(sstring)
+        tag.append(simple)
+    return str(root)
 
 if __name__ == "__main__":
     parser = argparse.ArgumentParser()
@@ -177,35 +254,45 @@ if __name__ == "__main__":
     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")
+    parser.add_argument("--no_remux", help="Don't remux into mkv", action="store_true")
+    
     args = parser.parse_args()
     if args.rss: 
+        import feedparser
         d = feedparser.parse(args.rss)
         for e in d.entries:
-            print("Downloading: %s"%e.title)
+            print(("Downloading: %s"%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]
+            video = scrape_player_page({'title':e.title,'url':e.link})
+            if args.no_remux:
+                continue
+            self.remux(video)
         #print(e.description)
     if args.mirror:
+        if not os.path.exists('.seen'):
+            os.mkdir('.seen')
         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" 
+            print(video['title']+'.mkv')
+            print("{} of {}".format(video['num'], video['total']))
+            
+            if os.path.exists(os.path.join('.seen',video['title'])):
+                print("Skipping") 
                 continue
             print("Downloading...")
             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()
+            open(os.path.join('.seen',video['title']),'w').close() #touch
+            video = scrape_player_page(video)
+            if args.no_remux:
+                continue
+            xml = mkv_metadata(video)
+            remux(video, xml)
+            
     else:
         if not args.no_act:
-            video = scrape_player_page(args.url, None)
-        print(u"Downloaded {}".format(args.url))   
\ No newline at end of file
+            video = scrape_player_page({'url':args.url})
+        if not args.no_remux:
+            remux({'title':e.title})
+        print(("Downloaded {}".format(args.url)))