-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiki.py
executable file
·81 lines (74 loc) · 3.02 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
import newspaper
import sys
import subprocess
from bs4 import BeautifulSoup
import urllib2
arguments = sys.argv[1:]
separator = """
"""
#arguments = ["kindle"]
def wikiDisambiguation(linker, titleList):
url = 'http://en.wikipedia.org' + str(linker)
page = urllib2.urlopen(url).read()
soup = BeautifulSoup(page)
tags = soup.find(id='mw-content-text').find_all('a')
for link in soup.find_all('a'):
foundValue = False
title = link.get('title')
if(title is not None):
for arg in titleList.split(" "):
foundValue = foundValue or any(arg.lower() in s.lower() for s in title.split(" "))
if(foundValue):
print("wiki " + title.replace("(", "\(").replace(")", "\)"))
def wikiSug():
entity = "+".join(arguments)
url = 'https://en.wikipedia.org/w/index.php?title=Special%3ASearch&profile=default&search='+entity+'&fulltext=Search'
page = urllib2.urlopen(url).read()
soup = BeautifulSoup(page)
tags = soup.find_all(class_='mw-search-result-heading')
for link in tags:
foundValue = False
title = link.find('a').get('title')
if(title is not None):
if("(disambiguation)" in title):
wikiDisambiguation(link.find('a').get('href'), title.replace(" (disambiguation)", ""))
else:
for arg in arguments:
foundValue = foundValue or any(arg.lower() in s.lower() for s in title.split(" "))
if(foundValue):
print("wiki " + title.replace("(", "\(").replace(")", "\)"))
def wikiSearch():
entity = "_".join(arguments)
url = 'http://en.wikipedia.org/wiki/'+entity
#url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&redirects=&titles='+entity+'&rvprop=content&format=json'
article = newspaper.Article(url, language='en')
article.download()
article.parse()
article.nlp()
printList = []
printList.append("*URL:* " + url)
printList.append(separator)
printList.append("*Title:* "+" ".join(arguments))
printList.append("*Keywords:* " + str(article.keywords))
printList.append(separator)
printList.append("*Summary*")
printList.append(article.text.encode('UTF-8').split("\n")[0])
printList.append(article.summary.encode('UTF-8'))
printList.append(separator)
printList.append("*Wikipedia text*")
printList.append(article.text.encode('UTF-8'))
printList.append(separator)
printList.append("*Images*")
printList.append(str(article.images))
printList.append(separator)
#print("\n".join(printList))
#print(" ".join(["echo", "\n".join(printList), "|", "vim", "-","+/*.*"]))
p1 = subprocess.Popen(["echo", "\n".join(printList)], stdout=subprocess.PIPE)
p2 = subprocess.Popen(["vim", "-","+/*.*"], stdin=p1.stdout)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
output,err = p2.communicate()
if(any("-s" == s.lower() for s in arguments)):
arguments.remove("-s")
wikiSug()
else:
wikiSearch()