-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxkcdwork.py
More file actions
34 lines (27 loc) · 759 Bytes
/
Copy pathxkcdwork.py
File metadata and controls
34 lines (27 loc) · 759 Bytes
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
import json
import zmq
import requests
from lxml import etree
def get_title(url):
"""Given a XKCD url, retrieve the title of the image."""
r = requests.get(url)
tree = etree.HTML(r.content)
title = tree.xpath('//div[@id="comic"]/img/@title')
if title:
return title[0]
else:
return None
context = zmq.Context()
# Socket to receive messages on
receiver = context.socket(zmq.PULL)
receiver.connect("tcp://localhost:5557")
# Socket to send messages to
sender = context.socket(zmq.PUSH)
sender.connect("tcp://localhost:5558")
# Process tasks forever
while True:
s = receiver.recv()
title = get_title(s)
print s, title
# Send a JSON payload to sink
sender.send(json.dumps({"url":s, "title":title}))