3 Commits

Author SHA1 Message Date
5b75ef3a27 Added a license header to history. 2025-03-06 20:59:26 +01:00
e7434f976e Merge pull request '#1: created the history class' (#13) from history-class into main
Reviewed-on: #13
2025-02-26 20:47:33 +01:00
fbc843ad6d Finished the History class 2025-02-26 20:36:11 +01:00

View File

@@ -1,15 +1,51 @@
#
# Copyright (c) 2025 Mykola Shulhin.
# Copyright (c) 2025 Fedir Kovalov.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import json import json
from typing import Iterator
from .types import Video
from abc import ABC, abstractmethod
class History: #Abstract class
def __init__(self, filename: str): class History(ABC): # Abstract class
#self.history = list() @abstractmethod
pass def __len__(self) -> int:
def __size__(self):
pass pass
@staticmethod @staticmethod
def parse_history(self, filename: str): def parse_history(filename: str) -> "History":
return FreeTubeHistory(filename)
@abstractmethod
def is_this_type(self, filename: str) -> bool:
pass
@abstractmethod
def get_video(self, index: int) -> Video:
pass
@abstractmethod
def __iter__(self) -> Iterator[Video]:
pass
class FreeTubeHistory(History):
def __init__(self, filename: str) -> None:
parsed_data = [] parsed_data = []
with open(filename, "r", encoding="utf-8") as file: with open(filename, "r", encoding="utf-8") as file:
@@ -20,17 +56,44 @@ class History: #Abstract class
try: try:
parsed_data.append(json.loads(line)) parsed_data.append(json.loads(line))
except json.JSONDecodeError: except json.JSONDecodeError:
fixed_line = fix_unquoted_values(line) fixed_line = FreeTubeHistory._fix_unquoted_values(line)
parsed_data.append(json.loads(fixed_line)) parsed_data.append(json.loads(fixed_line))
return parsed_data self._parsed_data = parsed_data
def is_this_type(self, filename: str): # bool function, @staticmethod
pass # returns false if Youtube history def _fix_unquoted_values(line: str) -> str:
"""Attempts to fix unquoted values by adding quotes around them."""
import re
def get_video(self, index: int): def replacer(match):
pass key, value = match.groups()
if not (value.startswith('"') and value.endswith('"')):
value = f'"{value}"' # Add quotes around the value
return f'"{key}":{value}'
fixed_line = re.sub(r'"(\w+)":(\w+)', replacer, line)
return fixed_line
@staticmethod
def _to_video(entry) -> Video:
return Video(
id=entry["videoId"],
title=entry["title"],
description=entry["description"],
watch_time=entry["timeWatched"],
watch_progress=entry["watchProgress"],
)
def __len__(self):
return len(self._parsed_data)
def is_this_type(self, filename: str) -> bool:
raise NotImplementedError()
def get_video(self, index: int) -> Video:
return FreeTubeHistory._to_video(self._parsed_data[index])
def __iter__(self): def __iter__(self):
pass for entry in self._parsed_data:
yield FreeTubeHistory._to_video(entry)