Cringe fix

This commit is contained in:
2022-07-01 01:04:20 +02:00
parent cafaf77d28
commit cc7aaf19ec
2 changed files with 14 additions and 21 deletions

View File

@ -9,7 +9,13 @@ import string
import datetime import datetime
import collections import collections
from contextlib import closing from contextlib import closing
try:
import pytz import pytz
have_pytz = True
except ModuleNotFoundError:
have_pytz = False
import requests import requests
from requests.models import urlencode from requests.models import urlencode
import dateutil import dateutil
@ -3176,11 +3182,11 @@ class Mastodon:
""" """
date_time_utc = None date_time_utc = None
if date_time.tzinfo is None: if date_time.tzinfo is None:
date_time_utc = date_time.replace(tzinfo=pytz.utc) date_time_utc = date_time.replace(tzinfo=pytz.utc) if have_pytz else date_time
else: else:
date_time_utc = date_time.astimezone(pytz.utc) date_time_utc = date_time.astimezone(pytz.utc) if have_pytz else date_time
epoch_utc = datetime.datetime.utcfromtimestamp(0).replace(tzinfo=pytz.utc) epoch_utc = datetime.datetime.utcfromtimestamp(0).replace(tzinfo=pytz.utc) if have_pytz else datetime.datetime.utcfromtimestamp(0)
return (date_time_utc - epoch_utc).total_seconds() return (date_time_utc - epoch_utc).total_seconds()
@ -3214,7 +3220,7 @@ class Mastodon:
if v != None: if v != None:
try: try:
if isinstance(v, int): if isinstance(v, int):
json_object[k] = datetime.datetime.fromtimestamp(v, pytz.utc) json_object[k] = datetime.datetime.fromtimestamp(v, pytz.utc) if have_pytz else datetime.datetime
else: else:
json_object[k] = dateutil.parser.parse(v) json_object[k] = dateutil.parser.parse(v)
except: except:
@ -3266,7 +3272,7 @@ class Mastodon:
every time instead of randomly doing different things on some systems every time instead of randomly doing different things on some systems
and also it represents that time as the equivalent UTC time. and also it represents that time as the equivalent UTC time.
""" """
isotime = datetime_val.astimezone(pytz.utc).strftime("%Y-%m-%dT%H:%M:%S%z") isotime = datetime_val.astimezone(pytz.utc).strftime("%Y-%m-%dT%H:%M:%S%z") if have_pytz else datetime_val.strftime("%Y-%m-%dT%H:%M:%S%z")
if isotime[-2] != ":": if isotime[-2] != ":":
isotime = isotime[:-2] + ":" + isotime[-2:] isotime = isotime[:-2] + ":" + isotime[-2:]
return isotime return isotime
@ -3366,7 +3372,7 @@ class Mastodon:
self.ratelimit_reset += server_time_diff self.ratelimit_reset += server_time_diff
self.ratelimit_lastcall = time.time() self.ratelimit_lastcall = time.time()
except Exception as e: except Exception as e:
raise MastodonRatelimitError("Rate limit time calculations failed: %s" % e) pass #raise MastodonRatelimitError("Rate limit time calculations failed: %s" % e)
# Handle response # Handle response
if self.debug_requests: if self.debug_requests:

View File

@ -8,7 +8,7 @@
| ================================= """ | ================================= """
from Libs.bs4 import BeautifulSoup from Libs.bs4 import BeautifulSoup
#from Libs.mastodon import Mastodon from Libs.mastodon import Mastodon
from Modules.Utils import * from Modules.Utils import *
def MastodonGetSession(MastodonURL, MastodonToken): def MastodonGetSession(MastodonURL, MastodonToken):
@ -33,7 +33,6 @@ def MastodonDoPost(Session, Text, Lang=None, Visibility='public'):
# TODO: Set a limit/cooldown on how many new posts at a time can be posted, or ignore posts older than date X.. otherwise if someone starts using this after having written 100 blog posts, bad things will happen # TODO: Set a limit/cooldown on how many new posts at a time can be posted, or ignore posts older than date X.. otherwise if someone starts using this after having written 100 blog posts, bad things will happen
def MastodonShare(MastodonURL, MastodonToken, Pages, SiteDomain, SiteLang, Locale): def MastodonShare(MastodonURL, MastodonToken, Pages, SiteDomain, SiteLang, Locale):
#return None
Session = MastodonGetSession(MastodonURL, MastodonToken) Session = MastodonGetSession(MastodonURL, MastodonToken)
Posts = MastodonGetPostsFromUserID( Posts = MastodonGetPostsFromUserID(
Session, Session,
@ -42,10 +41,7 @@ def MastodonShare(MastodonURL, MastodonToken, Pages, SiteDomain, SiteLang, Local
Parse = BeautifulSoup(e['content'], 'html.parser') Parse = BeautifulSoup(e['content'], 'html.parser')
Posts[i] = { Posts[i] = {
'URL': e['uri'], 'URL': e['uri'],
#e['content'],
'LastLink': Parse.find_all('a')[-1]['href'] if Parse.a else None} 'LastLink': Parse.find_all('a')[-1]['href'] if Parse.a else None}
#print(i['uri'], i['content'])
#print(Posts)
Pages.sort() Pages.sort()
for File, Content, Titles, Meta, HTMLContent, Description, Image in Pages: for File, Content, Titles, Meta, HTMLContent, Description, Image in Pages:
if Meta['Type'] == 'Post': if Meta['Type'] == 'Post':
@ -54,13 +50,7 @@ def MastodonShare(MastodonURL, MastodonToken, Pages, SiteDomain, SiteLang, Local
Paragraphs = Parse.p.get_text().split('\n') Paragraphs = Parse.p.get_text().split('\n')
Read = '...' + Locale['ReadFullPost'] + ':\n' Read = '...' + Locale['ReadFullPost'] + ':\n'
URL = '{}/{}.html'.format(SiteDomain, StripExt(File)) URL = '{}/{}.html'.format(SiteDomain, StripExt(File))
#Desc = '...' + Read + ':\n' + URL
#DescLen = len(Read) + 30
#if not Paragraphs[0]:
# Paragraphs.pop(0)
for p in Paragraphs: for p in Paragraphs:
#while len(Description) <= 450:
#if p and len(Description+p)+2 <= 450:
if p and len(Read+Desc+p)+25 < 500: if p and len(Read+Desc+p)+25 < 500:
Desc += p + '\n\n' Desc += p + '\n\n'
else: else:
@ -68,8 +58,6 @@ def MastodonShare(MastodonURL, MastodonToken, Pages, SiteDomain, SiteLang, Local
break break
else: else:
Desc = p[:500-25-5-len(Read)] + '...' Desc = p[:500-25-5-len(Read)] + '...'
#if len(Description) > 450:
# Description = Description[:450] + '...'
DoPost = True DoPost = True
for p in Posts: for p in Posts:
if p['LastLink'] == URL: if p['LastLink'] == URL:
@ -80,4 +68,3 @@ def MastodonShare(MastodonURL, MastodonToken, Pages, SiteDomain, SiteLang, Local
Session, Session,
Desc + Read + URL, Desc + Read + URL,
SiteLang) SiteLang)
#BodyDescription = Parse.p.get_text()[:150].replace('\n', ' ').replace('"', "'") + '...'