From a1b3ad2a925f9e7dfd6ec811589f361e67b946e5 Mon Sep 17 00:00:00 2001 From: retoor Date: Tue, 26 Nov 2024 09:22:24 +0100 Subject: [PATCH] Blacked. --- src/drstats/dataset.py | 18 ++++++++++-------- src/drstats/db.py | 26 +++++++++++++++++++------- src/drstats/statistics.py | 23 +++++++---------------- src/drstats/sync.py | 15 ++++++++++----- 4 files changed, 46 insertions(+), 36 deletions(-) diff --git a/src/drstats/dataset.py b/src/drstats/dataset.py index 7feeb5c..270523b 100644 --- a/src/drstats/dataset.py +++ b/src/drstats/dataset.py @@ -23,9 +23,7 @@ def dump(): print("\n".join(statistics_text)) all_content = "" for user in list(db.get_users()): - text = ( - db.get_all_rants_of_user(user) - ) + text = db.get_all_rants_of_user(user) total_text = "" if text: total_text += text @@ -42,9 +40,7 @@ def dump(): f.write(" said ") f.write(text) f.write("```") - text = ( - db.get_all_posts_of_user(user) - ) + text = db.get_all_posts_of_user(user) if text: total_text += text print( @@ -62,7 +58,7 @@ def dump(): f.write("```") all_content += total_text print("===Mentions of users:", "```") - + users = db.get_users() users.sort() for user in users: @@ -76,4 +72,10 @@ def dump(): printr(f"export/view-{view['name']}.json") with pathlib.Path(f"export/view-{view['name']}.json").open("w+") as f: with db.Db() as _db: - json.dump([dict(record) for record in _db.query("SELECT * FROM {}".format(view['name']))],f) \ No newline at end of file + json.dump( + [ + dict(record) + for record in _db.query("SELECT * FROM {}".format(view["name"])) + ], + f, + ) diff --git a/src/drstats/db.py b/src/drstats/db.py index a7d2fac..372bf89 100644 --- a/src/drstats/db.py +++ b/src/drstats/db.py @@ -206,8 +206,12 @@ def get_db(): "CREATE VIEW posts_of_user AS SELECT user_username as username, GROUP_CONCAT(body) as text FROM comments" ) db.query("DROP VIEW IF EXISTS contributions_extended_ranked") - db.query("CREATE VIEW contributions_extended_ranked AS SELECT ROW_NUMBER() OVER (ORDER BY upvotes_per_post_on_average DESC) as rank_by_appreciation_based_on_upvotes_per_message, * FROM contributions_extended ORDER BY upvotes_per_post_on_average DESC") - db.query("CREATE VIEW IF NOT EXISTS views AS SELECT sql, name FROM sqlite_schema WHERE type='view' AND name != 'views';") + db.query( + "CREATE VIEW contributions_extended_ranked AS SELECT ROW_NUMBER() OVER (ORDER BY upvotes_per_post_on_average DESC) as rank_by_appreciation_based_on_upvotes_per_message, * FROM contributions_extended ORDER BY upvotes_per_post_on_average DESC" + ) + db.query( + "CREATE VIEW IF NOT EXISTS views AS SELECT sql, name FROM sqlite_schema WHERE type='view' AND name != 'views';" + ) return db @@ -246,21 +250,29 @@ def get_views(): with Db() as db: return list(db.query("SELECT * FROM views;")) + def get_contributions(): with Db() as db: - contributions = db.query( - "SELECT * FROM contributions_extended_ranked" - ) + contributions = db.query("SELECT * FROM contributions_extended_ranked") return list(contributions) def get_upvote_average(): - return avg(contribution["upvotes_per_post_on_average"] for contribution in get_contributions()) + return avg( + contribution["upvotes_per_post_on_average"] + for contribution in get_contributions() + ) def get_users(): with Db() as db: - return [user["username"] for user in db.query("SELECT DISTINCT username FROM contributions ORDER BY username")] + return [ + user["username"] + for user in db.query( + "SELECT DISTINCT username FROM contributions ORDER BY username" + ) + ] + def get_user_count(): return len(get_users()) diff --git a/src/drstats/statistics.py b/src/drstats/statistics.py index 78e4e77..32b070c 100644 --- a/src/drstats/statistics.py +++ b/src/drstats/statistics.py @@ -3,10 +3,9 @@ import asyncio from drstats.duration import Duration - - def new_plot(): import matplotlib.pyplot as plt + plt.clf() plt.cla() plt.close(0) @@ -16,11 +15,12 @@ def new_plot(): def get_date_range(): with Db() as db: - record = list(db.query( - "SELECT min(date(created)) as start_date, max(date(created)) as end_date FROM rants" - ))[0] + record = list( + db.query( + "SELECT min(date(created)) as start_date, max(date(created)) as end_date FROM rants" + ) + )[0] return record["start_date"], record["end_date"] - def get_date_range_str(): @@ -50,7 +50,6 @@ async def rant_stats_per_day(): plt.title("Rants per day") plt.legend() plt.savefig(f"export/rants_per_day_{get_date_range_str()}.png") - async def comment_stats_per_day(): @@ -76,7 +75,6 @@ async def comment_stats_per_day(): plt.title("Comments per day") plt.legend() plt.savefig(f"export/comments_per_day_{get_date_range_str()}.png") - async def rant_stats_per_weekday(): @@ -97,7 +95,6 @@ async def rant_stats_per_weekday(): plt.title("Rants per weekday") plt.legend() plt.savefig(f"export/rants_per_weekday_{get_date_range_str()}.png") - async def comment_stats_per_weekday(): @@ -118,7 +115,6 @@ async def comment_stats_per_weekday(): plt.title("Comments per weekday") plt.legend() plt.savefig(f"export/comments_per_weekday_{get_date_range_str()}.png") - async def rant_stats_per_hour(): @@ -139,7 +135,6 @@ async def rant_stats_per_hour(): plt.title("Rants per hour") plt.legend() plt.savefig(f"export/rants_per_hour_{get_date_range_str()}.png") - async def comment_stats_per_hour(): @@ -160,7 +155,6 @@ async def comment_stats_per_hour(): plt.title("Comments per hour") plt.legend() plt.savefig(f"export/comments_per_hour_{get_date_range_str()}.png") - async def score_most_ignored_last_7_days(): @@ -185,8 +179,7 @@ async def score_most_ignored_last_7_days(): plt.savefig( f"export/score_ignored_most_last_7_days_{get_date_range_str()}.png" ) - - + async def score_last_7_days(): with Duration("Upvotes (score) last 7 days"): @@ -206,7 +199,6 @@ async def score_last_7_days(): plt.title("Most ignored based on score regarding last 7 days") plt.legend() plt.savefig(f"export/score_last_7_days_{get_date_range_str()}.png") - async def user_score_per_day(username): @@ -258,7 +250,6 @@ async def user_score_per_day(username): plt.legend() plt.savefig(f"export/score_user_{username}_{get_date_range_str()}.png") - def stats(): diff --git a/src/drstats/sync.py b/src/drstats/sync.py index 65364fc..0c5f8d0 100644 --- a/src/drstats/sync.py +++ b/src/drstats/sync.py @@ -31,7 +31,7 @@ async def get_recent_rants(start_from=1, page_size=10): def get_rants(): return dr.get_rants("recent", page_size, start_from)["rants"] - + rants = asyncio.wait_for(loop.run_in_executor(get_rants), 5) page += 1 @@ -46,7 +46,7 @@ async def get_recent_rants(start_from=1, page_size=10): start_from += page_size -async def _sync_rants(start_from, page_size,count): +async def _sync_rants(start_from, page_size, count): async for rant in get_recent_rants(start_from, page_size): start_from += page_size count += 1 @@ -55,6 +55,7 @@ async def _sync_rants(start_from, page_size,count): print(f"Upserted {count} rant(s).") return count + async def sync_rants(): count = 0 start_from = 0 @@ -63,11 +64,15 @@ async def sync_rants(): while True: try: - count += await asyncio.wait_for(_sync_rants(start_from, page_size,count),5) + count += await asyncio.wait_for( + _sync_rants(start_from, page_size, count), 5 + ) start_from += page_size except Exception as ex: print(ex) - print("If exception described above is an timeout related error, it's due ratelimiting and considered OK.") + print( + "If exception described above is an timeout related error, it's due ratelimiting and considered OK." + ) break @@ -76,7 +81,7 @@ async def sync_comments(): rants_synced = 0 for rant in db["rants"].find(order_by="-id"): rants_synced += 1 - comments = dr.get_rant(rant["id"]).get("comments",[]) + comments = dr.get_rant(rant["id"]).get("comments", []) for comment in comments: comments_synced += 1 comment["created"] = timestamp_to_string(comment["created_time"])