#!/usr/bin/python3 # # Program to dump a MediaProcessor DB into a folder of html files # # This will create an index.html file that has links to all files # produced by this program. Each file is named by the IPFS hash # for the media content. It contains all of the metadata and as a # hidden set of name/value pairs with a few visible lines for use # in a web browser. # import datetime import sqlite3 import time import sys import os def usage(): cmd = sys.argv[0] str = "\nCreates a file for each row of the target SQLite database.\n" str += "Usage: " + cmd + " <-d sqlite DB file> <-f folder> [-s since]\n\n" print(str) exit(0) HTML1 = "\n\t\n\t\t\n\t\t\t" HTML2 = "\n\t\t\n\t\t\n" HTML3 = "\n\n\n" ############################################################################# # # # main # # # ############################################################################# argc = len(sys.argv) cols = [] conn = sql = row = url = since = None try: # Parse command line if argc > 4: # Required parameter: SQLite database file to check pins against if sys.argv[1] == "-d": dbFile = sys.argv[2] conn = sqlite3.connect(dbFile) # Failure to open is exception conn.row_factory = sqlite3.Row # Result format to dictionary else: usage() # Required parameter: folder where files will be written if sys.argv[3] == "-f": folder = sys.argv[4] if not os.path.exists(folder): os.mkdir(folder) else: usage() # Optional parameter: since date if argc == 7: if sys.argv[5] == "-s": try: since = sys.argv[6] datetime.datetime.strptime(since, '%Y-%m-%d') except Exception: print("Incorrect date format, must be YYYY-MM-DD") exit(0) else: usage() else: usage() # Make an index.html file with links to all files generated with open(folder + "/index.html", 'w') as idx: idx.write(HTML1 + "IPFS Hash Index" + HTML2 + "
\n")
        sql = "SELECT vhash FROM IPFS_HASH_INDEX ORDER BY grupe, sqlts"
        rows = (conn.cursor().execute(sql)).fetchall()
        for row in range(0, len(rows)):
            vhash = rows[row]['vhash']
            if vhash and len(vhash) == 46:
                idx.write(f"{vhash}  ")
                if (row + 1) % 3 == 0: idx.write('\n')
        idx.write(HTML3)

    # Create the files, with contents being all metadata fields & values.
    # Display a few items at top of file and leave most hidden.
    sql = "SELECT * FROM IPFS_HASH_INDEX "
    rows = conn.cursor().execute(sql + "limit 1;")
    for col in rows.description:                   # First get column names
        cols.append(col[0])

    if since: sql += f"WHERE sqlts >= '{since}' "
    sql +=  "ORDER BY grupe, sqlts"

    rows = (conn.cursor().execute(sql)).fetchall() # Now get the column values
    fiveSpaces = "     "
    for row in rows:
        vhash = row['vhash']
        if not vhash or len(vhash) < 46: continue
        file = folder + '/' + row['vhash'] + ".html"
        with open(file, 'w') as f:
            f.write(HTML1 + row['title'] + HTML2)
            ipfs = f"http://127.0.0.1:8080/ipfs/{row['vhash']}"
            url = f"{row['webpage_url']}"
            f.write(f"\t\t\t

Open on IPFS

\n") f.write(f"\t\t\tTitle:{fiveSpaces} {row['title']}
\n") f.write(f"\t\t\tSource:{fiveSpaces} {url}
\n") f.write(f"\t\t\tDescription:\n") f.write(f"\t\t\t

{row['description']}

\n") # Remainder of columns will only be visible with "show page source" f.write(f"
\n")
            for col in cols:
                f.write(f"{col} = {row[col]}\n")
            f.write(HTML3)

except sqlite3.Error as e:
    print("Database error during query: %s\nSQL=%s\n\n" % (e, sql))

except sqlite3.OperationalError:
    print("ERROR!: Query Failure")

except Exception as e:
    print("Exception: %s\n\n" % e)

exit(0)