|
@@ -7,14 +7,13 @@
|
|
|
# TODO: Make routes for various chat and video links
|
|
|
|
|
|
try:
|
|
|
- from names import t_name, m_name
|
|
|
+ from names import m_name
|
|
|
from config import *
|
|
|
from auth import *
|
|
|
from flask import Flask, jsonify, current_app, request
|
|
|
import requests
|
|
|
import logging
|
|
|
import time
|
|
|
- from twython import Twython, TwythonError
|
|
|
import socket
|
|
|
from mcstatus import JavaServer
|
|
|
except Exception as import_error: # Log any errors loading modules, and try to keep running
|
|
@@ -24,14 +23,11 @@ except Exception as import_error: # Log any errors loading modules, and try to
|
|
|
|
|
|
logging.basicConfig(filename='/var/www/html/webhooks.log', level=logging.INFO)
|
|
|
|
|
|
-current_names = [] # Initialize empty list to hold current Twitter and Mastodon names
|
|
|
+current_names = [] # Initialize empty list to hold current names
|
|
|
|
|
|
testing = 0 # Are we testing? 1 for testing. 0 for live.
|
|
|
|
|
|
-twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
|
|
|
-get_t_info = twitter.verify_credentials() # Get current Twitter name
|
|
|
-
|
|
|
-get_m_info = requests.get('https://fosstodon.org/api/v1/accounts/verify_credentials', headers={'Authorization': m_api_key, 'Accept': 'application/json'}) # Get Mastodon account information
|
|
|
+get_m_info = requests.get('https://fosstodon.org/api/v1/accounts/verify_credentials', headers={'Authorization': m_api_key, 'Accept': 'application/json'}) # Get Mastodon account information
|
|
|
get_m_info = get_m_info.json() # Jsonify the response
|
|
|
|
|
|
app = Flask(__name__)
|
|
@@ -41,41 +37,30 @@ def get_now(): # This creates and returns a time stamp
|
|
|
now = str(time.strftime("%Y/%m/%d %H:%M:%S"))
|
|
|
return now
|
|
|
|
|
|
-logging.info(f'\n\n\n\n{get_now()} - Webhook called.\n')
|
|
|
-
|
|
|
-def streaming_t_name(): # Change Twitter name to show we're streaming'
|
|
|
- get_t_info = twitter.verify_credentials() # Get current Twitter name
|
|
|
- current_t_name = f't_name = \'{get_t_info["name"]}\'' # Create Twitter list entry
|
|
|
- current_names.append(current_t_name) # Append to list of current names
|
|
|
- logging.info(f'{get_now()} - Current Twitter name is \"{get_t_info["name"]}\".')
|
|
|
|
|
|
- set_t_name = twitter.update_profile(name=t_streaming_name)
|
|
|
+logging.info(f'\n\n\n\n{get_now()} - Webhook called.\n')
|
|
|
|
|
|
- logging.info(f'{get_now()} - Twitter name is now {set_t_name["name"]}.')
|
|
|
|
|
|
def streaming_m_name(): # Change Mastodon name to show we're streaming
|
|
|
- get_m_info = requests.get('https://fosstodon.org/api/v1/accounts/verify_credentials', headers={'Authorization': m_api_key, 'Accept': 'application/json'}) # Get Mastodon account information
|
|
|
+ get_m_info = requests.get('https://fosstodon.org/api/v1/accounts/verify_credentials', headers={'Authorization': m_api_key, 'Accept': 'application/json'}) # Get Mastodon account information
|
|
|
get_m_info = get_m_info.json() # Jsonify the response
|
|
|
current_m_name = f'm_name = \'{get_m_info["display_name"]}\'' # Create Mastodon list entry
|
|
|
current_names.append(current_m_name) # Append to list of current names
|
|
|
logging.info(f'{get_now()} - Current Mastodon name is \"{get_m_info["display_name"]}\".')
|
|
|
|
|
|
- set_m_name = requests.patch('https://fosstodon.org/api/v1/accounts/update_credentials', params={'display_name': m_streaming_name}, headers={'Authorization': m_api_key, 'Accept': 'application/json'}) # Set Mastodon display name
|
|
|
+ set_m_name = requests.patch('https://fosstodon.org/api/v1/accounts/update_credentials', params={'display_name': m_streaming_name}, headers={'Authorization': m_api_key, 'Accept': 'application/json'}) # Set Mastodon display name
|
|
|
set_m_name = set_m_name.json() # Jsonify the response
|
|
|
|
|
|
logging.info(f'{get_now()} - Mastodon name is now \"{set_m_name["display_name"]}\".')
|
|
|
|
|
|
-def reg_t_name(): # Change Twitter name to regular name
|
|
|
- set_t_name = twitter.update_profile(name=t_name)
|
|
|
-
|
|
|
- logging.info(f'{get_now()} - Twitter name is now {set_t_name["name"]}.')
|
|
|
|
|
|
def reg_m_name(): # Change Mastodon name to regular name
|
|
|
- set_m_name = requests.patch('https://fosstodon.org/api/v1/accounts/update_credentials', params={'display_name': m_name}, headers={'Authorization': m_api_key, 'Accept': 'application/json'}) # Set Mastodon display name
|
|
|
+ set_m_name = requests.patch('https://fosstodon.org/api/v1/accounts/update_credentials', params={'display_name': m_name}, headers={'Authorization': m_api_key, 'Accept': 'application/json'}) # Set Mastodon display name
|
|
|
set_m_name = set_m_name.json() # Jsonify the response
|
|
|
|
|
|
logging.info(f'{get_now()} - Mastodon name is now \"{set_m_name["display_name"]}\".')
|
|
|
|
|
|
+
|
|
|
def write_current_names(): # Write current names to names.py
|
|
|
logging.info(f'{get_now()} - Storing {current_names}.')
|
|
|
try:
|
|
@@ -86,6 +71,7 @@ def write_current_names(): # Write current names to names.py
|
|
|
except Exception as write_error:
|
|
|
logging.info(f'{get_now()} - {write_error}')
|
|
|
|
|
|
+
|
|
|
def mc_chat(mc_msg): # Send chat message to Minecraft chat
|
|
|
logging.info(f'{get_now()} - Checking Minecraft server for players.')
|
|
|
mc_server = JavaServer.lookup('mc.deadtom.me:25565')
|
|
@@ -96,16 +82,31 @@ def mc_chat(mc_msg): # Send chat message to Minecraft chat
|
|
|
logging.info(f'{get_now()} - Connecting...')
|
|
|
sock_host = 'mc.deadtom.me'
|
|
|
sock_port = 6791
|
|
|
- mySocket = socket.socket()
|
|
|
- mySocket.connect((sock_host, sock_port))
|
|
|
- time.sleep(1)
|
|
|
- logging.info(f'{get_now()} - Connected. Sending {mc_msg}...')
|
|
|
- mySocket.send(mc_msg.encode())
|
|
|
- logging.info(f'{get_now()} - sent.')
|
|
|
- mySocket.close()
|
|
|
+ try:
|
|
|
+ mySocket = socket.socket()
|
|
|
+ mySocket.connect((sock_host, sock_port))
|
|
|
+ time.sleep(1)
|
|
|
+ logging.info(f'{get_now()} - Connected. Sending {mc_msg}...')
|
|
|
+ mySocket.send(mc_msg.encode())
|
|
|
+ logging.info(f'{get_now()} - sent.')
|
|
|
+ mySocket.close()
|
|
|
+ except ConnectionRefusedError as error:
|
|
|
+ logging.info(f'{get_now()} - Unable to contact {sock_host} - {error}.')
|
|
|
+ mySocket.close()
|
|
|
+ except TimeoutError as error:
|
|
|
+ logging.info(f'{get_now()} - Unable to contact {sock_host} - {error}.')
|
|
|
+ mySocket.close()
|
|
|
+ except OSError as error:
|
|
|
+ logging.info(f'{get_now()} - Unable to contact {sock_host} - {error}.')
|
|
|
+ mySocket.close()
|
|
|
else: # If I'm not on the server, don't send the message
|
|
|
logging.info(f'{get_now()} - DeadTOm is not on the server, so not sending message.')
|
|
|
|
|
|
+
|
|
|
+def chat_bot(bot_msg): # Send chat message to chatbot
|
|
|
+ logging.info(f'{get_now()} - {bot_msg}')
|
|
|
+
|
|
|
+
|
|
|
def set_hashtags(title): # Sets up hash tags to be appended to social media
|
|
|
logging.info(f'{get_now()} - Examining stream title \"{title}\" to apply hashtags.')
|
|
|
check_title = title.lower()
|
|
@@ -120,6 +121,7 @@ def set_hashtags(title): # Sets up hash tags to be appended to social media
|
|
|
logging.info(f'{get_now()} - Adding {tags} to title.')
|
|
|
return tags
|
|
|
|
|
|
+
|
|
|
def social_post(msg, discmsg): # Post to Mastodon
|
|
|
# Send to Mastodon
|
|
|
logging.info(f'{get_now()} - Posting to Mastodon.')
|
|
@@ -132,10 +134,6 @@ def social_post(msg, discmsg): # Post to Mastodon
|
|
|
for line in json_response:
|
|
|
logging.debug(f'{get_now()} - API returned: {line}: {json_response[line]}')
|
|
|
|
|
|
- # Send to Twitter
|
|
|
- twitter.update_status(status=msg) # Tweet the message
|
|
|
- logging.info(f'{get_now()} - Posted to Twitter.')
|
|
|
-
|
|
|
# Send to Discord
|
|
|
# for all params, see https://discordapp.com/developers/docs/resources/webhook#execute-webhook
|
|
|
data = {
|
|
@@ -148,6 +146,7 @@ def social_post(msg, discmsg): # Post to Mastodon
|
|
|
logging.info(
|
|
|
f'{get_now()} - Discord message not sent with {talkback.status_code}, response:\n{talkback.json()}')
|
|
|
|
|
|
+
|
|
|
@app.route('/stream_started/', methods=["POST"])
|
|
|
def start():
|
|
|
logging.info(f'{get_now()} - stream_started request')
|
|
@@ -160,15 +159,14 @@ def start():
|
|
|
discmsg = f'DeadTOm is streaming on Owncast, at https://owncast.deadtom.me. {stream_title}'
|
|
|
if testing != 1: # If we're testing, don't actually send out notification
|
|
|
streaming_m_name()
|
|
|
- streaming_t_name()
|
|
|
- write_current_names()
|
|
|
social_post(msg, discmsg)
|
|
|
else:
|
|
|
logging.info(f'-----------------------\n\n'
|
|
|
f'{get_now()} - Currently running in test mode. We are streaming, but no notifications are being sent to social media.'
|
|
|
f'\n\n---------------------------------')
|
|
|
|
|
|
- return jsonify({"Data": raw_data,})
|
|
|
+ return jsonify({"Data": raw_data, })
|
|
|
+
|
|
|
|
|
|
@app.route('/stream_stopped/', methods=["POST"])
|
|
|
def stop():
|
|
@@ -181,10 +179,10 @@ def stop():
|
|
|
discmsg = f'DeadTOm is done streaming.'
|
|
|
if testing != 1: # If we're testing, don't actually send out notification
|
|
|
reg_m_name()
|
|
|
- reg_t_name()
|
|
|
social_post(msg, discmsg)
|
|
|
|
|
|
- return jsonify({"Data": raw_data,})
|
|
|
+ return jsonify({"Data": raw_data, })
|
|
|
+
|
|
|
|
|
|
@app.route('/user_joined/', methods=["POST"])
|
|
|
def joined():
|
|
@@ -197,7 +195,8 @@ def joined():
|
|
|
mc_chat(ownchat_msg)
|
|
|
logging.info(f'{get_now()} - Sent to Minecraft server.')
|
|
|
|
|
|
- return jsonify({"Data": raw_data,})
|
|
|
+ return jsonify({"Data": raw_data, })
|
|
|
+
|
|
|
|
|
|
@app.route('/name_changed/', methods=["POST"])
|
|
|
def changed():
|
|
@@ -214,7 +213,8 @@ def changed():
|
|
|
mc_chat(ownchat_msg)
|
|
|
logging.info(f'{get_now()} - Sent to Minecraft server.')
|
|
|
|
|
|
- return jsonify({"Data": raw_data,})
|
|
|
+ return jsonify({"Data": raw_data, })
|
|
|
+
|
|
|
|
|
|
@app.route('/message_sent/', methods=["POST"])
|
|
|
def sent():
|
|
@@ -228,7 +228,21 @@ def sent():
|
|
|
mc_chat(ownchat_msg)
|
|
|
logging.info(f'{get_now()} - Sent to Minecraft server.')
|
|
|
|
|
|
- return jsonify({"Data": raw_data,})
|
|
|
+ return jsonify({"Data": raw_data, })
|
|
|
+
|
|
|
+
|
|
|
+@app.route('/chatbot/', methods=["POST"])
|
|
|
+def send_chatbot():
|
|
|
+ logging.info(f'{get_now()} - message_sent request')
|
|
|
+ raw_data = request.get_json(force=True) # Get the raw data
|
|
|
+ event_data = raw_data['eventData']
|
|
|
+ chatter_name = event_data['user']['displayName']
|
|
|
+ chat_msg = event_data['rawBody']
|
|
|
+ bot_msg = f'{chatter_name}, {chat_msg}'
|
|
|
+ chat_bot(bot_msg)
|
|
|
+ logging.info(f'{get_now()} - Sent to chat bot.')
|
|
|
+
|
|
|
+ return jsonify({"Data": raw_data, })
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|