Commit 8df3991b authored by Lysander Trischler's avatar Lysander Trischler

Add markread utility

parent c170319e
#!/usr/bin/python3
import argparse
import configparser
import datetime
import dateutil.parser
import shelve
import os.path
import xdg
import twtxthash
def parse_datetime(s):
t = dateutil.parser.parse(s)
if not t.tzinfo:
# default to UTC if timezone is missing
t.tzinfo = dt.replace(tzinfo=datetime.timezone.utc)
return t
parser = argparse.ArgumentParser(description="Mark twtxt.txt feed read")
parser.add_argument("-c", "--config-file",
metavar="FILE",
default=os.path.join(xdg.xdg_config_home(), "twtxt", "config"),
help="path to the config file to resolve nicks to URLs (default: %(default)s)")
parser.add_argument("-f", "--cache-file",
metavar="FILE",
default=os.path.join(xdg.xdg_config_home(), "twtxt", "cache"),
help="path to the cache file (default: %(default)s)")
parser.add_argument("-d", "--data-file",
metavar="FILE",
default=os.path.join(xdg.xdg_data_home(), "twxt", "data"),
help="path to the data file (default: %(default)s)")
parser.add_argument("nick_or_url",
metavar="NICK/URL",
help="nick or URL of the feed to mark twts read")
parser.add_argument("start",
metavar="START",
type=parse_datetime,
help="start timestamp to include twts for marking read")
parser.add_argument("end",
metavar="END",
type=parse_datetime,
help="end timestamp to include twts for marking read")
args = parser.parse_args()
if args.cache_file.endswith(".db"):
args.cache_file = args.cache_file[:-3]
cache = shelve.open(args.cache_file)
if args.data_file.endswith(".db"):
args.data_file = args.data_file[:-3]
data = shelve.open(args.data_file)
if args.nick_or_url in cache:
feed = cache[args.nick_or_url]
else:
# we need to resolve the nick to an URL first
cfg = configparser.ConfigParser()
cfg.read(args.config_file)
url = cfg.get("following", args.nick_or_url, fallback=None)
if url is None:
parser.error("unknown nick/URL '%s'" % args.nick_or_url)
feed = cache[url]
ignored = 0
read = 0
for twt in feed['tweets']:
if args.start <= twt.created_at <= args.end:
hash = twtxthash.create_hash(created_at=twt.created_at,
text=twt.text,
source=twt.source)
entry = data.get(hash, {})
entry["read"] = True
data[hash] = entry
read += 1
else:
ignored += 1
if read > 0:
data.sync()
print("Marked %d twt%s read, ignored %d twt%s" % (
read, "" if read == 1 else "s",
ignored, "" if ignored == 1 else "s"))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment