38 lines
1.6 KiB
Python
38 lines
1.6 KiB
Python
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import json
|
|
import os
|
|
from rivista.xmpp.utilities import XmppUtilities
|
|
|
|
class JsonIndex:
|
|
|
|
def generate_json(iq, directory_cache_json):
|
|
"""Create a JSON file from node items."""
|
|
json_data = []
|
|
pubsub = iq['from'].bare
|
|
node = iq['pubsub']['items']['node']
|
|
entries = iq['pubsub']['items']
|
|
for entry in entries:
|
|
item_id = entry['id']
|
|
item_payload = entry['payload']
|
|
namespace = '{http://www.w3.org/2005/Atom}'
|
|
title = item_payload.find(namespace + 'title')
|
|
title_text = '*** No Title ***' if title == None else title.text
|
|
# updated = item.find(namespace + 'updated')
|
|
# updated = None if updated == None else updated.text
|
|
# if updated: updated = datetime.datetime.fromisoformat(updated)
|
|
link_href = XmppUtilities.form_an_item_link(pubsub, node, item_id)
|
|
# link = item.find(namespace + 'link')
|
|
# link_href = '' if link == None else link.attrib['href']
|
|
json_data_entry = {'title' : title_text,
|
|
'link' : link_href}
|
|
json_data.append(json_data_entry)
|
|
#if len(json_data) > 6: break
|
|
directory = os.path.join(directory_cache_json, pubsub)
|
|
if not os.path.exists(directory):
|
|
os.mkdir(directory)
|
|
filename = os.path.join(directory_cache_json, pubsub, f'{node}.json')
|
|
|
|
with open(filename, 'w', encoding='utf-8') as f:
|
|
json.dump(json_data, f, ensure_ascii=False, indent=4)
|