annotate paste/paste.3547 @ 9285:8320c9c4620f

<oerjan> learn Umlaut is German for "hum aloud", an important feature of the German language. It is indicated by putting two dots over the vowel of the syllable.
author HackBot
date Sat, 15 Oct 2016 00:04:47 +0000
parents e037173e0012
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e037173e0012 Initial import.
HackBot
parents:
diff changeset
1 #!/usr/bin/env python
e037173e0012 Initial import.
HackBot
parents:
diff changeset
2
e037173e0012 Initial import.
HackBot
parents:
diff changeset
3 import os
e037173e0012 Initial import.
HackBot
parents:
diff changeset
4 import sys
e037173e0012 Initial import.
HackBot
parents:
diff changeset
5 import json
e037173e0012 Initial import.
HackBot
parents:
diff changeset
6 import urllib2
e037173e0012 Initial import.
HackBot
parents:
diff changeset
7
e037173e0012 Initial import.
HackBot
parents:
diff changeset
8 proxy_handler = urllib2.ProxyHandler({'http': os.environ['http_proxy']})
e037173e0012 Initial import.
HackBot
parents:
diff changeset
9 opener = urllib2.build_opener(proxy_handler)
e037173e0012 Initial import.
HackBot
parents:
diff changeset
10 urllib2.install_opener(opener)
e037173e0012 Initial import.
HackBot
parents:
diff changeset
11
e037173e0012 Initial import.
HackBot
parents:
diff changeset
12 def lose():
e037173e0012 Initial import.
HackBot
parents:
diff changeset
13 print 'You get NOTHING! You LOSE! Good DAY sir!'
e037173e0012 Initial import.
HackBot
parents:
diff changeset
14 sys.exit()
e037173e0012 Initial import.
HackBot
parents:
diff changeset
15
e037173e0012 Initial import.
HackBot
parents:
diff changeset
16 def eels():
e037173e0012 Initial import.
HackBot
parents:
diff changeset
17 print 'My hovercraft is full of eels.'
e037173e0012 Initial import.
HackBot
parents:
diff changeset
18 sys.exit()
e037173e0012 Initial import.
HackBot
parents:
diff changeset
19
e037173e0012 Initial import.
HackBot
parents:
diff changeset
20 if len(sys.argv) > 2:
e037173e0012 Initial import.
HackBot
parents:
diff changeset
21 args = sys.argv[1:]
e037173e0012 Initial import.
HackBot
parents:
diff changeset
22 elif len(sys.argv) == 2:
e037173e0012 Initial import.
HackBot
parents:
diff changeset
23 args = sys.argv[1].split()
e037173e0012 Initial import.
HackBot
parents:
diff changeset
24 else:
e037173e0012 Initial import.
HackBot
parents:
diff changeset
25 lose()
e037173e0012 Initial import.
HackBot
parents:
diff changeset
26
e037173e0012 Initial import.
HackBot
parents:
diff changeset
27 if len(args) == 2:
e037173e0012 Initial import.
HackBot
parents:
diff changeset
28 from_lang = args[0]
e037173e0012 Initial import.
HackBot
parents:
diff changeset
29 to_lang = 'en'
e037173e0012 Initial import.
HackBot
parents:
diff changeset
30 word = args[1]
e037173e0012 Initial import.
HackBot
parents:
diff changeset
31 elif len(args) == 3:
e037173e0012 Initial import.
HackBot
parents:
diff changeset
32 from_lang = args[0]
e037173e0012 Initial import.
HackBot
parents:
diff changeset
33 to_lang = args[1]
e037173e0012 Initial import.
HackBot
parents:
diff changeset
34 word = args[2]
e037173e0012 Initial import.
HackBot
parents:
diff changeset
35 else:
e037173e0012 Initial import.
HackBot
parents:
diff changeset
36 lose()
e037173e0012 Initial import.
HackBot
parents:
diff changeset
37
e037173e0012 Initial import.
HackBot
parents:
diff changeset
38 def query(continue_id):
e037173e0012 Initial import.
HackBot
parents:
diff changeset
39 url = 'http://%s.wikipedia.org/w/api.php?format=json&action=query&' \
e037173e0012 Initial import.
HackBot
parents:
diff changeset
40 'redirects=1&titles=%s&prop=langlinks' % (from_lang, word)
e037173e0012 Initial import.
HackBot
parents:
diff changeset
41 if continue_id:
e037173e0012 Initial import.
HackBot
parents:
diff changeset
42 url += '&llcontinue=' + continue_id
e037173e0012 Initial import.
HackBot
parents:
diff changeset
43 try:
e037173e0012 Initial import.
HackBot
parents:
diff changeset
44 response = urllib2.urlopen(url).read()
e037173e0012 Initial import.
HackBot
parents:
diff changeset
45 except urllib2.URLError, e:
e037173e0012 Initial import.
HackBot
parents:
diff changeset
46 print e.reason
e037173e0012 Initial import.
HackBot
parents:
diff changeset
47 sys.exit()
e037173e0012 Initial import.
HackBot
parents:
diff changeset
48 return json.loads(response)
e037173e0012 Initial import.
HackBot
parents:
diff changeset
49
e037173e0012 Initial import.
HackBot
parents:
diff changeset
50 continue_id = None
e037173e0012 Initial import.
HackBot
parents:
diff changeset
51 while True:
e037173e0012 Initial import.
HackBot
parents:
diff changeset
52 q = query(continue_id)
e037173e0012 Initial import.
HackBot
parents:
diff changeset
53 if '-1' in q['query']['pages']:
e037173e0012 Initial import.
HackBot
parents:
diff changeset
54 eels()
e037173e0012 Initial import.
HackBot
parents:
diff changeset
55 page = q['query']['pages'].values()[0]
e037173e0012 Initial import.
HackBot
parents:
diff changeset
56 if 'langlinks' not in page:
e037173e0012 Initial import.
HackBot
parents:
diff changeset
57 eels()
e037173e0012 Initial import.
HackBot
parents:
diff changeset
58 for link in q['query']['pages'].values()[0]['langlinks']:
e037173e0012 Initial import.
HackBot
parents:
diff changeset
59 if link['lang'] == to_lang:
e037173e0012 Initial import.
HackBot
parents:
diff changeset
60 print link['*'].encode('utf-8')
e037173e0012 Initial import.
HackBot
parents:
diff changeset
61 sys.exit()
e037173e0012 Initial import.
HackBot
parents:
diff changeset
62 if 'query-continue' in q:
e037173e0012 Initial import.
HackBot
parents:
diff changeset
63 continue_id = q['query-continue']['langlinks']['llcontinue']
e037173e0012 Initial import.
HackBot
parents:
diff changeset
64 else:
e037173e0012 Initial import.
HackBot
parents:
diff changeset
65 eels()