-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhttpRequester.py~
96 lines (84 loc) · 2.63 KB
/
httpRequester.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from httplib import HTTPConnection
from requests import data
import pika
from RocketBooster.F1Engine.J2Engine import globalVars
import logging
import sys
from threading import Thread
import time
def callback(ch, method, properties, body):
print " [x] Received %r" % (body,)
handleMessage(body)
def notifications():
logging.basicConfig()
connection = pika.BlockingConnection(pika.ConnectionParameters(
host=globalVars.host))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_consume(callback, queue="hello", no_ack=True)
channel.start_consuming()
def parseUrl(url):
urlPieces = url.split("://")
host = urlPieces[1].partition('/')[0]
url = urlPieces[1].partition('/')[1] + urlPieces[1].partition('/')[2]
return (host, url)
def handleMessage(msg):
url = parseUrl(msg)
if "New" not in msg.split("://")[0]:
return
comicId = getComicId(url[0])
httpRequest(url[0], url[1], comicId)
latest = getLatest(url[0])
if len(latest) == 0:
with open("missed.txt", 'a+') as f:
f.write("missed: " + msg)
print "Bad Notification:", msg
invalidNotif(url[0], url[1], msg)
elif len(latest) > 1:
with open("missed.txt", 'a+') as f:
f.write("extra: " + msg)
for l in latest:
url = parseUrl(l)
httpRequest(url[0], url[1], comicId)
def getComicId(host):
with open("/usr/local/bin/comics.ids") as f:
data = f.read()
data = eval(data)
if host:
return data[host]
return data
def getLatest(host, url=None):
latest = []
new = (url == None)
with open("/usr/local/bin/" + host + ".list") as f:
for line in f:
if new:
latest.append(line.strip())
if url == line.strip():
new = True
return latest
def httpRequest(host, url, comicId):
conn = HTTPConnection(host, 81)
conn.request("GET", url, headers = {"comicID" : comicId})
#r = conn.getresponse()
def invalidNotif(host, url, msg):
conn = HTTPConnection(host, 81)
conn.request("POST", url, headers = {"url" : msg, "crFunction" : "invalidNotification"})
#r = conn.getresponse()
def getCaughtUp():
hosts = getComicId(None)
for h in hosts:
latest = getLatest(h)
for l in latest:
url = parseUrl(l)
if url:
httpRequest(url[0], url[1], hosts[h])
time.sleep(.1)
if __name__ == "__main__":
t = Thread(target=getCaughtUp, args=())
t.start()
try:
notifications()
except KeyboardInterrupt:
t.join()
sys.exit()