Skip to content

Commit

Permalink
Add timezone config for timestamps in the site. Closes #94.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Feb 27, 2023
1 parent 3445c37 commit a9d2f2f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
5 changes: 3 additions & 2 deletions tgarchive/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from .db import DB

__version__ = "1.0.0"
__version__ = "1.1.0"

logging.basicConfig(format="%(asctime)s: %(message)s",
level=logging.INFO)
Expand Down Expand Up @@ -37,6 +37,7 @@
"telegram_url": "https://t.me/{id}",
"per_page": 1000,
"show_sender_fullname": False,
"timezone": "",
"site_name": "@{group} (Telegram) archive",
"site_description": "Public archive of @{group} Telegram messages.",
"meta_description": "@{group} {date} Telegram message archive.",
Expand Down Expand Up @@ -153,7 +154,7 @@ def main():

logging.info("building site")
config = get_config(args.config)
b = Build(config, DB(args.data), args.symlink)
b = Build(config, DB(args.data, config["timezone"]), args.symlink)
b.load_template(args.template)
if args.rss_template:
b.load_rss_template(args.rss_template)
Expand Down
3 changes: 1 addition & 2 deletions tgarchive/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import re
import shutil
import magic
from datetime import timezone

from feedgen.feed import FeedGenerator
from jinja2 import Template
Expand Down Expand Up @@ -143,8 +142,8 @@ def _build_rss(self, messages, rss_file, atom_file):
e = f.add_entry()
e.id(url)
e.title("@{} on {} (#{})".format(m.user.username, m.date, m.id))
e.published(m.date.replace(tzinfo=timezone.utc))
e.link({"href": url})
e.published(m.date)

media_mime = ""
if m.media and m.media.url:
Expand Down
15 changes: 13 additions & 2 deletions tgarchive/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sqlite3
from collections import namedtuple
from datetime import datetime
import json
import pytz
from typing import Iterator

schema = """
Expand Down Expand Up @@ -60,8 +60,9 @@ def _page(n, multiple):

class DB:
conn = None
tz = None

def __init__(self, dbfile):
def __init__(self, dbfile, tz=None):
# Initialize the SQLite DB. If it's new, create the table schema.
is_new = not os.path.isfile(dbfile)

Expand All @@ -72,6 +73,9 @@ def __init__(self, dbfile):
# by its row number and a limit multiple.
self.conn.create_function("PAGE", 2, _page)

if tz:
self.tz = pytz.timezone(tz)

if is_new:
for s in schema.split("##"):
self.conn.cursor().execute(s)
Expand Down Expand Up @@ -225,6 +229,13 @@ def _make_message(self, m) -> Message:
description=desc,
thumb=media_thumb)

date = pytz.utc.localize(date) if date else None
edit_date = pytz.utc.localize(edit_date) if edit_date else None

if self.tz:
date = date.astimezone(self.tz) if date else None
edit_date = edit_date.astimezone(self.tz) if edit_date else None

return Message(id=id,
type=typ,
date=date,
Expand Down
5 changes: 5 additions & 0 deletions tgarchive/example/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ telegram_url: "https://t.me/{id}"
# phonebook for users who are in your phonebook.
show_sender_fullname: False

# Timezone to apply on timestamps when building the site. If no value
# is specified, all timestamps are in UTC:
# Eg: US/Eastern Asia/Kolkata
timezone: ""

publish_rss_feed: True
rss_feed_entries: 100 # Show Latest N messages in the RSS feed.

Expand Down

0 comments on commit a9d2f2f

Please sign in to comment.