From 487b087910ff2957906465e349a5a9bfb00a34f2 Mon Sep 17 00:00:00 2001 From: Samuel Sloniker Date: Thu, 24 Nov 2022 21:27:00 -0800 Subject: [PATCH] Rewrite stats script in Python --- stats.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ stats.sh | 30 ---------------------------- 2 files changed, 60 insertions(+), 30 deletions(-) create mode 100644 stats.py delete mode 100755 stats.sh diff --git a/stats.py b/stats.py new file mode 100644 index 0000000..5d90af8 --- /dev/null +++ b/stats.py @@ -0,0 +1,60 @@ +import sqlite3 +import tomli + +with open("sources.toml", "rb") as f: + sources = tomli.load(f) + +con = sqlite3.connect("articles.db") +con.execute("CREATE TABLE IF NOT EXISTS articles(source, category, url, text);") + +article_count = len(list(con.execute("SELECT url FROM articles"))) +left_article_count = len(list(con.execute("SELECT url FROM articles WHERE category = 'left'"))) +right_article_count = len(list(con.execute("SELECT url FROM articles WHERE category = 'right'"))) + +source_count = 0 +left_source_count = 0 +right_source_count = 0 + +left_sources = [] +right_sources = [] + +for source_id, source_info in sources.items(): + source_count += 1 + if source_info["category"] == "left": + left_source_count += 1 + source_list = left_sources + else: + right_source_count += 1 + source_list = right_sources + + source_list.append({ + "name": source_info["name"], + "sort": source_info.get("sort", source_info["name"]), + "count": len(list(con.execute("SELECT url FROM articles WHERE source = ?", (source_id,)))), + }) + +left_sources.sort(key=lambda x: x["sort"]) +right_sources.sort(key=lambda x: x["sort"]) + +left_breakdown = "\n".join([f"* {source['name']}: {source['count']}" for source in left_sources]) +right_breakdown = "\n".join([f"* {source['name']}: {source['count']}" for source in right_sources]) + +con.commit() +con.close() + + +print(f"""\ +This model contains a total of {article_count} articles from {source_count} sources. + +## Left + +{left_breakdown} + +Left total: {left_article_count} articles from {left_source_count} sources + +## Right + +{right_breakdown} + +Right total: {right_article_count} articles from {right_source_count} sources +""") diff --git a/stats.sh b/stats.sh deleted file mode 100755 index 61d1b6e..0000000 --- a/stats.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/bin/bash - -total=$(sqlite3 articles.db "SELECT url FROM articles" | wc -l) -left=$(sqlite3 articles.db "SELECT url FROM articles WHERE category = 'left'" | wc -l) -right=$(sqlite3 articles.db "SELECT url FROM articles WHERE category = 'right'" | wc -l) - -left_sources=$(sqlite3 articles.db "SELECT source FROM articles WHERE category = 'left'" | sort | uniq) -right_sources=$(sqlite3 articles.db "SELECT source FROM articles WHERE category = 'right'" | sort | uniq) - -total_source_count=$(sqlite3 articles.db "SELECT source FROM articles" | sort | uniq | wc -l) -left_source_count=$(echo $left_sources | wc -w) -right_source_count=$(echo $right_sources | wc -w) - -echo "This model contains a total of $total articles from $total_source_count sources." -echo "" -echo "## Left" -echo "" -for i in $left_sources; do - echo "* $i: $(sqlite3 articles.db "SELECT url FROM articles WHERE source = '$i'" | wc -l)" -done -echo "" -echo "Left total: $left articles from $left_source_count sources" -echo "" -echo "## Right" -echo "" -for i in $right_sources; do - echo "* $i: $(sqlite3 articles.db "SELECT url FROM articles WHERE source = '$i'" | wc -l)" -done -echo "" -echo "Right total: $right articles from $right_source_count sources"