Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A command-line script to find the common tropes of two or more media items. To use, you need to install the python libraries 'pattern' and 'pyquery'. Then use like this, passing in media names or links to tv tropes pages: > python 'My Little Pony Friendship is Magic' 'Hamlet'
# a script to get all the common tropes for media from tv tropes
# usage:
# python name1 name2 [name3...nameN]
# please put names with spaces or special characters in quotes
# you can also pass in the urls if it won't automatch by name.
# pip install pattern
# pip install pyquery
import sys
from pprint import pprint
import re
import urllib
from pattern import web
from pyquery import PyQuery
names = sys.argv[1:]
spider_regex = re.compile(r'[A-Z](To|-)[A-Z]$')
queries = ['#wikitext > ul > li > a:first-child', '#wikitext div > ul > li > a:first-child', '#wikitext > ul > li > ul a:first-child']
trope_urls = {}
def get_tropes_by_url(url):
page = web.URL(url).download()
pq_page = PyQuery(page)
print 'Page title:', pq_page('title').text()
tropes = set()
for query in queries:
if len(tropes) < 3:
for a in pq_page(query):
pq_a = PyQuery(a)
tropes = tropes.union(get_tropes_by_url(pq_a.attr('href')))
trope_urls[pq_a.text()] = pq_a.attr('href')
return tropes
def get_tropes(name):
# TODO: turn name into url somehow
if 'http:' in name:
url = name
url = '{}'.format(urllib.quote('tv tropes ' + name))
print url
return get_tropes_by_url(url)
def trope_intersection(tropes1, tropes2):
return tropes1.intersection(tropes2)
if len(names) > 1:
common_tropes = reduce(trope_intersection, (get_tropes(name) for name in names))
if common_tropes:
print 'Common matches are: {}'.format(len(common_tropes))
for trope in common_tropes:
print '\t', trope #, '\t', trope_urls[trope]
print 'There are no common tropes!'
print 'Please enter enter 2 or more shows/movies/books/etc'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.