Debian Codegraph @

resources/scripts/scrapers/piped-fix.py

raw symbols SCIP
1 # Fixes Piped ATOM feeds.
2 #
3 # You must provide raw ATOM feed XML data as input, for example with curl:
4 # curl 'https://pipedapi.kavin.rocks/feed/unauthenticated/rss?channels=UCXuqSBlHAE6Xw-yeJA0Tunw' | python ./piped-fix.py
5
6 import json
7 import sys
8 import urllib.request
9 import xml.etree.ElementTree as ET
10
11 # Globals.
12 atom_ns = {"atom": "http://www.w3.org/2005/Atom"}
13
14 def main():
15 sys.stdin.reconfigure(encoding="utf-8")
16 feed_data = sys.stdin.read()
17 feed_document = ET.fromstring(feed_data)
18
19 # Prepare elements.
20 elem_title = feed_document.find("atom:title", atom_ns)
21 elem_description = feed_document.find("atom:subtitle", atom_ns)
22 elem_title = feed_document.find("atom:title", atom_ns)
23 elem_icon = ET.SubElement(feed_document, ET.QName(atom_ns["atom"], "icon"))
24
25 # Find channel ID in the source XML feed and extract it out.
26 channel_id = feed_document.find(".//atom:uri", atom_ns).text
27 channel_id = channel_id[channel_id.index("channel/") + 8:]
28
29 # Fetch data with Piped API.
30 api_url = "https://pipedapi.kavin.rocks/channel/{}".format(channel_id)
31 api_request = urllib.request.Request(api_url)
32
33 api_request.add_header("User-Agent", "curl")
34 api_response = urllib.request.urlopen(api_request)
35 api_data = api_response.read().decode("utf-8")
36
37 # Fill data into feed.
38 api_json = json.loads(api_data)
39 elem_title.text = api_json["name"]
40 elem_description.text = api_json["description"]
41 elem_icon.text = api_json["avatarUrl"]
42
43 print(ET.tostring(feed_document).decode())
44
45 if __name__ == '__main__':
46 main()