Virtua Computers > Applescript, Python and codef00

Applescript, Python and codef00

Posted by Justin on Dec 26, 2010, Categories: Apple Stuff, Misc

0

Thanks to Evan at CodeF00, we were able to take Nigel and have him get the movie times from Fandango and tell me what is playing. We plugged this into Dragon Dictate and now make the command “Nigel Movies”. Check it out after the jump.

say "What time do you want to see a movie?"
tell application "Dragon Dictate"
	set microphone to dictation
end tell
set timeparam to text returned of (display dialog "What time?" default answer "")
tell application "Dragon Dictate"
	set microphone to command operation
end tell
do shell script "python /location_to/scripts/Movie-Listings.py " & "'" & timeparam & "'"

When you say to Nigel – Nigel movies, he asks what time you want – state it in “o’clock” – i.e. say eleven o’clock so it inputs “11:00 PM”. The script below will read all movies within of the 11:00 variable (so 10:30pm-11:30pm)

and here is the python code in the Movie-Listings.py script:

#!/usr/bin/python
import urllib2
import BeautifulSoup
import re
import HTMLParser
import subprocess
import sys
import time
import datetime

window = 30

if len(sys.argv) == 2:
	time_string = sys.argv[1]
	t = time.strptime(time_string, '%I:%M %p')
	t = t.tm_hour * 60 + t.tm_min
else:
	t = None

html_parser = HTMLParser.HTMLParser()

theater_max = 3
url='http://www.fandango.com/10028_movietheatershowtimes'
f = urllib2.urlopen(url)
html = f.read()
soup = BeautifulSoup.BeautifulSoup(html)

divs = soup.findAll('div', { "class" : re.compile("theater .*") })
i=0
for theater in divs:
	i += 1
	if i > 3:
		break

	address = html_parser.unescape(theater.find('p').contents[0])
	name = theater.find('h3').find('a').contents[0]

	item = theater.findNextSibling('ul')
	said_playing = False

	for title_div in item.findAll('div', {"class":"title"} ):
		title = title_div.h4.a.contents[0].strip()
		times_div = title_div.findNextSibling('div')
		times_ul = times_div.ul

		times = []

		for time_li in times_ul.findAll('li', {"class":re.compile("^showtime_itr") }):
			show_time = time_li.a.span.contents[0].strip()
			if not show_time.endswith('am'):
				show_time = show_time + ' PM'
			else:
				show_time = show_time.replace('am', ' AM')

			show_time_obj = time.strptime(show_time, '%I:%M %p')

			if show_time_obj.tm_hour < 1:
				show_time_d = (24 * 60) + show_time_obj.tm_min
			else:
				show_time_d = show_time_obj.tm_hour * 60 + show_time_obj.tm_min

			if (t is None) or (abs(show_time_d - t) < window):
				times.append(show_time)

		if not said_playing and len(times) > 0:
			subprocess.call(["say", "Now playing at " + name])
			said_playing = True

		if len(times) > 0:
			subprocess.call(["say", title + " at " + ', '.join(times)])

Just change the URL from my zipcode to yours. Also, if you are running this on OS X – download the BeautifulSoup python library and keep it in the same directory as the Movie-listings script.

We have it say just the 3 closest movie theaters, but you can edit as you like.

Have fun @ the movies and don’t forget the popcorn!!!

Blog Posts

Apple Hot News

Twitter Updates