finished changes to class structure

This commit is contained in:
b3yond 2017-12-30 16:33:34 +01:00
parent 5f2fa46a47
commit 7c00640afa
4 changed files with 48 additions and 40 deletions

View file

@ -3,9 +3,9 @@
import os
import datetime
class Log(object):
class Logger(object):
def __init__(self, logpath):
def __init__(self, logpath=None):
# initialize logging
if logpath:
@ -19,7 +19,7 @@ class Log(object):
Writing an error message & sometimes a traceback to a logfile in logs/
and prints it.
:param message: (string) Log message to be displayed
:param message: (string) Logger message to be displayed
"""
time = str(datetime.datetime.now())
line = "[" + time + "] " + message + "\n"

View file

@ -7,10 +7,11 @@ import pickle
import re
import time
import trigger
import log
import logger
class RetootBot(object):
def __init__(self, config, filter, logpath=None):
def __init__(self, config, filter, logger):
self.config = config
self.filter = filter
self.register()
@ -23,7 +24,7 @@ class RetootBot(object):
except IOError:
self.seen_toots = set()
self.log = log.Log(logpath)
self.logger = logger
def register(self):
self.client_id = os.path.join(
@ -64,8 +65,8 @@ class RetootBot(object):
notification['status']['content'])
if not self.filter.is_ok(text_content):
continue
self.log.log('Boosting toot from %s: %s' % (
#notification['status']['id'],
self.logger.log('Boosting toot from %s: %s' % (
# notification['status']['id'],
notification['status']['account']['acct'],
notification['status']['content']))
self.m.status_reblog(notification['status']['id'])
@ -89,7 +90,9 @@ if __name__ == '__main__':
config = toml.load(configfile)
filter = trigger.Trigger(config)
bot = RetootBot(config, filter)
logger = logger.Logger()
bot = RetootBot(config, filter, logger)
while True:
bot.retoot()

View file

@ -7,7 +7,7 @@ import pytoml as toml
import trigger
from time import sleep
import traceback
import log
import logger
class RetweetBot(object):
@ -23,13 +23,15 @@ class RetweetBot(object):
last_mention: the ID of the last tweet which mentioned you
"""
def __init__(self, trigger, config, historypath="last_mention", logpath=None):
def __init__(self, trigger, config, logger, historypath="last_mention"):
"""
Initializes the bot and loads all the necessary data.
:param trigger: object of the trigger
:param config: object of the config
:param logger: object of the logger
:param historypath: Path to the file with ID of the last retweeted
Tweet
:param logpath: Path to the file where the log is stored
"""
self.config = config
@ -54,7 +56,7 @@ class RetweetBot(object):
self.trigger = trigger
self.waitcounter = 0
self.log = log.Log(logpath)
self.logger = logger
def get_api_keys(self):
@ -135,10 +137,10 @@ class RetweetBot(object):
mentions = self.api.mentions_timeline(since_id=self.last_mention)
return mentions
except tweepy.RateLimitError:
self.log.log("Twitter API Error: Rate Limit Exceeded.")
self.logger.log("Twitter API Error: Rate Limit Exceeded.")
self.waitcounter += 60*15 + 1
except requests.exceptions.ConnectionError:
self.log.log("Twitter API Error: Bad Connection.")
self.logger.log("Twitter API Error: Bad Connection.")
self.waitcounter += 10
return None
@ -152,17 +154,17 @@ class RetweetBot(object):
while 1:
try:
self.api.retweet(status.id)
self.log.log("Retweeted: " + self.format_mastodon(status))
self.logger.log("Retweeted: " + self.format_mastodon(status))
if status.id > self.last_mention:
self.last_mention = status.id
return self.format_mastodon(status)
# maybe one day we get rid of this error. If not, try to uncomment
# these lines.
except requests.exceptions.ConnectionError:
self.log.log("Twitter API Error: Bad Connection.")
self.logger.log("Twitter API Error: Bad Connection.")
sleep(10)
except tweepy.TweepError as error:
self.log.log("Twitter Error " + error.api_code + ": " + error.reason + error.response)
self.logger.log("Twitter Error " + error.api_code + ": " + error.reason + error.response)
# self.log.log("Twitter API Error: You probably already retweeted this tweet: " + status.text)
if status.id > self.last_mention:
self.last_mention = status.id
@ -181,7 +183,7 @@ class RetweetBot(object):
self.api.update_status(status=post)
return
except requests.exceptions.ConnectionError:
self.log.log("Twitter API Error: Bad Connection.")
self.logger.log("Twitter API Error: Bad Connection.")
sleep(10)
def flow(self, to_tweet=()):
@ -221,7 +223,7 @@ class RetweetBot(object):
logmessage = "Shit went wrong, closing down."
if self.screen_name:
logmessage = logmessage + " Sending message to " + self.screen_name
self.log.log(logmessage)
self.logger.log(logmessage)
if self.no_shutdown_contact:
return
self.save_last_mention()
@ -229,7 +231,7 @@ class RetweetBot(object):
self.api.send_direct_message(self.screen_name, "Help! I broke down. restart me pls :$")
except:
# traceback.print_exc()
self.log.log(traceback.extract_tb(sys.exc_info()[2]))
self.logger.log(traceback.extract_tb(sys.exc_info()[2]))
print()
@ -239,8 +241,9 @@ if __name__ == "__main__":
config = toml.load(configfile)
trigger = trigger.Trigger(config)
logger = logger.Logger()
bot = RetweetBot(trigger, config)
bot = RetweetBot(trigger, config, logger)
try:
while True:
bot.flow()
@ -248,6 +251,6 @@ if __name__ == "__main__":
except KeyboardInterrupt:
print("Good bye! Remember to restart the bot.")
except:
bot.log.log(traceback.extract_tb(sys.exc_info()[2]))
bot.logger.log(traceback.extract_tb(sys.exc_info()[2]))
print()
bot.shutdown()

View file

@ -1,6 +1,7 @@
#!/usr/bin/env python3
import pytoml as toml
import logger
import time
import traceback
import os
@ -20,9 +21,10 @@ if __name__ == '__main__':
trigger = Trigger(config)
logpath = os.path.join("logs", str(datetime.datetime.now()))
logger = logger.Logger(logpath)
mbot = RetootBot(config, trigger, logpath=logpath)
tbot = RetweetBot(trigger, config, logpath=logpath)
mbot = RetootBot(config, trigger, logger)
tbot = RetweetBot(trigger, config, logger)
try:
statuses = []
@ -33,5 +35,5 @@ if __name__ == '__main__':
except KeyboardInterrupt:
print("Good bye. Remember to restart the bot!")
except:
tbot.log(traceback.extract_tb(sys.exc_info()[2]))
tbot.logger(traceback.extract_tb(sys.exc_info()[2]))
tbot.shutdown()