import sqlite3

conn = sqlite3.connect("data/leach.db")
curs = conn.cursor()

months = {
        "JAN" : "01",
        "FEB" : "02",
        "MAR" : "03",
        "APR" : "04",
        "MAY" : "05",
        "JUN" : "06",
        "JUL" : "07",
        "AUG" : "08",
        "SEP" : "09",
        "OCT" : "10",
        "NOV" : "11",
        "DEC" : "12"
        }

#
# sql = """ SELECT id, date(date_published) FROM podcasts """
#
# curs.execute(sql)
# olddates = curs.fetchall()
# # print(sql)

conn = sqlite3.connect("data/temp.db")
curs = conn.cursor()

# sql = """CREATE TABLE podcasts (
#             id int,
#             l_date text,
#             j_date real,
#             shortdate text);"""
# curs.execute(sql)
# conn.commit()

# olddates = ([4, "Mon, 08 Jul 2019 00:00:00 -0800"],)

sql = """ SELECT * FROM podcasts """
curs.execute(sql)
olddates = curs.fetchall()

print("olddates -", olddates)
for row in olddates:
    print("row -", row, type(row))

    tdate = row[1].split(" ")
    print("tdate -", tdate, type(tdate))

    rowid = row[0]
    print("rowid -", rowid, type(rowid))

    newdate = "%s-%s-%s" % (tdate[3], months[tdate[2].upper()], tdate[1])
    print("newdate -", newdate, type(newdate))

    sql = """ UPDATE podcasts
              SET j_date=julianday('%s'), shortdate='%s'
              WHERE id=%s ;""" % (newdate, newdate, rowid)

    curs.execute(sql)


def fixdate(longdate):
    """ Many podcasts use a super long date format that I hate. This will
    cut them down to a simple format. Here's the original:

    "Mon, 08 Jul 2019 00:00:00 -0800"

    """

    tdate = longdate.split(" ")
    months = {"JAN" : "01", "FEB" : "02", "MAR" : "03", "APR" : "04",
              "MAY" : "05", "JUN" : "06", "JUL" : "07", "AUG" : "08",
              "SEP" : "09", "OCT" : "10", "NOV" : "11", "DEC" : "12"}

    return "%s-%s-%s" % (tdate[3], months[tdate[2].upper()], tdate[1])





# for row in olddates:
#     print(row)
#     sql = """
#         UPDATE podcasts
#         SET date_published=julianday('%s')
#         WHERE id='%s'; """ % (row[1], row[0])
#     print(sql)
#     curs.execute(sql)
#
conn.commit()


"""
2019-07-05
2019-07-08
2019-07-02
2019-07-08
2019-06-24


Mon, 08 Jul 2019 00:00:00 -0800



"""

import sqlite3

conn = sqlite3.connect("data/leach.db")
curs = conn.cursor()

months = {
        "JAN" : "01",
        "FEB" : "02",
        "MAR" : "03",
        "APR" : "04",
        "MAY" : "05",
        "JUN" : "06",
        "JUL" : "07",
        "AUG" : "08",
        "SEP" : "09",
        "OCT" : "10",
        "NOV" : "11",
        "DEC" : "12"
        }

#
# sql = """ SELECT id, date(date_published) FROM podcasts """
#
# curs.execute(sql)
# olddates = curs.fetchall()
# # print(sql)

conn = sqlite3.connect("data/temp.db")
curs = conn.cursor()

# sql = """CREATE TABLE podcasts (
#             id int,
#             l_date text,
#             j_date real,
#             shortdate text);"""
# curs.execute(sql)
# conn.commit()

# olddates = ([4, "Mon, 08 Jul 2019 00:00:00 -0800"],)

sql = """ SELECT * FROM podcasts """
curs.execute(sql)
olddates = curs.fetchall()

print("olddates -", olddates)
for row in olddates:
    print("row -", row, type(row))

    tdate = row[1].split(" ")
    print("tdate -", tdate, type(tdate))

    rowid = row[0]
    print("rowid -", rowid, type(rowid))

    newdate = "%s-%s-%s" % (tdate[3], months[tdate[2].upper()], tdate[1])
    print("newdate -", newdate, type(newdate))

    sql = """ UPDATE podcasts
              SET j_date=julianday('%s'), shortdate='%s'
              WHERE id=%s ;""" % (newdate, newdate, rowid)

    curs.execute(sql)


def fixdate(longdate):
    """ Many podcasts use a super long date format that I hate. This will
    cut them down to a simple format. Here's the original:

    "Mon, 08 Jul 2019 00:00:00 -0800"

    """

    tdate = longdate.split(" ")
    months = {"JAN" : "01", "FEB" : "02", "MAR" : "03", "APR" : "04",
              "MAY" : "05", "JUN" : "06", "JUL" : "07", "AUG" : "08",
              "SEP" : "09", "OCT" : "10", "NOV" : "11", "DEC" : "12"}

    return "%s-%s-%s" % (tdate[3], months[tdate[2].upper()], tdate[1])





# for row in olddates:
#     print(row)
#     sql = """
#         UPDATE podcasts
#         SET date_published=julianday('%s')
#         WHERE id='%s'; """ % (row[1], row[0])
#     print(sql)
#     curs.execute(sql)
#
conn.commit()


"""
2019-07-05
2019-07-08
2019-07-02
2019-07-08
2019-06-24


Mon, 08 Jul 2019 00:00:00 -0800



"""

# # sql = """ SELECT id, date(date_published) FROM podcasts """ # # curs.execute(sql) # olddates = curs.fetchall() # # print(sql) # sql = """CREATE TABLE podcasts ( #             id int, #             l_date text, #             j_date real, #             shortdate text);""" # curs.execute(sql) # conn.commit() # olddates = ([4, "Mon, 08 Jul 2019 00:00:00 -0800"],) SELECT * FROM podcasts UPDATE podcasts               SET j_date=julianday('%s'), shortdate='%s'               WHERE id=%s ; Many podcasts use a super long date format that I hate. This will     cut them down to a simple format. Here's the original:      "Mon, 08 Jul 2019 00:00:00 -0800" # for row in olddates: #     print(row) #     sql = """ #         UPDATE podcasts #         SET date_published=julianday('%s') #         WHERE id='%s'; """ % (row[1], row[0]) #     print(sql) #     curs.execute(sql) # 2019-07-05 2019-07-08 2019-07-02 2019-07-08 2019-06-24   Mon, 08 Jul 2019 00:00:00 -0800