Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovear committed Sep 29, 2022
1 parent 8eecbb8 commit 711ed9c
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from PIL import Image
from io import BytesIO
import json
from numbers import Number

from .args import get_args
from .logger import logger
Expand Down Expand Up @@ -292,10 +293,10 @@ def clean_post(self, post:dict, user:dict, domain:str):
new_post['post_variables']['username'] = user['name']
new_post['post_variables']['site'] = domain
new_post['post_variables']['service'] = post['service']
new_post['post_variables']['added'] = datetime.datetime.strptime(post['added'], r'%a, %d %b %Y %H:%M:%S %Z').strftime(self.date_strf_pattern) if post['added'] else None
new_post['post_variables']['updated'] = datetime.datetime.strptime(post['edited'], r'%a, %d %b %Y %H:%M:%S %Z').strftime(self.date_strf_pattern) if post['edited'] else None
new_post['post_variables']['user_updated'] = datetime.datetime.strptime(user['updated'], r'%a, %d %b %Y %H:%M:%S %Z').strftime(self.date_strf_pattern) if user['updated'] else None
new_post['post_variables']['published'] = datetime.datetime.strptime(post['published'], r'%a, %d %b %Y %H:%M:%S %Z').strftime(self.date_strf_pattern) if post['published'] else None
new_post['post_variables']['added'] = self.format_time_by_type(post['added']) if post['added'] else None
new_post['post_variables']['updated'] = self.format_time_by_type(post['edited']) if post['edited'] else None
new_post['post_variables']['user_updated'] = self.format_time_by_type(user['updated']) if user['updated'] else None
new_post['post_variables']['published'] = self.format_time_by_type(post['published']) if post['published'] else None

new_post['post_path'] = compile_post_path(new_post['post_variables'], self.download_path_template, self.restrict_ascii)

Expand Down Expand Up @@ -544,7 +545,7 @@ def write_archive(self, post:dict):
def skip_user(self, user:dict):
# check last update date
if self.user_up_datebefore or self.user_up_dateafter:
if check_date(datetime.datetime.strptime(user['updated'], r'%a, %d %b %Y %H:%M:%S %Z'), None, self.user_up_datebefore, self.user_up_dateafter):
if check_date(self.get_date_by_type(user['updated']), None, self.user_up_datebefore, self.user_up_dateafter):
logger.info("Skipping user | user updated date not in range")
return True
return False
Expand All @@ -560,7 +561,7 @@ def skip_post(self, post:dict):
if not post['post_variables']['published']:
logger.info("Skipping post | post published date not in range")
return True
elif check_date(datetime.datetime.strptime(post['post_variables']['published'], self.date_strf_pattern), self.date, self.datebefore, self.dateafter):
elif check_date(self.get_date_by_type(post['post_variables']['published']), self.date, self.datebefore, self.dateafter):
logger.info("Skipping post | post published date not in range")
return True

Expand Down Expand Up @@ -667,5 +668,20 @@ def start_download(self):
except:
logger.exception(f"Unable to get posts for {url}")

def get_date_by_type(self, time):
if isinstance(time, Number):
t = datetime.datetime.fromtimestamp(time)
elif isinstance(time, str):
t = datetime.datetime.strptime(time, r'%a, %d %b %Y %H:%M:%S %Z')
elif time == None:
return None
else:
raise Exception(f'Can not format time {time}')
return t

def format_time_by_type(self, time):
t = self.get_date_by_type(time)
return t.strftime(self.date_strf_pattern) if t != None else t

def main():
downloader(get_args())

0 comments on commit 711ed9c

Please sign in to comment.