-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscript.py
61 lines (55 loc) · 1.87 KB
/
script.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
import sys
import requests
from bs4 import BeautifulSoup
from time import sleep
from App import *
def is_valid_syntax(argv):
if len(sys.argv) < 2:
sys.stderr.write('Usage: python script.py <tags with spaces in between> \n')
sys.stderr.write('Ctrl-C to close the program \n')
return False
else:
return True
def is_valid_tag(tag):
url = "http://stackoverflow.com/feeds/tag?tagnames=%s&sort=newest" % tag
r = requests.get(url)
if r.status_code == 404:
sys.stderr.write('Please check the tag(s) %s \n' % tag)
sys.stderr.write('The following url should not return 404: \n')
sys.stderr.write('http://stackoverflow.com/feeds/tag?tagnames=%s&sort=newest \n' % tag)
sys.stderr.write('Processing other tag(s)... \n')
return False
else:
return True
def get_question_from_tag(tag, last_ques):
url = "http://stackoverflow.com/feeds/tag?tagnames=%s&sort=newest" % tag
r = requests.get(url)
while r.status_code is not 200:
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")
data = soup.find_all("link")
qurl = data[2].get('href')
print qurl
question = qurl[qurl.find('questions') + 19:]
if last_ques[str(tag)] != str(question):
app = App(tag.upper() + '\n' + qurl)
def main(argv):
if is_valid_syntax(argv):
try:
tags = []
last_ques = {}
for i in sys.argv[1:]:
if is_valid_tag(i):
tags.append(i)
last_ques[str(i)] = ' '
while True:
for tag in tags:
get_question_from_tag(tag, last_ques)
sleep(60)
except KeyboardInterrupt:
sys.stderr.write('\nBye \n')
return True
else:
return True
if __name__ == "__main__":
sys.exit(main(sys.argv))