view bin/wl @ 2651:4c617ce2bbe1

<Jafet> echo -e \'\\x7fELF\\x02\\x01\\x01\\0\\0\\0\\0\\0\\0\\0\\0\\0\\x02\\0>\\0\\x01\\0\\0\\0x\\0@\\0\\0\\0\\0\\0@\\0\\0\\0\\0\\0\\0\\0\\xb0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0@\\08\\0\\x01\\0@\\0\\x03\\0\\x02\\0\\x01\\0\\0\\0\\x05\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0@\\0\\0\\0\\0\\0\\0\\0@\\0\\0\\0\\0\\0\\x9a\\0\\0\\0\\0\\0\\0\\0\\x9a\\0\\0\\0\\0\\0\\0\\0\\0\\0 \\0\\0\\0\\0\\0H\\xc7\\xc7\\x01\\0\\0\\0H\\xc7\\xc6\\x98\\0@\\0H\\xc7\\xc2\\x02\\0\\0\\0H\\xc7\\xc0\\x01\\0\\0\\0\\x0f\\x05\\xeb\\xf5y\' > bin/y && chmod +x bin/y
author HackBot
date Sat, 13 Apr 2013 22:29:43 +0000
parents e037173e0012
children
line wrap: on
line source

#!/usr/bin/env python

import os
import sys
import json
import urllib2

proxy_handler = urllib2.ProxyHandler({'http': os.environ['http_proxy']})
opener = urllib2.build_opener(proxy_handler)
urllib2.install_opener(opener)

def lose():
    print 'You get NOTHING! You LOSE! Good DAY sir!'
    sys.exit()

def eels():
    print 'My hovercraft is full of eels.'
    sys.exit()

if len(sys.argv) > 2:
    args = sys.argv[1:]
elif len(sys.argv) == 2:
    args = sys.argv[1].split()
else:
    lose()

if len(args) == 2:
    from_lang = args[0]
    to_lang = 'en'
    word = args[1]
elif len(args) == 3:
    from_lang = args[0]
    to_lang = args[1]
    word = args[2]
else:
    lose()

def query(continue_id):
    url = 'http://%s.wikipedia.org/w/api.php?format=json&action=query&' \
        'redirects=1&titles=%s&prop=langlinks' % (from_lang, word)
    if continue_id:
        url += '&llcontinue=' + continue_id
    try:
        response = urllib2.urlopen(url).read()
    except urllib2.URLError, e:
        print e.reason
        sys.exit()
    return json.loads(response)

continue_id = None
while True:
    q = query(continue_id)
    if '-1' in q['query']['pages']:
        eels()
    page = q['query']['pages'].values()[0]
    if 'langlinks' not in page:
        eels()
    for link in q['query']['pages'].values()[0]['langlinks']:
        if link['lang'] == to_lang:
            print link['*'].encode('utf-8')
            sys.exit()
    if 'query-continue' in q:
        continue_id = q['query-continue']['langlinks']['llcontinue']
    else:
        eels()