forked from 4rsh/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpywiki.py
89 lines (69 loc) · 2.43 KB
/
pywiki.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from sys import argv
from urllib2 import urlopen
import requests
import BeautifulSoup
import os
"""
WARNING:
Make sure you have all the modules imported in this script.
If you think the script useless, do not use.
HowTo:
$ python filename.py --en YourQuery
or
$ python filename.py --en Your_Query
PyWiki - Developed by Arsh Leak. (github.com/4rsh)
"""
script, lang, query = argv
def success(query):
os.system("clear")
print """
\033[0;37m
██████╗ ██╗ ██╗██╗ ██╗██╗██╗ ██╗██╗
██╔══██╗╚██╗ ██╔╝██║ ██║██║██║ ██╔╝██║
██████╔╝ ╚████╔╝ ██║ █╗ ██║██║█████╔╝ ██║
██╔═══╝ ╚██╔╝ ██║███╗██║██║██╔═██╗ ██║
██║ ██║ ╚███╔███╔╝██║██║ ██╗██║
╚═╝ ╚═╝ ╚══╝╚══╝ ╚═╝╚═╝ ╚═╝╚═╝
Developed by \033[1;37mArsh Leak.\033[0;37m (github.com/4rsh)\033[0m
"""
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
def wiki_ok(lang, query):
search = requests.get("http://%s.wikipedia.org/wiki/%s" % (lang, query))
if search.status_code != 200:
print "\033[1;31mError in connection, or your search was not found.\033[0m\n"
else:
conn = BeautifulSoup.BeautifulSoup(search.content)
for detect_class in conn.findAll(attrs={'class':'mw-content-ltr'}):
success(query)
if "may refer to:" in detect_class.p.text:
print "\033[1;31mThe searches were not specified correctly.\033[0m"
else:
reps = {
"[1]" : " ",
"[2]" : " ",
"[3]" : " ",
"[4]" : " ",
"[5]" : " ",
"[6]" : " ",
"[7]" : " ",
"[8]" : " ",
"[9]" : " ",
"," : ", ",
" " : " ",
"." : ". "
}
complete = replace_all(detect_class.p.text, reps)
print "\033[1;37m[ %s ]\n\n\033[0m\033[0;37m%s\n\n\033[1;37mRead more at: http://%s.wikipedia.org/wiki/%s\n \033[0m" % (query, complete, lang, query)
def wiki_error():
print "\n\033[1;37mSomething is wrong, you used:\033[0;37m\n$ python %s --yourtld Query ?\n\033[0m" % script
if "--" in lang:
lang = lang.replace("--", "")
wiki_ok(lang, query)
else:
wiki_error()
# Enjoy, Arsh Leak.