Compare commits

...

6 Commits

Author SHA1 Message Date
jeancf
0fed0843b2 Replace log file by new one 2022-12-04 22:29:40 +01:00
jeancf
bbe2f6e5ab Implemented log file truncation 2022-12-04 22:17:28 +01:00
jeancf
4c38be9194 Added log_days to config file 2022-12-04 20:26:17 +01:00
jeancf
d11fbcaf83 Added mention of logging config option 2022-12-04 18:59:09 +01:00
jeancf
0119d65eea Added logging of new option 2022-12-04 17:28:20 +01:00
jeancf
155f541e18 Added OFF option for verbosity 2022-12-04 17:23:15 +01:00
3 changed files with 56 additions and 13 deletions

View File

@ -13,6 +13,7 @@ It is simple to set-up on a local machine, configurable and feature-rich.
* A footer line can be specified in the config file that gets added to all toots (with e.g. tags)
* Added option to not add reference to "Original tweet" at the bottom of toots
* A password must be provided with `-p` for the first run only. After that it is no longer required.
* The verbosity of logging messages can be set in the config file with `log_level=`.
> Previous updates can be found in CHANGELOG.

View File

@ -69,6 +69,11 @@ subst_youtube = []
subst_reddit = []
# Verbosity of log messages
# One of DEBUG, INFO, WARNING, ERROR, CRITICAL
# One of DEBUG, INFO, WARNING, ERROR, CRITICAL, OFF
# Default is "WARNING"
log_level = "WARNING"
# How many days to keep log messages for
# Log messages older than log_days will be deleted
# Default is 3
log_days = 3

View File

@ -19,9 +19,10 @@
"""
import argparse
import datetime
from datetime import datetime, timedelta
import logging
import os
import shutil
import random
import re
import shutil
@ -90,7 +91,8 @@ def build_config(args):
'subst_twitter': [],
'subst_youtube': [],
'subst_reddit': [],
'log_level': "WARNING"
'log_level': "WARNING",
'log_days': 3,
}
# Create default config object
@ -573,12 +575,6 @@ def main(argv):
mast_password = args['p']
# Remove previous log file
# try:
# os.remove(TOML['config']['twitter_account'] + '.log')
# except FileNotFoundError:
# pass
# Setup logging to file
logging.basicConfig(
filename=TOML['config']['twitter_account'] + '.log',
@ -625,7 +621,9 @@ def main(argv):
logging.info(' subst_twitter : ' + str(TOML['options']['subst_twitter']))
logging.info(' subst_twitter : ' + str(TOML['options']['subst_youtube']))
logging.info(' subst_twitter : ' + str(TOML['options']['subst_reddit']))
logging.info(' log_level : ' + str(TOML['options']['log_level']))
logging.info(' log_days : ' + str(TOML['options']['log_days']))
# Try to open database. If it does not exist, create it
sql = sqlite3.connect('twoot.db')
db = sql.cursor()
@ -716,10 +714,10 @@ def main(argv):
# Extract time stamp
time_string = status.find('span', class_='tweet-date').a.get('title')
try:
timestamp = datetime.datetime.strptime(time_string, '%d/%m/%Y, %H:%M:%S').timestamp()
timestamp = datetime.strptime(time_string, '%d/%m/%Y, %H:%M:%S').timestamp()
except:
# Dec 21, 2021 · 12:00 PM UTC
timestamp = datetime.datetime.strptime(time_string, '%b %d, %Y · %I:%M %p %Z').timestamp()
timestamp = datetime.strptime(time_string, '%b %d, %Y · %I:%M %p %Z').timestamp()
# Check if time is within acceptable range
if not is_time_valid(timestamp):
@ -976,8 +974,47 @@ def main(argv):
logging.info('Deleted ' + str(excess_count) + ' old records from database.')
terminate(0)
# Close logger and log file
logging.shutdown()
# Remove older log messages
# Max allowed age of log message
max_delta = timedelta(TOML['options']['log_days'])
# Open log file
log_file_name = TOML['options']['twitter_account'] + '.log'
new_log_file_name = TOML['options']['twitter_account'] + '.log.new'
log_file = open(log_file_name, 'r')
# Check each line
pos = log_file.tell()
while log_file:
line = log_file.readline()
try:
# Extract date on log line
date = datetime.strptime(line[:10], '%Y-%m-%d')
except ValueError:
# date was not found on this line, try next one
continue
# Time difference between log message and now
log_delta = datetime.now() - date
# Only keep the number of days of the difference
log_delta = timedelta(days=log_delta.days)
if log_delta < max_delta:
# Reset file pointer to position before reading last line
log_file.seek(pos)
remainder = log_file.read()
output_file = open(new_log_file_name, 'w')
output_file.write(remainder)
output_file.close()
# replace log file by new one
shutil.move(new_log_file_name, log_file_name)
break # Exit while loop
pos = log_file.tell()
terminate(0)
if __name__ == "__main__":
main(sys.argv)