/
wiki.py
115 lines (111 loc) · 4.63 KB
/
wiki.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
import wikipedia
from flask import Flask, request, redirect
from creoleparser import text2html
from yahoo.search.news import NewsSearch
from url_shortener import UrlShortener
from threading import Lock
import sys
import duckduckgo
from duckduckgo import Result
from duckduckgo import query as Q
reload(sys)
lock = Lock()
THREADS = 20
sys.setdefaultencoding("utf-8")
app = Flask(__name__)
@app.route('/<query>')
def wsearch(query, methods=['POST', 'GET']):
try:
print query
head = str(query)
style = '''<head>
<link rel="stylesheet" type="text/css" href="wiki.css">
</head>'''
title = "<title>{0}</title>".format(str(head))
result = Q(query)
uri = result.abstract.url
page = wikipedia.page(title=query)
content = text2html(page.content)
for elem in page.links:
refrence = "<a href=http://localhost:5000/{0}><center><br>{0}</center></body></a>".format(elem)
summary = "<body><center>{0}</center></body>".format(content)
us = UrlShortener()
url = UrlShortener.shorten(us, page.url)
home = str("/")
re = "<br><center><a href={0}>Home</a></center>".format(home)
body = "<br><center><a href={0}>Random</a></center>".format(str('{success:'))
link = "<br><center><a href={0}>WikiPage</a></center>".format(uri)
wiki = str(style + title + "\n" + "<br>"+ summary + "\n" + body + "\n" + link + re + refrence + "<center>" + "</center>")
except wikipedia.DisambiguationError:
print "\n"
try:
head = wikipedia.random(pages=1)
style = '''<head>
<link rel="stylesheet" type="text/css" href="wiki.css">
</head>'''
title = "<title>{0}</title>".format(str(head))
page = wikipedia.page(title=head)
content = text2html(page.content)
result = duckduckgo.query(head)
uri = result.abstract.url
summ = wikipedia.summary(str(head))
summary = "<body><center><div class='page'>{0}</div><center><body>".format(content)
for elem in page.links:
refrence = "<a href=http://localhost:5000/{0}><center><br>{0}</center></body></a>".format(elem)
us = UrlShortener()
url = UrlShortener.shorten(us, page.url)
home = str("/")
re = "<br><a href={0}>Home</a>".format(home)
body = "<br><a href={0}>Random</a>".format(str('{success:'))
link = "<br><a href={0}>WikiPage</a>".format(uri)
wiki = str(style + title + "\n" + summary + "\n" + body + "\n" + link + re + refrence)
return wiki
except wikipedia.DisambiguationError, UnicodeEncodeError:
head = wikipedia.random(pages=1)
style = '''<head>
<link rel="stylesheet" type="text/css" href="wiki.css">
</head>'''
title = "<title>{0}</title>".format(str(head))
page = wikipedia.page(title=head)
content = text2html(page.content)
result = duckduckgo.query(head)
uri = result.abstract.url
summ = wikipedia.summary(str(head))
summary = "<body><center><div class='page'>{0}</div><center><body>".format(content)
for elem in page.links:
refrence = "<a href=http://localhost:5000/" + elem + "><center><br>{0}</center></body></a>".format(elem)
us = UrlShortener()
url = UrlShortener.shorten(us, page.url)
home = str("/")
re = "<br><a href={0}>Home</a>".format(home)
body = "<br><a href={0}>Random</a>".format(str('{success:'))
link = "<br><a href={0}>WikiPage</a>".format(uri)
wiki = str(style + title + "\n" + summary + "\n" + body + "\n" + link + refrence)
return wiki
@app.route('/', methods=['GET', 'POST'])
def index():
return '''
<title>Wiki Rest API</title>
<link rel="stylesheet" type="text/css" href="wiki.css">
<body>
<b><center>Welcome to WikiRest Api</center></b>
<b><center>About <a href="/wiki">Wiki</a></center></b>
<b><center>Random Page <a href="/'{success':'">Random</a>
<script>
function makeid()
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
document.cookie=makeid();
</script>
</body>
'''
if __name__ == "__main__":
try:
app.run()
except UnicodeEncodeError:
print "\n"