133 lines
4.2 KiB
Python
133 lines
4.2 KiB
Python
from datetime import timedelta
|
|
from datetime import datetime
|
|
from datetime import timezone
|
|
from environment import *
|
|
from utility import *
|
|
import os
|
|
|
|
class Video:
|
|
def __init__(self):
|
|
self.description=None
|
|
self.url=None
|
|
self.icon=None
|
|
self.timestamp=DateTime()
|
|
self.feedTimeOffset='just now'
|
|
self.feedtime=datetime.now()
|
|
|
|
def __init__(self, description, url):
|
|
self.description=description
|
|
self.url=url
|
|
self.icon=None
|
|
self.timestamp=DateTime()
|
|
self.feedTimeOffset='just now'
|
|
self.feedtime=datetime.now()
|
|
|
|
def __init__(self, description, url, icon, timestamp=None):
|
|
self.description=description
|
|
self.url=url
|
|
self.icon=icon
|
|
if None==timestamp:
|
|
self.timestamp=DateTime()
|
|
else:
|
|
self.timestamp=timestamp
|
|
self.feedTimeOffset='just now'
|
|
self.feedtime=datetime.now()
|
|
|
|
def getDescription(self):
|
|
return self.description
|
|
|
|
def getUrl(self):
|
|
return self.url
|
|
|
|
def getIcon(self):
|
|
return self.icon
|
|
|
|
def getTimestamp(self):
|
|
return self.timestamp
|
|
|
|
# This is a datetime. This time gets calculated by applying the feedtime offset to today.
|
|
def setFeedTime(self,feedtime):
|
|
self.feedtime=feedtime
|
|
|
|
def getFeedTime(self):
|
|
return self.feedtime
|
|
|
|
# This is the <time> section in the video feed. '16 mins ago', 'May 5', 'July 14 2023' etc.,
|
|
def getFeedTimeOffset(self):
|
|
return self.feedTimeOffset
|
|
|
|
def setFeedTimeOffset(self, feedtimeoffset):
|
|
self.feedTimeOffset=feedtimeoffset
|
|
|
|
def toString(self):
|
|
return(self.description+"|"+self.url+"|"+self.icon+"|"+self.timestamp.toString())
|
|
|
|
@staticmethod
|
|
def fromString(line):
|
|
splits=line.split("|")
|
|
description=splits[0].strip()
|
|
url=splits[1].strip()
|
|
icon=splits[2].strip()
|
|
timestamp=DateTime(splits[3].strip())
|
|
return(Video(description,url,icon,timestamp))
|
|
|
|
@staticmethod
|
|
def sort(videoList):
|
|
return sorted(videoList, key=lambda x:x.getTimestamp().toString(),reverse=True)
|
|
|
|
def getPureDescription(self):
|
|
textElement=StringHelper.betweenString(self.description,None,'-')
|
|
timeElement=StringHelper.betweenString(self.description,'-',None)
|
|
durationElement=StringHelper.betweenString(timeElement,' ',' ')
|
|
newDescription=textElement+'-'+' '+ durationElement+' ('+self.getTimestamp().toStringMonthDay()+')'
|
|
return newDescription
|
|
|
|
# This method takes a dictionary of video
|
|
@staticmethod
|
|
def write(pathOutputFile, videos):
|
|
unique={}
|
|
|
|
if None == videos:
|
|
return
|
|
|
|
if os.path.exists(pathOutputFile):
|
|
print('Removing {file}'.format(file=pathOutputFile))
|
|
os.remove(pathOutputFile)
|
|
|
|
videoList=list(videos.values())
|
|
for video in videoList:
|
|
heading = StringHelper.betweenString(video.getDescription(), None, '-')
|
|
if not heading in unique:
|
|
unique[heading]=video
|
|
videoList=Video.sort(list(unique.values()))
|
|
with open(pathOutputFile, "w", encoding='utf-8') as outputStream:
|
|
for video in videoList:
|
|
outputStream.write(video.toString()+'\r\n')
|
|
outputStream.close()
|
|
print('wrote {lines} lines to {file}'.format(lines=len(videoList),file=pathOutputFile))
|
|
return
|
|
|
|
# This method returns a dictionary so caller must use videos=list(load("").values()) to create a list
|
|
@staticmethod
|
|
def load(pathfilename):
|
|
unique={}
|
|
videos={}
|
|
|
|
if not os.path.exists(pathfilename):
|
|
return videos
|
|
try:
|
|
with open(pathfilename, "r", encoding='utf-8') as inputStream:
|
|
for line in inputStream:
|
|
lowerLine=line.lower()
|
|
heading = StringHelper.betweenString(line, None, '-')
|
|
if not heading in unique:
|
|
unique[heading]=heading
|
|
video = Video.fromString(line)
|
|
video.description=video.getPureDescription()
|
|
videos[video.description]=video
|
|
inputStream.close()
|
|
return videos
|
|
except Exception as exception:
|
|
print('Exception reading {file} {exception}'.format(file=pathfilename,exception=exception))
|
|
return None
|