This commit is contained in:
JKuijperM 2021-05-15 11:33:10 +02:00
parent f5baf85dec
commit c3c3f6be84

37
main.py
View File

@ -1,25 +1,41 @@
import csv #!/usr/bin/env python
"""
Python script that converts an exported json file from the web TVISO to a csv file can be read by the web Letterboxd.
Author: Jorge Kuijper
Copyright: Copyright 2021, TvisoToLetterboxd
License: GPL 3.0
"""
import json import json
import pandas as pd import pandas as pd
from pathlib import Path from pathlib import Path
from datetime import datetime from datetime import datetime
# csv_path = Path(r'D:\DESCARGAS\tviso-collection.csv') # input_path is the path for the json file
# df = pd.read_csv(csv_path, sep=';') input_path = Path('your_path.json') # Replace the string 'your_path' by yours
# output_path is the path where will be saved the csv file
json_path = Path('data/tviso-collection.json') output_path = Path('your_path.csv') # Replace the string 'your_path' by yours
with open(json_path, encoding='utf-8') as json_file: with open(input_path, encoding='utf-8') as json_file:
data = json.load(json_file) data = json.load(json_file)
# col = ['imdbID,Title,Rating10,WatchedDate'] # The following column names are the tags that need Letterboxd to read the file.
# Create an empty DataFrame with this columns:
# * imdbID: is the ID of the film in IMDB
# * Title: is the Title of the movie
# * Rating10: is the rate in TVISO
# * WatchedDate: is the date when you watched the movie
col = ['imdbID', 'Title', 'Rating10', 'WatchedDate'] col = ['imdbID', 'Title', 'Rating10', 'WatchedDate']
df = pd.DataFrame(columns=col) df = pd.DataFrame(columns=col)
# Fill the DataFrame
for line in data: for line in data:
# Check if the status is watched and the type is movie
if line['status'] == 'watched' and line['type'] == 2: if line['status'] == 'watched' and line['type'] == 2:
title = line['title'] title = line['title']
imdb = line['imdb'] imdb = line['imdb']
rating = line['rating'] if line['rating'] is not None else '' rating = line['rating'] if line['rating'] is not None else ''
# Silent catch if the watched date doesn't exist in TVISO and add an empty string
try: try:
watchedDate = line['checkedDate'] watchedDate = line['checkedDate']
except: except:
@ -29,8 +45,9 @@ for line in data:
dt = datetime.strptime(watchedDate, '%Y-%m-%dT%H:%M:%S+%f:00') dt = datetime.strptime(watchedDate, '%Y-%m-%dT%H:%M:%S+%f:00')
watchedDate = dt.strftime('%Y-%m-%d') watchedDate = dt.strftime('%Y-%m-%d')
# row_data = '{},{},{},{}'.format(imdb, title, rating, watchedDate) # Insert into the DataFrame
row_data = [imdb, title, rating, watchedDate] row_data = [imdb, title, rating, watchedDate]
df.loc[len(df) + 1] = row_data df.loc[len(df) + 1] = row_data
# Export DataFrame to csv
df.to_csv('test.csv', index=False, encoding='utf-8') df.to_csv(output_path, index=False, encoding='utf-8')
print('Done!')