-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfixer.py
142 lines (91 loc) · 3.95 KB
/
fixer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#! /usr/bin/python3
#
# Coded by --> JCGdev
#
# Convert your CSV file to a valid JSON for NewPipe
#
#
import os
import sys
import io
import json
import csv
import argparse
# -------- Setting arguments -----------
argumentParser = argparse.ArgumentParser(description="Convert your Youtube takeout CSV into a JSON that NewPipe is able to read!")
argumentParser.add_argument("-f", "--file", type=str, required=True, help="Path of your subscriptions CSV")
argumentParser.add_argument("-j", "--jsonHeader", type=str, required=True, help="Path to the NewPipe Headers (app_version, app_verion_int...)")
argumentParser.add_argument("-e", "--encoding", type=str, required=False, help="Specify other encoding, by default is utf8")
arguments = argumentParser.parse_args()
# ---------------------------------------
# ------ Defining global variables ------
runtimePath: str = os.getcwd()
csvFilename: str = arguments.file
jsonHeaderFilename: list = arguments.jsonHeader
paramEncoding: str = arguments.encoding
if(paramEncoding == None):
paramEncoding = "utf8"
# ---------------------------------------
def getFilenamesInDir() -> list:
return os.listdir()
# def getCsvFilename(filenames: str) -> str:
# return "".join(name for name in filenames if name.endswith(".csv"))
def getFileDescriptor(filename: str, mode: str) -> io.TextIOWrapper:
global paramEncoding
return open(filename, mode, encoding=paramEncoding)
def readFileLines(fileDescriptor: io.TextIOWrapper) -> dict:
lines: dict = fileDescriptor.readlines()
fileDescriptor.close()
return lines
def readFileString(fileDescriptor: io.TextIOWrapper) -> str:
fileContent: str = fileDescriptor.read()
fileDescriptor.close()
return fileContent
def writeLinesToFile(fileDescriptor: io.TextIOWrapper, content: list) -> None:
fileDescriptor.writelines(content)
fileDescriptor.close()
def writeStringToFile(fileDescriptor: io.TextIOWrapper, content: str) -> None:
fileDescriptor.write(content)
fileDescriptor.close()
def removeBlankLines(paramList: list) -> list:
parsedList: list = []
for section in paramList:
if not section.isspace():
parsedList.append(section)
return parsedList
def cleanFiles() -> None:
os.remove("parsed_subscriptions.csv")
# ---------- Main functions --------
def parseCsv(csvFileDescriptor: io.TextIOWrapper) -> list:
csvContent: list = removeBlankLines(readFileLines(csvFileDescriptor))
csvContent[0] = "service_id,url,name\n" # Overriding not working column names from the 1st line
for line in csvContent:
index: int = csvContent.index(line)
csvContent[index] = line.replace("http", "https")
return csvContent
def convertCsvToJson(CsvFileDescriptor: io.TextIOWrapper) -> str:
global jsonHeaderFilename
debugPrint("Converting parsed CSV to JSON file...")
jsonHeaderContent: dict = json.loads(readFileString(getFileDescriptor(jsonHeaderFilename, "r")))
csvParser = csv.DictReader(CsvFileDescriptor)
for row in csvParser:
jsonHeaderContent["subscriptions"].append(row)
return json.dumps(jsonHeaderContent, indent=4)
def debugPrint(message: str) -> None:
print(f"[*] {message} \n")
def main() -> None:
global csvFilename
try:
debugPrint("Parsing CSV file...")
parsedCsvContent: list = parseCsv(getFileDescriptor(csvFilename, "r"))
writeLinesToFile(getFileDescriptor("parsed_subscriptions.csv", "w"), parsedCsvContent)
convertedCsvToJson: str = convertCsvToJson(getFileDescriptor("parsed_subscriptions.csv", "r"))
writeStringToFile(getFileDescriptor("subscriptions.json", "w"), convertedCsvToJson)
debugPrint(f"Done! file saved as 'subscriptions.json'")
cleanFiles()
except UnicodeDecodeError:
debugPrint("ERROR (UnicodeDecodeError). Please try to run the script with --encode cp437")
cleanFiles()
sys.exit(1)
if __name__ == "__main__":
main()