#!/usr/bin/python3

#
# Create a UID of the article in old articles where we don't have RSS UID and where we can't generate the article_id on the fly.
# It takes a while, but it's a one-shot.
#

import sqlite3
import hashlib

db_con = sqlite3.connect("../data/diffs.db")
db = db_con.cursor()


def create_article_id(uid, feed):
    # Create a fake unique ID from RSS unique tag and feed name to reference the article in database
    id_string = str(uid) + str(feed)
    id_bytes = id_string.encode("utf-8")
    article_id = hashlib.sha256(id_bytes).hexdigest()
    return article_id


def update_diff(diff_id, article_id):
    sql = "UPDATE diffs SET article_id = ? WHERE diff_id = ?"
    sql_data = (article_id, diff_id)
    db.execute(sql, sql_data)
    db_con.commit()


db.execute(
    "SELECT * FROM diffs WHERE NOT 'article_id' ORDER BY diff_id DESC ",
)
diffs = db.fetchall()

for diff in diffs:
    article_id = create_article_id(diff[1], diff[2])
    update_diff(diff[0], article_id)
    print(article_id)