-
Notifications
You must be signed in to change notification settings - Fork 0
/
slugwikiutils.py
56 lines (50 loc) · 2.29 KB
/
slugwikiutils.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
#This function gets a quote from slugwiki
import mwclient
import random
class SlugWikiUtils:
url = "slugwiki.mit.edu"
quotesPage='Slug_Quotes'
username="button711"
password=""
page711="711_button"
def __init__(self):
useragent = 'YourBot, based on mwclient v0.7.2. Run by User: Ivan [email protected]'
self.site = mwclient.Site(('http', self.url), clients_useragent=useragent, path ='/')
if self.password!="":
self.site.login(self.username, self.password)
def getRandomQuote(self):
page = self.site.Pages[self.quotesPage]
pageText=page.text()
sentences=page.text().split('\n')
numberOfSentences=len(sentences)
people= [k for k in sentences if "'''"in k] #Filter sentences that have ''' are the ones with names of people
numberOfPeople=len(people)
randomPersonIndex = random.randrange(numberOfPeople)
randomPerson = people[randomPersonIndex]
randomPersonSentenceIndex=sentences.index(randomPerson)
if randomPersonIndex<(numberOfPeople-1):
nextPerson=people[randomPersonIndex+1]
nextRandomPersonSentenceIndex = sentences.index(nextPerson)
else:
nextRandomPersonSentenceIndex=numberOfSentences #removes last line
if (randomPersonSentenceIndex+1<nextRandomPersonSentenceIndex):
for i in range(5): #try 5 times to get a non empty index
randomSentenceIndex = random.randrange(randomPersonSentenceIndex+1, nextRandomPersonSentenceIndex)
randomSentence= sentences[randomSentenceIndex]
if randomSentence != "":
break
else:
randomPerson=""
for i in range(5): #try 5 times to get a non empty index
randomSentenceIndex = random.randrange(len(sentences))
randomSentence= sentences[randomSentenceIndex]
if randomSentence != "":
break
return [randomPerson,sentences[randomSentenceIndex]]
def getQuotesText(self):
page = self.site.Pages[self.quotesPage]
return page.text()
def addTo711(self, text):
page = self.site.Pages[self.page711]
pageText=page.text()
page.save(pageText + u'<br>' + text, summary = '711')