Переглянути джерело

Added Twitter and Mastodon name change functions

DeadTOm 2 роки тому
батько
коміт
7e9146000b
1 змінених файлів з 37 додано та 1 видалено
  1. 37 1
      hooks.py

+ 37 - 1
hooks.py

@@ -8,7 +8,6 @@
 # my Minecraft server
 # TODO: Make routes for various chat and video links
 
-
 try:
     from config import *
     from auth import *
@@ -28,9 +27,14 @@ logging.basicConfig(filename='/var/www/html/webhooks.log', level=logging.INFO)
 #logging.basicConfig(filename='/var/www/html/webhooks.log', level=logging.DEBUG)
 #logging.basicConfig(level=logging.DEBUG)
 
+current_names = []  # Initialize empty list to hold current Twitter and Mastodon 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 = get_m_info.json()  # Jsonify the response
 
 app = Flask(__name__)
 
@@ -40,6 +44,35 @@ def get_now():  # This creates and returns a time stamp
 
 logging.info(f'\n\n\n\n{get_now()} - Webhook called.\n')
 
+def change_t_name():
+    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_online_name)
+
+    logging.info(f'{get_now()} - New Twitter name is {set_t_name["name"]}.')
+
+def change_m_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 = 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_online_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()} - New name is \"{set_m_name["display_name"]}\".')
+
+def write_current_names():  # Write current names to names.py
+    logging.info(f'{get_now()} - Storing {current_names}.')
+    file = open("names.py", "w")
+    for name in current_names:
+        file.write(f'{name}\n')
+    file.close()
+
 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')
@@ -114,6 +147,9 @@ 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
         social_post(msg, discmsg)
+        change_m_name()
+        change_t_name()
+        write_current_names()
     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.'