Change field selection to named columns instead of indexes. (csv.DictReader)

Allow empty menu input, defaulting to choice #1
Change total line count to reuse already opened CSV file
Use enumerate() to track row ID
This commit is contained in:
Baptiste Roux 2022-02-19 11:58:40 +01:00
parent 6d8818bc14
commit 9436798e47
No known key found for this signature in database
GPG Key ID: F2D53AA58807C6B5
1 changed files with 114 additions and 125 deletions

View File

@ -1,17 +1,15 @@
# main.py
from logging import error
import sys
from trakt import *
import trakt.core
import os
#!/usr/bin/env python3
import csv
from datetime import datetime
import time
from tinydb import TinyDB, Query
import json
import os
import re
import sys
import time
from datetime import datetime
import trakt.core
from tinydb import Query, TinyDB
from trakt import Expando
from trakt.tv import TVShow
# Adjust this value to increase/decrease your requests between episodes.
@ -58,6 +56,7 @@ def getFollowedShowsPath():
def initTraktAuth():
return True
# Set the method of authentication
trakt.core.AUTH_METHOD = trakt.core.OAUTH_AUTH
return init(config.TRAKT_USERNAME, store=True, client_id=config.CLIENT_ID, client_secret=config.CLIENT_SECRET)
@ -291,42 +290,31 @@ def processWatchedShows():
# Total amount of rows which have been processed in the CSV file
rowsCount = 0
# Total amount of rows in the CSV file
rowsTotal = 0
# Total amount of errors which have occurred in one streak
errorStreak = 0
# Get the total amount of rows in the CSV file,
# which is helpful for keeping track of progress.
# However, if you have a VERY large CSV file (e.g above 100,000 rows)
# then it might be a good idea to remove this due to the performance
# overhead.
with open(getWatchedShowsPath()) as f:
rowsTotal = sum(1 for line in f)
# Open the CSV file within the GDPR exported data
with open(getWatchedShowsPath(), newline='') as csvfile:
# Create the CSV reader, which will break up the fields using the delimiter ','
showsReader = csv.reader(csvfile, delimiter=',')
showsReader = csv.DictReader(csvfile, delimiter=',')
# Get the total amount of rows in the CSV file,
rowsTotal = len(list(showsReader))
# Move position to the beginning of the file
csvfile.seek(0, 0)
# Loop through each line/record of the CSV file
for row in showsReader:
# Increment the row counter to keep track of progress completing the
# records during the import process.
rowsCount += 1
# Get the name of the TV show
tvShowName = row[8]
# Ignore the header row
if rowsCount > 1:
next(showsReader, None)
for rowsCount, row in enumerate(showsReader):
# Get the name of the TV show
tvShowName = row["tv_show_name"]
# Get the TV Time Episode Id
tvShowEpisodeId = row[4]
tvShowEpisodeId = row["episode_id"]
# Get the TV Time Season Number
tvShowSeasonNo = row[5]
tvShowSeasonNo = row["episode_season_number"]
# Get the TV Time Episode Number
tvShowEpisodeNo = row[6]
tvShowEpisodeNo = row["episode_number"]
# Get the date which the show was marked 'watched' in TV Time
tvShowDateWatched = row[7]
tvShowDateWatched = row["updated_at"]
# Parse the watched date value into a Python type
print(tvShowDateWatched)
tvShowDateWatchedConverted = datetime.strptime(
tvShowDateWatched, '%Y-%m-%d %H:%M:%S')
@ -429,7 +417,8 @@ def start():
while True:
try:
menuSelection = int(input(f"Enter your menu selection: "))
menuSelection = input(f"Enter your menu selection: ")
menuSelection = 1 if not menuSelection else int(menuSelection)
break
except ValueError:
print("Invalid input. Please enter a numerical number.")