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 pandas as pd
from pathlib import Path
from datetime import datetime
# csv_path = Path(r'D:\DESCARGAS\tviso-collection.csv')
# df = pd.read_csv(csv_path, sep=';')
json_path = Path('data/tviso-collection.json')
with open(json_path, encoding='utf-8') as json_file:
# input_path is the path for the json file
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
output_path = Path('your_path.csv') # Replace the string 'your_path' by yours
with open(input_path, encoding='utf-8') as 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']
df = pd.DataFrame(columns=col)
# Fill the DataFrame
for line in data:
# Check if the status is watched and the type is movie
if line['status'] == 'watched' and line['type'] == 2:
title = line['title']
imdb = line['imdb']
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:
watchedDate = line['checkedDate']
except:
@ -29,8 +45,9 @@ for line in data:
dt = datetime.strptime(watchedDate, '%Y-%m-%dT%H:%M:%S+%f:00')
watchedDate = dt.strftime('%Y-%m-%d')
# row_data = '{},{},{},{}'.format(imdb, title, rating, watchedDate)
# Insert into the DataFrame
row_data = [imdb, title, rating, watchedDate]
df.loc[len(df) + 1] = row_data
df.to_csv('test.csv', index=False, encoding='utf-8')
# Export DataFrame to csv
df.to_csv(output_path, index=False, encoding='utf-8')
print('Done!')