Skip to content

Instantly share code, notes, and snippets.

@scturtle
Created March 8, 2012 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scturtle/1999012 to your computer and use it in GitHub Desktop.
Save scturtle/1999012 to your computer and use it in GitHub Desktop.
get subtitles of coursera classes
import os, urllib2
game_cookie = 'csrf_token=XXXX; session=XXXX;'
class Sub_downloader:
def __init__(self, cookie):
self.cookie = cookie # 'csrf_token=XXXX; session=XXXX;'
def getname(self, response):
content_type = response.headers['content-type']
if 'download' not in content_type:
return None # 404
disposition = response.headers['content-disposition']
st,ed = disposition.index('"'), disposition.rindex('"')
return disposition[st+1:ed]
def getsub(self, class_name, lecture_id, lang='en'):
req = urllib2.Request(
'https://www.coursera.org/%s/lecture/subtitles?q=%d_%s'
% (class_name, lecture_id, lang),
headers={'Cookie':self.cookie})
response = urllib2.urlopen(req)
name = self.getname(response)
if not name:
return None # 404
if not os.path.exists(name):
with file(name,'w') as f:
f.write(response.read())
return name
if __name__ == '__main__':
sd = Sub_downloader(game_cookie)
game_lst = [49,12,50,8,9,10,11]
for i in game_lst:
s='%d\t%s' %(i, sd.getsub('gametheory',i))
print s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment