-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_rssfeed.py
75 lines (57 loc) · 2.13 KB
/
generate_rssfeed.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
""" This code demonstrates writing, deploying, and triggering a
Cloud Function with a Cloud Firestore trigger
for any change to a specific document .
"""
from xml.etree.ElementTree import Element, SubElement, \
Comment, tostring
from xml.dom import minidom
from google.cloud import firestore, storage
from os import environ
destination_bucket = environ.get('DESTINATION_BUCKET', '')
collection = environ.get('collection', '')
database_client = firestore.Client()
storage_client = storage.Client()
def prettify(elem):
"""Return a pretty-printed XML string for the Element.
"""
rough_string = tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
def gather_items(parent_element):
"""Search into firestore database in the cloud
to scrap the title, name, link, creation and generation date.
"""
docs = database_client.collection(collection).stream()
for doc in docs:
dict_values = doc.to_dict()
item = SubElement(parent_element, 'item')
title = SubElement(item, 'title')
title.text = dict_values['title']
link = SubElement(item, 'link')
link.text = dict_values['link']
description = SubElement(item, 'description')
description.text = dict_values['description']
date_creation = SubElement(item, 'Date')
date_creation.text = str(dict_values['creation_date'])
upload = SubElement(item, 'upload')
upload.text = str(dict_values['generation_date'])
def upload_blob(destination_bucket, content, destination_blob_name):
"""Upload a file into the bucket destination.
"""
bucket = storage_client.get_bucket(destination_bucket)
blob = bucket.blob(destination_blob_name)
blob.upload_from_string(content)
def main(event, context):
"""Write the file.
"""
channel = Element('channel')
title = SubElement(channel, 'title')
title.text = "podcasts"
comment = Comment('Generated for Learning')
channel.append(comment)
gather_items(channel)
upload_blob(
destination_bucket,
prettify(channel),
'rssfeed.xml'
)