forked from AnirudhBhat/cliWiki.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcliwiki.py
executable file
·183 lines (129 loc) · 3.96 KB
/
cliwiki.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#! /usr/bin/env python
from __future__ import print_function
import json
import urllib2
import sys
import re
KEY = 0
base_url = "http://en.wikipedia.org/w/api.php?"
action = "action=query"
Format = "&format=json"
titles="&titles="
def welcome_message():
print("Commond Line Wikipedia")
def get_title():
title = raw_input('enter the title you want to search:\n')
title = title.replace(' ','_')
global titles
titles = titles + title
def url_and_displaytitle():
print('\ntitle and url for this wikipedia site',end="\n")
global base_url
global action
global titles
global Format
prop = "&prop=info"
inprop = "&inprop=url|displaytitle"
url = base_url + action + titles + prop + inprop + Format
result = json.load(urllib2.urlopen(url))
key = result['query']['pages'].keys()
global KEY
KEY = (key[0][:])
print(result['query']['pages'][str(KEY)]['title'])
print(result['query']['pages'][str(KEY)]['fullurl'])
print('\t-------------------\t')
def interesting_links():
print('\nyou may also be interested in the following links',end="\n")
global base_url
global Format
global action
global titles
prop = "&prop=extlinks"
try:
url = base_url + action + titles + prop + Format
result =json.load(urllib2.urlopen(url))
key = result['query']['pages'].keys()
key = key[0][0:]
j = 0
offset = result['query-continue']['extlinks']['eloffset']
while j < offset:
print(result['query']['pages'][str(key)]['extlinks'][j])
j=j+1
except:
print('sorry,couldn\'t find any links')
#def interwiki_links():
# print('inter wiki links found for this search',end="\n")
# base_url
# action
# titles
# prop = "&prop=iwlinks"
# url = base_url + action + titles + prop
# print(url)
# result = urllib2.urlopen(url)
# for i in result:
# print(i)
def wiki_search():
global base_url
global action
global titles
global Format
prop = "&prop=extracts"
plaintext = "&explaintext"
section_format = "&exsectionformat=plain"
try:
url = base_url + action + titles + prop + plaintext + section_format + Format
result = json.load(urllib2.urlopen(url))
key = result['query']['pages'].keys()
key = key[0][0:]
print(result['query']['pages'][str(key)]['extract'],end="\n")
except:
print('oops!,no wikipedia page for that title.Wikipedia search titles are case Sensitive...')
def images():
print('\nall images related to this search',end="\n")
image_url = "http://en.wikipedia.org/wiki/"
global base_url
global Format
global action
global titles
prop = "&prop=images"
url = base_url + action + titles + prop + Format
result = json.load(urllib2.urlopen(url))
key = result['query']['pages'].keys()
key = key[0][0:]
try:
i = 1
while(i):
Image = str(result['query']['pages'][str(key)]['images'][i]['title'])
image = image_url + Image.replace(' ','_')
print(image)
i=i+1
except:
print('\t------------------\t',end="\n")
pass
def featured_feed():
global base_url
Format = "&format=json"
action = "&action=featuredfeed"
try:
feed = "&feed=" + str(sys.argv[1])
url = base_url + action + feed + Format
print(url)
result = urllib2.urlopen(url).read()
res1 = re.compile('<title>(.*)</title>')
res2 = re.compile('<link>(.*)en</link>')
Result1 = re.findall(res1,result)
Result2 = re.findall(res2,result)
for i in enumerate(zip(Result1,Result2)):
print(i)
except:
print('error!')
if len(sys.argv) < 2:
welcome_message()
get_title()
wiki_search()
url_and_displaytitle()
images()
#interwiki_links()
interesting_links()
else:
featured_feed()