Last active
August 29, 2015 13:56
-
-
Save pandada8/8860639 to your computer and use it in GitHub Desktop.
163 Open Course downloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import requests | |
from bs4 import BeautifulSoup as bs | |
import re | |
import xmlrpc.client | |
import sys | |
import xml.etree.ElementTree as et | |
""" | |
Usage: python 163.py <url of the content> | |
Dependency:python-requests BeautifulSoup and a running daemon-mode Aria2 in localhost | |
""" | |
a = xmlrpc.client.Server('http://127.0.0.1:6800/rpc') | |
def getClasses(url): | |
soup = bs(requests.get(url).text) | |
name = soup.find(class_='pos').text | |
return name,[(''.join(i.text.split()),i.a['href']) for i in soup.find(id='list2').findAll('td',class_='u-ctitle')] | |
def parseVideo(url): | |
cid,mid = re.findall(r'[0-9A-Z]{9}',url) | |
url = 'http://live.ws.126.net/movie/{mid[7]}/{mid[8]}/2_{cid}_{mid}.xml'.format(cid=cid,mid=mid) | |
# print(url) | |
xml = requests.get(url).content.decode("GBK",'IGNORE') | |
# FIXME: here is a workround for the GBK encoding of the xml file | |
soup = bs(xml) | |
video_url = soup.find('flvurlorigin').text if soup.find('flvurlorigin').text else soup.find('flvurl').text | |
if soup.find('subs'): | |
sub_url = [(i.find('name').text,i.url.text)for i in soup.findAll('sub')] | |
else: | |
sub_url = None | |
return video_url,sub_url | |
def downloadFile(url,filename): | |
option = {'max-connection-per-server':20,'split':10,'continue':True,'pause':True} | |
option['out'] = filename | |
a.aria2.addUri([url],option) | |
def main(): | |
url = sys.argv[1] | |
name,clist = getClasses(url) | |
for mname,mlink in clist: | |
vlink,sub_urls = parseVideo(mlink) | |
if sub_urls: | |
for i,j in sub_urls: | |
subname = "{}/{}-{}.srt".format(name,mname,i) | |
downloadFile(j,subname) | |
mname = '{}/{}.flv'.format(name,mname) | |
downloadFile(vlink,mname) | |
print(mname,vlink) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment