Debian Codegraph @

resources/scripts/scrapers/funnyjunk-embed-gifs.py

raw symbols SCIP
1 # Embeds GIFs into individual messages from Funnyjunk.com
2 # Sample input file whose contents must be provided as stdin: "https://funnyjunk.com/rss/most_popular.rss"
3
4 import json
5 import re
6 import sys
7 import urllib.request
8 import xml.etree.ElementTree as ET
9 from datetime import datetime
10
11 input_data = sys.stdin.read()
12
13 tree = ET.fromstring(input_data)
14 pattern = re.compile("href=\"(https://[^<>]+\.(gif|png|jpg))\"")
15
16 for ite in tree.find("channel").iter("item"):
17 link = ite.find("link").text
18 #print(link)
19 response = urllib.request.urlopen(link)
20 text = response.read().decode("utf-8")
21 for pic_link in re.findall(pattern, text):
22 new = ET.SubElement(ite, "enclosure")
23 new.set("url", pic_link[0])
24 new.set("type", "image/" + pic_link[1])
25 #print(ET.tostring(ite, encoding="unicode"))
26
27 print(ET.tostring(tree, encoding="unicode"))