-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbashQuotes.py
More file actions
145 lines (124 loc) · 3.08 KB
/
Copy pathbashQuotes.py
File metadata and controls
145 lines (124 loc) · 3.08 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
"""Summary
"""
import pytz
from datetime import datetime
from datetime import timedelta
import random
import lxml
from bs4 import BeautifulSoup
import urllib
import urllib2, urllib
import collections
import re
def get_post(post_number=-1):
"""Summary
Args:
post_number (TYPE, optional): Description
Returns:
TYPE: Description
"""
if post_number == -1:
return random_quote()
elif post_number == 'top':
return rand_top()
elif not represents_int(post_number):
return getKewordPost(post_number)
elif represents_int(post_number):
return getSpecificPost(post_number)
def getKewordPost(keywords):
"""Summary
Args:
keywords (TYPE): Description
Returns:
TYPE: Description
"""
query = keywords
query = urllib.quote_plus(query)
url = 'http://bash.org/?search=' +query +'&sort=0&show=25'
#content = urllib2.urlopen(url).readlines()
#print content
r = urllib.urlopen(url).read()
#print r
soup = BeautifulSoup(r, "lxml")
mydivs = soup.findAll("p", {"class" : "qt"})
for row in mydivs:
return row.text.encode('utf-8')
return "No quote found matching that search term..."
def random_quote():
"""Summary
Returns:
TYPE: Description
"""
url = 'http://www.bash.org/?random'
#content = urllib2.urlopen(url).readlines()
#print content
r = urllib.urlopen(url).read()
#print r
soup = BeautifulSoup(r, "lxml")
mydiv = soup.find("p", {"class" : "qt"})
return mydiv.text
def getSpecificPost(post):
"""Summary
Args:
post (TYPE): Description
Returns:
TYPE: Description
"""
url = 'http://www.bash.org/?' + str(post)
#content = urllib2.urlopen(url).readlines()
#print content
r = urllib.urlopen(url).read()
#print r
soup = BeautifulSoup(r, "lxml")
mydivs = soup.findAll("p", {"class" : "qt"})
for row in mydivs:
return row.text.encode('utf-8')
return "That quote does not exist..."
def rand_top():
"""Summary
Returns:
TYPE: Description
"""
if random.random() < .5:
page = 1
else:
page = 2
url = 'http://bash.org/?top' + str(page)
#content = urllib2.urlopen(url).readlines()
#print content
r = urllib.urlopen(url).read()
#print r
soup = BeautifulSoup(r, "lxml")
mydivs = soup.findAll("p", {"class" : "qt"})
rand_top_post = random.randint(0, 99)
return mydivs[rand_top_post].text.encode('utf-8')
def get_HTML(url):
"""Summary
Args:
url (TYPE): Description
Returns:
TYPE: Description
"""
#content = urllib2.urlopen(url).readlines()
#print content
return urllib.urlopen(url).read()
#print r
def represents_int(value):
"""Summary
Args:
s (TYPE): Description
Returns:
TYPE: Description
"""
try:
int(value)
return True
except ValueError:
return False
def main():
"""Summary
"""
print getKewordPost('twitter')
if __name__ == '__main__':
main()