Skip to content

Instantly share code, notes, and snippets.

@Aldaviva
Last active December 16, 2015 07:59
Show Gist options
  • Save Aldaviva/5402970 to your computer and use it in GitHub Desktop.
Save Aldaviva/5402970 to your computer and use it in GitHub Desktop.
Film Quotation Completion

Given a film, an incomplete quotation from the film, and multiple-choice completions for the quotation, which completion is from the film?

Film: RoboCop
Question: Come quietly or there will be ______.
Possible answers: fisticuffs, a strongly worded letter, trouble, an altercation
Best answer: trouble

Letterboxd poses these questions as captchas for their sign-up flow.

This solution makes unauthenticated requests to Google's Ajax Search service, which is deprecated and throttled, so a real attack would require a different method of scoring results (like a paid API account, a different search engine, or a botnet).

import re
import requests
def get_result_count(query):
search_res = requests.get('https://ajax.googleapis.com/ajax/services/search/web', params={'v': '1.0', 'q': query})
result_count = search_res.json()['responseData']['cursor']['estimatedResultCount']
return int(result_count)
res = requests.get('http://letterboxd.com/create-account/')
page_source = res.text
question = re.search(u'<p class="captcha-quote">\u201C(.+)\u201D</p>', page_source).group(1)
answers = re.findall('<input type="radio" name="answer" class="radio" id="answer-\\d" value="(.+)">', page_source)
film_title = re.search('<span class="frame-title">(.+)</span>', page_source).group(1)
print "Film: " + film_title
print "Question: " + question
print "Possible answers: " + ", ".join(answers)
best_answer = {"name": "none", "score": 0}
for answer in answers:
query = re.sub('_+', answer, question)
result_count = get_result_count(query)
if result_count > best_answer['score']:
best_answer['score'] = result_count
best_answer['name'] = answer
print "Best answer: " + best_answer['name']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment