Untracked hashtags report

This commit is contained in:
Keith Edmunds 2023-01-21 23:11:35 +00:00
parent b15c1029ef
commit c7757efbf6
2 changed files with 97 additions and 3 deletions

View File

@ -21,3 +21,5 @@ class Config(object):
MAX_POSTS_TO_FETCH = 200 MAX_POSTS_TO_FETCH = 200
POINTS_BOOSTED = 1 POINTS_BOOSTED = 1
POINTS_FAVOURITED = 1 POINTS_FAVOURITED = 1
TOP_HASHTAGS_TO_REPORT = 3
TOP_POSTS_TO_REPORT = 3

View File

@ -1,5 +1,6 @@
#! /usr/bin/env python #! /usr/bin/env python
import argparse
import datetime import datetime
import ipdb import ipdb
import os import os
@ -7,6 +8,7 @@ import pickle
import random import random
import requests import requests
import stackprinter import stackprinter
import subprocess
import sys import sys
from config import Config from config import Config
@ -28,6 +30,10 @@ from models import (
Posts, Posts,
PostTags, PostTags,
) )
from sqlalchemy import (
func,
select,
)
from typing import List, Optional, Union from typing import List, Optional, Union
@ -101,7 +107,7 @@ class MastodonAPI:
_ = self.mastodon.status_unbookmark(post_id) _ = self.mastodon.status_unbookmark(post_id)
def main() -> None: def update_database() -> None:
""" """
Main loop Main loop
""" """
@ -142,6 +148,31 @@ def get_and_process_favourited(session, mastapi):
favourited = mastapi.mastodon.fetch_next(favourited) favourited = mastapi.mastodon.fetch_next(favourited)
def get_database_name():
"""Return database name as string"""
with Session() as session:
dbname = session.bind.engine.url.database
return dbname
def get_version_string():
"""Return Urma version as string"""
try:
return str(
subprocess.check_output(
['git', 'describe'], stderr=subprocess.STDOUT
)
).strip('\'b\\n')
except subprocess.CalledProcessError as exc_info:
gitproc = subprocess.Popen(['git', 'rev-parse', 'HEAD'],
stdout=subprocess.PIPE)
(stdout, _) = gitproc.communicate()
return stdout.strip()[:7].decode("utf-8")
def process_bookmarked_posts(session: Session, def process_bookmarked_posts(session: Session,
posts: List[Posts], me_id: int) -> bool: posts: List[Posts], me_id: int) -> bool:
""" """
@ -241,6 +272,46 @@ def _process_post(session: Session, post: Posts, me_id) -> Posts:
return rec return rec
def report():
"""Print report"""
print(f"Urma version: {get_version_string()}")
print(f"Database: {get_database_name()}")
print(f"Date: {datetime.datetime.now().strftime('%c')}")
print()
with Session() as session:
# Find the most popular hashtags that we don't follow
print("Hashtags you don't follow that feature in posts you like")
print("--------------------------------------------------------")
top_unfollowed_tags = (
session.execute(
select(Hashtags, func.count(Hashtags.name))
.join(PostTags).join(Posts)
.where(Posts.favourited == 1, Hashtags.followed == 0)
.group_by(Hashtags.name)
.order_by(func.count(Hashtags.name).desc())
.limit(Config.TOP_HASHTAGS_TO_REPORT))
.all()
)
# How many times was each hashtag in a post we didnt' like?
for (hashtag, like) in top_unfollowed_tags:
dislike = (
session.execute(
select(func.count(Posts.id))
.join(PostTags).join(Hashtags)
.where(Posts.favourited == 0, Hashtags.id == hashtag.id)
).scalars()
.all()[0]
)
print(
f"Hashtag {hashtag.name} {like=}, {dislike=} "
f"({like * 100 / (like + dislike):.2f}% liked)"
)
def update_followed_accounts(session: Session, mastapi: MastodonAPI) -> None: def update_followed_accounts(session: Session, mastapi: MastodonAPI) -> None:
""" """
Retrieve list of followed accounts and update accounts Retrieve list of followed accounts and update accounts
@ -313,10 +384,31 @@ if __name__ == "__main__":
If command line arguments given, carry out requested function and If command line arguments given, carry out requested function and
exit. Otherwise run full application. exit. Otherwise run full application.
""" """
try: try:
Base.metadata.create_all(engine) Base.metadata.create_all(engine)
sys.exit(main())
p = argparse.ArgumentParser()
# Only allow at most one option to be specified
group = p.add_mutually_exclusive_group()
group.add_argument('-u', '--update',
action="store_true", dest="update_database",
default=False, help="Update database from Mastodon")
group.add_argument('-r', '--report',
action="store_true", dest="report",
default=False, help="Report")
args = p.parse_args()
# Run as required
if args.update_database:
log.debug("Updating database")
update_database()
elif args.report:
log.debug("Report")
report()
else:
# For now, default to updating database
update_database()
except Exception as exc: except Exception as exc:
if os.environ["URMA_ENV"] != "DEVELOPMENT": if os.environ["URMA_ENV"] != "DEVELOPMENT":