forked from ticketfrei/ticketfrei
finished changes to class structure
This commit is contained in:
parent
5f2fa46a47
commit
7c00640afa
|
@ -3,9 +3,9 @@
|
||||||
import os
|
import os
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
class Log(object):
|
class Logger(object):
|
||||||
|
|
||||||
def __init__(self, logpath):
|
def __init__(self, logpath=None):
|
||||||
|
|
||||||
# initialize logging
|
# initialize logging
|
||||||
if logpath:
|
if logpath:
|
||||||
|
@ -19,7 +19,7 @@ class Log(object):
|
||||||
Writing an error message & sometimes a traceback to a logfile in logs/
|
Writing an error message & sometimes a traceback to a logfile in logs/
|
||||||
and prints it.
|
and prints it.
|
||||||
|
|
||||||
:param message: (string) Log message to be displayed
|
:param message: (string) Logger message to be displayed
|
||||||
"""
|
"""
|
||||||
time = str(datetime.datetime.now())
|
time = str(datetime.datetime.now())
|
||||||
line = "[" + time + "] " + message + "\n"
|
line = "[" + time + "] " + message + "\n"
|
43
retootbot.py
43
retootbot.py
|
@ -7,10 +7,11 @@ import pickle
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
import trigger
|
import trigger
|
||||||
import log
|
import logger
|
||||||
|
|
||||||
|
|
||||||
class RetootBot(object):
|
class RetootBot(object):
|
||||||
def __init__(self, config, filter, logpath=None):
|
def __init__(self, config, filter, logger):
|
||||||
self.config = config
|
self.config = config
|
||||||
self.filter = filter
|
self.filter = filter
|
||||||
self.register()
|
self.register()
|
||||||
|
@ -23,31 +24,31 @@ class RetootBot(object):
|
||||||
except IOError:
|
except IOError:
|
||||||
self.seen_toots = set()
|
self.seen_toots = set()
|
||||||
|
|
||||||
self.log = log.Log(logpath)
|
self.logger = logger
|
||||||
|
|
||||||
def register(self):
|
def register(self):
|
||||||
self.client_id = os.path.join(
|
self.client_id = os.path.join(
|
||||||
'appkeys',
|
'appkeys',
|
||||||
self.config['mapp']['name'] +
|
self.config['mapp']['name'] +
|
||||||
'@' + self.config['muser']['server']
|
'@' + self.config['muser']['server']
|
||||||
)
|
)
|
||||||
|
|
||||||
if not os.path.isfile(self.client_id):
|
if not os.path.isfile(self.client_id):
|
||||||
mastodon.Mastodon.create_app(
|
mastodon.Mastodon.create_app(
|
||||||
self.config['mapp']['name'],
|
self.config['mapp']['name'],
|
||||||
api_base_url=self.config['muser']['server'],
|
api_base_url=self.config['muser']['server'],
|
||||||
to_file=self.client_id
|
to_file=self.client_id
|
||||||
)
|
)
|
||||||
|
|
||||||
def login(self):
|
def login(self):
|
||||||
self.m = mastodon.Mastodon(
|
self.m = mastodon.Mastodon(
|
||||||
client_id=self.client_id,
|
client_id=self.client_id,
|
||||||
api_base_url=self.config['muser']['server']
|
api_base_url=self.config['muser']['server']
|
||||||
)
|
)
|
||||||
self.m.log_in(
|
self.m.log_in(
|
||||||
self.config['muser']['email'],
|
self.config['muser']['email'],
|
||||||
self.config['muser']['password']
|
self.config['muser']['password']
|
||||||
)
|
)
|
||||||
|
|
||||||
def retoot(self, toots=()):
|
def retoot(self, toots=()):
|
||||||
# toot external provided messages
|
# toot external provided messages
|
||||||
|
@ -64,8 +65,8 @@ class RetootBot(object):
|
||||||
notification['status']['content'])
|
notification['status']['content'])
|
||||||
if not self.filter.is_ok(text_content):
|
if not self.filter.is_ok(text_content):
|
||||||
continue
|
continue
|
||||||
self.log.log('Boosting toot from %s: %s' % (
|
self.logger.log('Boosting toot from %s: %s' % (
|
||||||
#notification['status']['id'],
|
# notification['status']['id'],
|
||||||
notification['status']['account']['acct'],
|
notification['status']['account']['acct'],
|
||||||
notification['status']['content']))
|
notification['status']['content']))
|
||||||
self.m.status_reblog(notification['status']['id'])
|
self.m.status_reblog(notification['status']['id'])
|
||||||
|
@ -89,7 +90,9 @@ if __name__ == '__main__':
|
||||||
config = toml.load(configfile)
|
config = toml.load(configfile)
|
||||||
|
|
||||||
filter = trigger.Trigger(config)
|
filter = trigger.Trigger(config)
|
||||||
bot = RetootBot(config, filter)
|
logger = logger.Logger()
|
||||||
|
|
||||||
|
bot = RetootBot(config, filter, logger)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
bot.retoot()
|
bot.retoot()
|
||||||
|
|
|
@ -7,7 +7,7 @@ import pytoml as toml
|
||||||
import trigger
|
import trigger
|
||||||
from time import sleep
|
from time import sleep
|
||||||
import traceback
|
import traceback
|
||||||
import log
|
import logger
|
||||||
|
|
||||||
|
|
||||||
class RetweetBot(object):
|
class RetweetBot(object):
|
||||||
|
@ -23,13 +23,15 @@ class RetweetBot(object):
|
||||||
last_mention: the ID of the last tweet which mentioned you
|
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.
|
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
|
:param historypath: Path to the file with ID of the last retweeted
|
||||||
Tweet
|
Tweet
|
||||||
:param logpath: Path to the file where the log is stored
|
|
||||||
"""
|
"""
|
||||||
self.config = config
|
self.config = config
|
||||||
|
|
||||||
|
@ -54,7 +56,7 @@ class RetweetBot(object):
|
||||||
self.trigger = trigger
|
self.trigger = trigger
|
||||||
self.waitcounter = 0
|
self.waitcounter = 0
|
||||||
|
|
||||||
self.log = log.Log(logpath)
|
self.logger = logger
|
||||||
|
|
||||||
|
|
||||||
def get_api_keys(self):
|
def get_api_keys(self):
|
||||||
|
@ -135,10 +137,10 @@ class RetweetBot(object):
|
||||||
mentions = self.api.mentions_timeline(since_id=self.last_mention)
|
mentions = self.api.mentions_timeline(since_id=self.last_mention)
|
||||||
return mentions
|
return mentions
|
||||||
except tweepy.RateLimitError:
|
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
|
self.waitcounter += 60*15 + 1
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
self.log.log("Twitter API Error: Bad Connection.")
|
self.logger.log("Twitter API Error: Bad Connection.")
|
||||||
self.waitcounter += 10
|
self.waitcounter += 10
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -152,17 +154,17 @@ class RetweetBot(object):
|
||||||
while 1:
|
while 1:
|
||||||
try:
|
try:
|
||||||
self.api.retweet(status.id)
|
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:
|
if status.id > self.last_mention:
|
||||||
self.last_mention = status.id
|
self.last_mention = status.id
|
||||||
return self.format_mastodon(status)
|
return self.format_mastodon(status)
|
||||||
# maybe one day we get rid of this error. If not, try to uncomment
|
# maybe one day we get rid of this error. If not, try to uncomment
|
||||||
# these lines.
|
# these lines.
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
self.log.log("Twitter API Error: Bad Connection.")
|
self.logger.log("Twitter API Error: Bad Connection.")
|
||||||
sleep(10)
|
sleep(10)
|
||||||
except tweepy.TweepError as error:
|
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)
|
# self.log.log("Twitter API Error: You probably already retweeted this tweet: " + status.text)
|
||||||
if status.id > self.last_mention:
|
if status.id > self.last_mention:
|
||||||
self.last_mention = status.id
|
self.last_mention = status.id
|
||||||
|
@ -181,7 +183,7 @@ class RetweetBot(object):
|
||||||
self.api.update_status(status=post)
|
self.api.update_status(status=post)
|
||||||
return
|
return
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
self.log.log("Twitter API Error: Bad Connection.")
|
self.logger.log("Twitter API Error: Bad Connection.")
|
||||||
sleep(10)
|
sleep(10)
|
||||||
|
|
||||||
def flow(self, to_tweet=()):
|
def flow(self, to_tweet=()):
|
||||||
|
@ -221,7 +223,7 @@ class RetweetBot(object):
|
||||||
logmessage = "Shit went wrong, closing down."
|
logmessage = "Shit went wrong, closing down."
|
||||||
if self.screen_name:
|
if self.screen_name:
|
||||||
logmessage = logmessage + " Sending message to " + self.screen_name
|
logmessage = logmessage + " Sending message to " + self.screen_name
|
||||||
self.log.log(logmessage)
|
self.logger.log(logmessage)
|
||||||
if self.no_shutdown_contact:
|
if self.no_shutdown_contact:
|
||||||
return
|
return
|
||||||
self.save_last_mention()
|
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 :$")
|
self.api.send_direct_message(self.screen_name, "Help! I broke down. restart me pls :$")
|
||||||
except:
|
except:
|
||||||
# traceback.print_exc()
|
# traceback.print_exc()
|
||||||
self.log.log(traceback.extract_tb(sys.exc_info()[2]))
|
self.logger.log(traceback.extract_tb(sys.exc_info()[2]))
|
||||||
print()
|
print()
|
||||||
|
|
||||||
|
|
||||||
|
@ -239,8 +241,9 @@ if __name__ == "__main__":
|
||||||
config = toml.load(configfile)
|
config = toml.load(configfile)
|
||||||
|
|
||||||
trigger = trigger.Trigger(config)
|
trigger = trigger.Trigger(config)
|
||||||
|
logger = logger.Logger()
|
||||||
|
|
||||||
bot = RetweetBot(trigger, config)
|
bot = RetweetBot(trigger, config, logger)
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
bot.flow()
|
bot.flow()
|
||||||
|
@ -248,6 +251,6 @@ if __name__ == "__main__":
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("Good bye! Remember to restart the bot.")
|
print("Good bye! Remember to restart the bot.")
|
||||||
except:
|
except:
|
||||||
bot.log.log(traceback.extract_tb(sys.exc_info()[2]))
|
bot.logger.log(traceback.extract_tb(sys.exc_info()[2]))
|
||||||
print()
|
print()
|
||||||
bot.shutdown()
|
bot.shutdown()
|
|
@ -1,6 +1,7 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import pytoml as toml
|
import pytoml as toml
|
||||||
|
import logger
|
||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
import os
|
import os
|
||||||
|
@ -20,9 +21,10 @@ if __name__ == '__main__':
|
||||||
trigger = Trigger(config)
|
trigger = Trigger(config)
|
||||||
|
|
||||||
logpath = os.path.join("logs", str(datetime.datetime.now()))
|
logpath = os.path.join("logs", str(datetime.datetime.now()))
|
||||||
|
logger = logger.Logger(logpath)
|
||||||
|
|
||||||
mbot = RetootBot(config, trigger, logpath=logpath)
|
mbot = RetootBot(config, trigger, logger)
|
||||||
tbot = RetweetBot(trigger, config, logpath=logpath)
|
tbot = RetweetBot(trigger, config, logger)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
statuses = []
|
statuses = []
|
||||||
|
@ -33,5 +35,5 @@ if __name__ == '__main__':
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("Good bye. Remember to restart the bot!")
|
print("Good bye. Remember to restart the bot!")
|
||||||
except:
|
except:
|
||||||
tbot.log(traceback.extract_tb(sys.exc_info()[2]))
|
tbot.logger(traceback.extract_tb(sys.exc_info()[2]))
|
||||||
tbot.shutdown()
|
tbot.shutdown()
|
||||||
|
|
Loading…
Reference in a new issue