TED Talks Download Subtitles

Fecha: January 5th, 2010 | Categoría: Internet | 21 Comments »

UPDATE: Online version

Go to the Online version
This is what I've been working on today. It's a simple console-based script to download subtitles for TED Talks - since I haven't found a way to download them directly from the web in a compatible format (I generally use '.srt' subtitles). Here is the script made in python. TEDTalkSubtitles.py

Key parts of the program:

A simple function to parse the value in miliseconds to something like "00:34:32,334":

  1. def getFormatedTime(intvalue):
  2.     mils = intvalue%1000
  3.     segs = (intvalue/1000)%60
  4.     mins = (intvalue/60000)%60
  5.     hors = (intvalue/3600000)
  6.     return "%02d:%02d:%02d,%03d"%(hors,mins,segs,mils)

With this recursive function, fetch available languages for the talk

  1. def availableSubs(subs):
  2.     a = subs.find("LanguageCode")
  3.     if a == -1:
  4.         return []
  5.     subs = subs[a+len("LanguageCode"):]
  6.     return [re.search("%22([^A-Z]+)%22", subs).group(1)] + availableSubs(subs)

Get information about the video

  1. def getVideoParameters(urldirection):
  2.     ht = urllib.urlopen(urldirection).read()
  3.     var = re.search('flashVars = {\n([^}]+)}', ht)
  4.     if var:
  5.         var = var.group(1)
  6.     else:
  7.         return None
  8.     var = [a.replace('\t', '') for a in var.split('\n')]
  9.     for a in range(len(var)):
  10.         if var[a]:
  11.             var[a] = var[a][:var[a].rfind(',')]
  12.     resultado = []
  13.     for a in var:
  14.         l = a.find(':')
  15.         if l != -1:
  16.             resultado.append((a[:l], a[l+1:]))
  17.     return dict(resultado)

Getting it all together:

  1. def downloadSub(idtalk, lang, timeIntro):
  2.     print("Downloading subtitles for language %s"%lang)
  3.     c = simplejson.load(urllib.urlopen('http://www.ted.com/talks/subtitles/id/%s/lang/%s'%(idtalk, lang)))
  4.     salida = file('subs_%s_%s.srt'%(idtalk,lang), 'w')
  5.     conta = 1
  6.     c = c['captions']
  7.     for linea in c:
  8.         salida.write("%d\n"%conta)
  9.         conta += 1
  10.         salida.write("%s --> %s\n"%(getFormatedTime(timeIntro+linea['startTime']), getFormatedTime(timeIntro+linea['startTime']+linea['duration'])))
  11.         salida.write("%s\n\n"%(linea['content'].encode('utf-8')))
  12.     salida.close()

Related to:
Parsing and Converting TED Talks JSON Subtitles
Download subtitles from TED talks for offline viewing


Flamewar Pyglet vs. Pygame

Fecha: December 9th, 2009 | Categoría: Python | 2 Comments »

Historial de conversación con Pablo Costesich:

eordano:
Serie de posts "Programas sencillos en Pygame" en mi blog http://bit.ly/64oH6I http://bit.ly/60OX6f http://bit.ly/7M0xiW #python #pygame

pcostesi:
@eordano No me andan los links por algún motivo extraño, pero los pude ver en tu blog. Fuera de Pyglet vs. Pygame, ¡muy buenos ejemplos!

eordano:
@pcostesi gracias! che, querés postear como guest en mi blog acerca de pyglet? #flamewar #pygame #pyglet

pcostesi:
@eordano Podría, ahora hago alguna app :P

eordano:
@pcostesi y? que pasó? estoy esperando esa app xD

pcostesi:
@eordano Salimos con mi viejo que hace mucho que no lo veía. Además, no pensé sobre qué puedo escribir :P

eordano:
@pcostesi jaja hacete el juego de la vida!

pcostesi:
@eordano Genial idea, ahora me pongo a leer las reglas :P
@eordano Lo prometido es deuda - http://pastebin.com/m5d34142a
game of life, versión 0.2: http://python.pastebin.com/f487eba21 #python #gameoflife #meganoelorgullo #estebanmedesafio #elcodigoeshorrible

Resumiendo:

Para mostrar las diferencias entre pygame y pyglet, Pablo se programó en muy pocas líneas de código, Conway's Game of Life. El código se puede encontrar aquí, en pastebin.

Update: Link al post de Pablo en su blog.


Más de cosas hechas en Python y PyGame

Fecha: December 7th, 2009 | Categoría: Python | No Comments »

En este caso, en Física 1 (ya escribí algo sobre las clases de Física en este post), el profesor escribe una fórmula y dice "bueno, con esto se puede simular el perfil de velocidades de una barra que está inclinada cuyo punto inferior tiene una determinada velocidad dada".

Y bueno, quería ver cómo se veía:

Gráfica interactiva del perfil de velocidades de una barra

Un script programado en Python y Pygame, hosteado en Pastebin


Rápido Sketching de programas en Pygame II

Fecha: December 4th, 2009 | Categoría: Python | 1 Comment »

Acá hay código de otros programas que sketcheé rápidamente (follow-up de este post)
Read the rest of this entry »