bugfix of timestamps

This commit is contained in:
Lucy Joshua 2023-03-22 14:44:54 +02:00 committed by GitHub
parent 8b728793ce
commit c9b12f3ea9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 16 deletions

View file

@ -4,9 +4,10 @@ import re
import requests import requests
import time import time
from connector import Connector from connector import Connector
from connector import printlog
def init_streamobject(conn): def init_streamobject(conn):
print(time.time() + ": " + "Loading Stream object...") printlog("Loading Stream object...")
return BotYamPoster(conn.get_bearer()) return BotYamPoster(conn.get_bearer())
def expand_tco_url(url): def expand_tco_url(url):
@ -29,9 +30,9 @@ def post_reply(conn, victim_bank, tweet, words, reply_text_bank, postcounter):
in_reply_to_tweet_id=tweet["id"] in_reply_to_tweet_id=tweet["id"]
) )
response_data = f"RESPONDING: {res.data['text']}" response_data = f"RESPONDING: {res.data['text']}"
print(time.time() + ": " + response_data) printlog(response_data)
except tweepy.errors.TwitterServerError as e: except tweepy.errors.TwitterServerError as e:
print(time.time() + ": " + "ERROR: An error occured, we'll try again in a few minutes. The error: " + str(e)) printlog("ERROR: An error occured, we'll try again in a few minutes. The error: " + str(e))
time.sleep(5) time.sleep(5)
try: try:
if tweet.data['author_id'] in victim_bank['author_id'] and not "@FromBotYam" in tweet.data['text']: if tweet.data['author_id'] in victim_bank['author_id'] and not "@FromBotYam" in tweet.data['text']:
@ -42,11 +43,11 @@ def post_reply(conn, victim_bank, tweet, words, reply_text_bank, postcounter):
) )
response_data = f"RESPONDING: {res.data['text']}" response_data = f"RESPONDING: {res.data['text']}"
print(time.time() + ": " + response_data) printlog(response_data)
except Exception as e: except Exception as e:
print(time.time() + ": " + "ERROR: An exception occured. The error: " + str(e)) printlog("ERROR: An exception occured. The error: " + str(e))
except Exception as e: except Exception as e:
print(time.time() + ": " + "ERROR: An exception occured. The error: " + str(e)) printlog("ERROR: An exception occured. The error: " + str(e))
postcounter = postcounter + 1 postcounter = postcounter + 1
return postcounter return postcounter
@ -69,11 +70,11 @@ class BotYamPoster(tweepy.StreamingClient):
# Debug # Debug
tweet_data = f"NEW TWEET from @{conn.api.get_user(id=tweet.data['author_id']).data['username']}: {tweet.data['text']}" tweet_data = f"NEW TWEET from @{conn.api.get_user(id=tweet.data['author_id']).data['username']}: {tweet.data['text']}"
print(time.time() + ": " + tweet_data) printlog(tweet_data)
# Spare me if starts with RT # Spare me if starts with RT
if tweet.data['text'][:2] == "RT": if tweet.data['text'][:2] == "RT":
print(time.time() + ": " + "Skipping retweet...") printlog("Skipping retweet...")
return return
# Run on all gags # Run on all gags
@ -99,7 +100,7 @@ class BotYamPoster(tweepy.StreamingClient):
postcounter) postcounter)
if (postcounter == 0 and "@FromBotYam" in tweet.data['text']): if (postcounter == 0 and "@FromBotYam" in tweet.data['text']):
print(time.time() + ": " + "Post counter for this tweet: 0! Posting tilt.") printlog("Post counter for this tweet: 0! Posting tilt.")
if any(tilter in tweet.data['text'] for tilter in reply_bank['special_gags']['tilt']['keywords']): if any(tilter in tweet.data['text'] for tilter in reply_bank['special_gags']['tilt']['keywords']):
postcounter = post_reply( postcounter = post_reply(
@ -122,4 +123,4 @@ class BotYamPoster(tweepy.StreamingClient):
# Define a callback function to handle errors # Define a callback function to handle errors
def on_error(self, status_code): def on_error(self, status_code):
# Print the error code # Print the error code
print(time.time() + ": " + "ERROR: " + status_code) printlog("ERROR: " + status_code)

View file

@ -2,10 +2,14 @@ import os
import redis import redis
import time import time
import tweepy import tweepy
from datetime import datetime
def printlog(text):
print((datetime.fromtimestamp(time.time())).strftime("%d-%m-%Y, %H:%M:%S") + ": " + text)
class Connector(): class Connector():
def __init__(self): def __init__(self):
print(time.time() + ": " + "Loading Redis connector...") printlog("Loading Redis connector...")
# TODO: Add token # TODO: Add token
self.dbconn = redis.Redis( self.dbconn = redis.Redis(
host=os.getenv("REDIS_HOST"), host=os.getenv("REDIS_HOST"),
@ -14,11 +18,11 @@ class Connector():
) )
self.api = Connector.init_api(self.dbconn) self.api = Connector.init_api(self.dbconn)
print(time.time() + ": " + "Success loading connectors!") printlog("Success loading connectors!")
def init_api(dbconn): def init_api(dbconn):
# Syslog report # Syslog report
print(time.time() + ": " + "Loading Twitter API connector...") printlog("Loading Twitter API connector...")
return tweepy.Client( return tweepy.Client(
bearer_token=dbconn.hget("api", "bearer").decode("utf-8"), bearer_token=dbconn.hget("api", "bearer").decode("utf-8"),

View file

@ -4,16 +4,17 @@ import time
from BotYamPoster import BotYamPoster from BotYamPoster import BotYamPoster
from BotYamPoster import init_streamobject from BotYamPoster import init_streamobject
from connector import Connector from connector import Connector
from connector import printlog
def main(): def main():
print(time.time() + ": " + "BOT-YAM - VERSION 3.0.2 >>>>") printlog("BOT-YAM - VERSION 3.0.2 >>>>")
conn = Connector() conn = Connector()
stream = init_streamobject(conn) stream = init_streamobject(conn)
print(time.time() + ": " + "Adding stream rules...") printlog("Adding stream rules...")
stream.add_rules(tweepy.StreamRule(conn.get_victims()['stream_filter'])) stream.add_rules(tweepy.StreamRule(conn.get_victims()['stream_filter']))
stream.add_rules(tweepy.StreamRule("@FromBotYam")) stream.add_rules(tweepy.StreamRule("@FromBotYam"))
# Start listening for tweets # Start listening for tweets
print(time.time() + ": " + "Starting Twitter stream!") printlog("Starting Twitter stream!")
stream.filter(expansions="author_id") stream.filter(expansions="author_id")
if __name__ == "__main__": if __name__ == "__main__":