Generate pages: process single message
This commit is contained in:
parent
ef1b6589bb
commit
4d98af3ddb
55
scel-buc.py
55
scel-buc.py
|
@ -20,6 +20,7 @@
|
|||
import urllib.request
|
||||
import json
|
||||
from datetime import datetime as dt
|
||||
from os.path import exists as path_exists
|
||||
|
||||
from constants import *
|
||||
|
||||
|
@ -31,7 +32,7 @@ def get_updates(offset=0):
|
|||
with open(MESSAGES, 'a') as f:
|
||||
for i in updates['result']:
|
||||
if 'message' in i:
|
||||
f.write(json.dumps(i['message'], indent=4)+'\n')
|
||||
f.write(json.dumps(i['message'], indent=4)+',\n')
|
||||
except urllib.error.HTTPError as e:
|
||||
print(f'HTTP error {e.code}: {e.reason}')
|
||||
if e.code == 401:
|
||||
|
@ -47,5 +48,55 @@ def send_message(chat_id, text):
|
|||
f.write(json.dumps(message, indent=4)+'\n')
|
||||
|
||||
|
||||
def write_message(my_id, my_name, message):
|
||||
# Extract the data
|
||||
sender_id = message['from']['id']
|
||||
chat_id = message['chat']['id']
|
||||
date = message['date']
|
||||
text = message['text']
|
||||
# Process the text
|
||||
text = text.replace('\n','</p>\n<p>')
|
||||
text = '<p>'+text+'</p>'
|
||||
|
||||
chat_file = CHAT_PATH+f'/{chat_id}.html'
|
||||
if not path_exists(chat_file):
|
||||
# Create the file
|
||||
with open(chat_file, 'w') as f:
|
||||
f.write(f'<html>\n'
|
||||
f' <head>\n'
|
||||
f' <link rel="stylesheet" href="./style.css" />\n'
|
||||
f' <title>{chat_id}</title>\n'
|
||||
f' </head>\n'
|
||||
f'<body>\n'
|
||||
f' <h1>Mom</h1>\n')
|
||||
with open(chat_file, 'a') as f:
|
||||
# Start the div
|
||||
f.write(f' <div class="message received">\n'
|
||||
f' <p class="metadata">\n'
|
||||
f' {sender_id} - {date} - ID: {sender_id}\n'
|
||||
f' </p>\n'
|
||||
f' <div class="content">\n')
|
||||
# Enter the content
|
||||
f.write(text)
|
||||
# End the div(s)
|
||||
f.write(f' </div>\n'
|
||||
f' </div>\n')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
get_updates()
|
||||
with urllib.request.urlopen(f'{API}/getMe') as request:
|
||||
reply = json.loads(request.read())
|
||||
me = reply['result']
|
||||
my_id = me['id']
|
||||
my_name = me['first_name']
|
||||
|
||||
with open(MESSAGES,'r') as f:
|
||||
messages = f.read()
|
||||
# Make messages a single, valid JSON object
|
||||
messages = messages.rstrip().rstrip(',')
|
||||
messages = '['+messages+']'
|
||||
# Read messages as JSON
|
||||
messages = json.loads(messages)
|
||||
|
||||
for i in messages:
|
||||
write_message(my_id,my_name,i)
|
||||
|
|
Reference in New Issue