-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuoteBot1.py
More file actions
74 lines (56 loc) · 1.8 KB
/
QuoteBot1.py
File metadata and controls
74 lines (56 loc) · 1.8 KB
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
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 16 15:40:25 2017
@author: T
"""
import requests
import html
import markovify
import tweepy
def main():
getquotes(100)
tweet()
def tweet():
#authenticate twitter account
CONSUMER_KEY = '-'
CONSUMER_SECRET = '-'
ACCESS_KEY = '-'
ACCESS_SECRET = '-'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
with open("C:\\path\\to\\text\\file.txt") as f:
text = f.read()
text_model = markovify.Text(text)
api.update_status(text_model.make_short_sentence(140))
print('tweeted')
def cleaner(messy):
"""
spaghetti method to parse out quote from request
"""
start, stop, startfound = -1, -1, 0
for i in range(len(messy)-4):
if messy[i] + messy[i+1] + messy[i+2] == '<p>' and not startfound:
start = i + 3
startfound = 1
if startfound and messy[i] + messy[i+1] + messy[i+2] + messy[i+3] + messy[i+4] == '<\/p>':
stop = i
break
return html.unescape(messy[start:stop])
def getquotes(n):
"""
retrives quotes and parses out useless text
n is number of quotes markov chain will sample from
"""
quotes = []
for i in range(n):
r = requests.get('http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1')
messy = r.text
clean = cleaner(messy)
quotes.append(clean)
print('quote got')
with open("C:\\path\\to\\text\\file.txt", 'w') as f:
for quote in quotes:
f.write(quote + ' ')
if __name__ == "__main__":
main()