Преглед изворни кода

Removing wild prints and now correctly logging errors

Lucas Villeneuve пре 5 година
родитељ
комит
233b507811
6 измењених фајлова са 32 додато и 44 уклоњено
  1. 0 3
      myanimebot.py
  2. 23 31
      myanimebot/anilist.py
  3. 5 3
      myanimebot/discord.py
  4. 0 1
      myanimebot/myanimelist.py
  5. 4 5
      myanimebot/utils.py
  6. 0 1
      tests/test_utils.py

+ 0 - 3
myanimebot.py

@@ -150,7 +150,6 @@ async def background_check_feed(asyncioloop):
 
 
 
 
 async def fetch_activities_anilist():
 async def fetch_activities_anilist():
-	print("Fetching activities")
 	await anilist.check_new_activities()
 	await anilist.check_new_activities()
 
 
 
 
@@ -296,7 +295,6 @@ async def add_user_cmd(words, message):
 		user_servers = utils.get_user_servers(user, service)
 		user_servers = utils.get_user_servers(user, service)
 		# User not present in database
 		# User not present in database
 		if user_servers is None: 
 		if user_servers is None: 
-			print('Inserting USER WITH {} {} {}'.format(user, service, server_id))
 			utils.insert_user_into_db(user, service, server_id)
 			utils.insert_user_into_db(user, service, server_id)
 			return await message.channel.send("**{}** added to the database for the server **{}**.".format(user, str(message.guild)))
 			return await message.channel.send("**{}** added to the database for the server **{}**.".format(user, str(message.guild)))
 		else: # User present in database
 		else: # User present in database
@@ -306,7 +304,6 @@ async def add_user_cmd(words, message):
 				return await message.channel.send("User **{}** is already registered in our database for this server!".format(user))
 				return await message.channel.send("User **{}** is already registered in our database for this server!".format(user))
 			else:
 			else:
 				new_servers = '{},{}'.format(user_servers, server_id)
 				new_servers = '{},{}'.format(user_servers, server_id)
-				print('UPDATING USER WITH {} {} {}'.format(user, service, new_servers))
 				utils.update_user_servers_db(user, service, new_servers)					
 				utils.update_user_servers_db(user, service, new_servers)					
 				return await message.channel.send("**{}** added to the database for the server **{}**.".format(user, str(message.guild)))
 				return await message.channel.send("**{}** added to the database for the server **{}**.".format(user, str(message.guild)))
 	except Exception as e:
 	except Exception as e:

+ 23 - 31
myanimebot/anilist.py

@@ -10,6 +10,7 @@ import myanimebot.globals as globals
 import myanimebot.utils as utils
 import myanimebot.utils as utils
 from myanimebot.discord import send_embed_wrapper, build_embed
 from myanimebot.discord import send_embed_wrapper, build_embed
 
 
+
 ANILIST_GRAPHQL_URL = 'https://graphql.anilist.co'
 ANILIST_GRAPHQL_URL = 'https://graphql.anilist.co'
     
     
 
 
@@ -100,12 +101,9 @@ def get_anilist_userId_from_name(user_name : str) -> int:
         response.raise_for_status()
         response.raise_for_status()
         return response.json()["data"]["User"]["id"]
         return response.json()["data"]["User"]["id"]
     except requests.HTTPError as e:
     except requests.HTTPError as e:
-        #TODO Correct error response
-        print('ERROR WRONG RESPONSE CODE')
+        globals.logging.error('HTPP Error while getting the AniList user ID for "{}". Error: {}'.format(user_name, e))
     except Exception as e:
     except Exception as e:
-        #TODO Correct error response
-        print('UNKNOWN Error when trying to get user id :')
-        print(e)
+        globals.logging.error('Unknown error while getting the AniList user ID for "{}". Error: {}'.format(user_name, e))
     return None
     return None
 
 
 
 
@@ -178,12 +176,9 @@ def get_latest_users_activities(users : List[utils.User], page: int, perPage = 5
         return feeds
         return feeds
 
 
     except requests.HTTPError as e:
     except requests.HTTPError as e:
-        #TODO Correct error response
-        print('ERROR WRONG RESPONSE CODE')
+        globals.logging.error('HTPP Error while getting the latest users\' AniList activities for {} on page {} with {} items per page. Error: {}'.format(users, page, perPage, e))
     except Exception as e:
     except Exception as e:
-        #TODO Correct error response
-        print('UNKNOWN Error when trying to get the users\' activities :')
-        print(e)
+        globals.logging.error('Unknown Error while getting the latest users\' AniList activities for {} on page {} with {} items per page. Error: {}'.format(users, page, perPage, e))
     return []
     return []
 
 
 
 
@@ -205,11 +200,12 @@ def check_username_validity(username) -> bool:
         response.raise_for_status()
         response.raise_for_status()
         return response.json()["data"]["User"]["name"] == username
         return response.json()["data"]["User"]["name"] == username
     except requests.HTTPError as e:
     except requests.HTTPError as e:
-        return False
+        status_code = e.response.status_code
+        if status_code != 404:
+            globals.logging.error('HTTP Error while trying to check this username validity: "{}". Error: {}'.format(username, e))
     except Exception as e:
     except Exception as e:
-        #TODO Correct error response
-        print('UNKNOWN Error when trying to get mal id : {}'.format(e))
-        return False
+        globals.logging.error('Unknown error while trying to check this username validity: "{}". Error: {}'.format(username, e))
+    return False
 
 
 
 
 def get_latest_activity(users : List[utils.User]):
 def get_latest_activity(users : List[utils.User]):
@@ -236,12 +232,9 @@ def get_latest_activity(users : List[utils.User]):
         response.raise_for_status()
         response.raise_for_status()
         return response.json()["data"]["Activity"]
         return response.json()["data"]["Activity"]
     except requests.HTTPError as e:
     except requests.HTTPError as e:
-        #TODO Correct error response
-        print('ERROR WRONG RESPONSE CODE')
+        globals.logging.error('HTPP Error while getting the latest AniList activity : {}'.format(e))
     except Exception as e:
     except Exception as e:
-        #TODO Correct error response
-        print('UNKNOWN Error when trying to get the latest activity :')
-        print(e)
+        globals.logging.error('Unknown error while getting the latest AniList activity : {}'.format(e))
     return None
     return None
 
 
 
 
@@ -275,7 +268,6 @@ def get_users_id(users_data) -> List[int]:
 
 
     # Get users using AniList
     # Get users using AniList
     if users_data is not None:
     if users_data is not None:
-        print("Users found: {}".format(users_data))
         for user_data in users_data:
         for user_data in users_data:
             users_ids.append(get_anilist_userId_from_name(user_data[globals.DB_USER_NAME]))
             users_ids.append(get_anilist_userId_from_name(user_data[globals.DB_USER_NAME]))
         # TODO Normalement pas besoin de recuperer les ids vu que je peux faire la recherche avec les noms
         # TODO Normalement pas besoin de recuperer les ids vu que je peux faire la recherche avec les noms
@@ -284,7 +276,8 @@ def get_users_id(users_data) -> List[int]:
 
 
 
 
 async def send_embed_to_channels(activity : utils.Feed):
 async def send_embed_to_channels(activity : utils.Feed):
-    # TODO Doc
+    ''' Send an embed message describing the activity to user's channel '''
+
     for server in activity.user.servers:
     for server in activity.user.servers:
         data_channels = utils.get_channels(server)
         data_channels = utils.get_channels(server)
     
     
@@ -297,6 +290,8 @@ async def send_embed_to_channels(activity : utils.Feed):
 
 
 
 
 def insert_feed_db(activity: utils.Feed):
 def insert_feed_db(activity: utils.Feed):
+    ''' Insert an AniList feed into database '''
+
     cursor = globals.conn.cursor(buffered=True)
     cursor = globals.conn.cursor(buffered=True)
     cursor.execute("INSERT INTO t_feeds (published, title, url, user, found, type, service) VALUES (FROM_UNIXTIME(%s), %s, %s, %s, NOW(), %s, %s)",
     cursor.execute("INSERT INTO t_feeds (published, title, url, user, found, type, service) VALUES (FROM_UNIXTIME(%s), %s, %s, %s, NOW(), %s, %s)",
                     (activity.date_publication.timestamp(),
                     (activity.date_publication.timestamp(),
@@ -319,11 +314,8 @@ async def process_new_activities(last_activity_date, users : List[utils.User]):
 
 
         # Processing them
         # Processing them
         for activity in activities:
         for activity in activities:
-            print(activity) # TODO Remove, DEBUG
-
             # Get time difference between now and activity creation date
             # Get time difference between now and activity creation date
             diffTime = datetime.datetime.now(globals.timezone) - activity.date_publication
             diffTime = datetime.datetime.now(globals.timezone) - activity.date_publication
-            print("Time difference between feed and now = {}".format(diffTime))
 
 
             # If the activity is older than the last_activity_date, we processed all the newest activities
             # If the activity is older than the last_activity_date, we processed all the newest activities
             # Also, if the time difference is bigger than the config's "secondMax", we can stop processing them
             # Also, if the time difference is bigger than the config's "secondMax", we can stop processing them
@@ -332,16 +324,16 @@ async def process_new_activities(last_activity_date, users : List[utils.User]):
                 # FIXME If two or more feeds are published at the same time, this would skip them
                 # FIXME If two or more feeds are published at the same time, this would skip them
                 continue_fetching = False
                 continue_fetching = False
                 break
                 break
+
             # Process activity
             # Process activity
-            # TODO Add logger infos
+            globals.logger.info('Adding new feed for "{}({})" about "{}"'.format(activity.user.name, activity.service.name, activity.media.name))
             insert_feed_db(activity)
             insert_feed_db(activity)
-            # TODO Create embed and send to channels
+
             await send_embed_to_channels(activity)
             await send_embed_to_channels(activity)
 
 
         # Load next activities page
         # Load next activities page
         # TODO How can I avoid duplicate if insertion in between? With storing ids?
         # TODO How can I avoid duplicate if insertion in between? With storing ids?
         if continue_fetching:
         if continue_fetching:
-            print('Fetching next page') # TODO Remove, Debug
             page_number += 1
             page_number += 1
             time.sleep(1)
             time.sleep(1)
 
 
@@ -351,14 +343,14 @@ def get_last_activity_date_db() -> float:
     globals.conn.commit()
     globals.conn.commit()
 
 
     # Get last activity date
     # Get last activity date
-    cursor = globals.conn.cursor(buffered=True)
+    cursor = globals.conn.cursor(buffered=True, dictionary=True)
     cursor.execute("SELECT published FROM t_feeds WHERE service=%s ORDER BY published DESC LIMIT 1", [globals.SERVICE_ANILIST])
     cursor.execute("SELECT published FROM t_feeds WHERE service=%s ORDER BY published DESC LIMIT 1", [globals.SERVICE_ANILIST])
     data = cursor.fetchone()
     data = cursor.fetchone()
 
 
     if data is None or len(data) == 0:
     if data is None or len(data) == 0:
         return 0.0
         return 0.0
     else:
     else:
-        return data[0].timestamp()
+        return data["published"].timestamp()
 
 
 
 
 async def check_new_activities():
 async def check_new_activities():
@@ -372,7 +364,7 @@ async def check_new_activities():
     if latest_activity is not None:
     if latest_activity is not None:
 
 
         # If the latest activity is more recent than the last we stored
         # If the latest activity is more recent than the last we stored
-        print('Last registered = {} | {} = latest feed'.format(last_activity_date, latest_activity["createdAt"]))
+        globals.logger.debug('Comparing last registered feed ({}) with latest found feed ({})'.format(last_activity_date, latest_activity["createdAt"]))
         if last_activity_date < latest_activity["createdAt"]:
         if last_activity_date < latest_activity["createdAt"]:
             globals.logger.debug("Found a more recent AniList feed")
             globals.logger.debug("Found a more recent AniList feed")
             await process_new_activities(last_activity_date, users)
             await process_new_activities(last_activity_date, users)
@@ -382,7 +374,7 @@ async def background_check_feed(asyncioloop):
     ''' Main function that check the AniList feeds '''
     ''' Main function that check the AniList feeds '''
 
 
     globals.logger.info("Starting up Anilist.background_check_feed")
     globals.logger.info("Starting up Anilist.background_check_feed")
-    await globals.client.wait_until_ready()	
+    await globals.client.wait_until_ready()
     globals.logger.debug("Discord client connected, unlocking Anilist.background_check_feed...")
     globals.logger.debug("Discord client connected, unlocking Anilist.background_check_feed...")
 
 
     while not globals.client.is_closed():
     while not globals.client.is_closed():

+ 5 - 3
myanimebot/discord.py

@@ -33,13 +33,15 @@ def build_embed(feed : utils.Feed):
 		globals.logger.error("Error when generating the message: " + str(e))
 		globals.logger.error("Error when generating the message: " + str(e))
 		return
 		return
 
 
-# Function used to send the embed
+
 async def send_embed_wrapper(asyncioloop, channelid, client, embed):
 async def send_embed_wrapper(asyncioloop, channelid, client, embed):
+	''' Send an embed message to a channel '''
+
 	channel = client.get_channel(int(channelid))
 	channel = client.get_channel(int(channelid))
 	
 	
 	try:
 	try:
 		await channel.send(embed=embed)
 		await channel.send(embed=embed)
-		globals.logger.info("Message sent in channel: " + channelid)
+		globals.logger.info("Message sent in channel: {}".format(channelid))
 	except Exception as e:
 	except Exception as e:
-		globals.logger.debug("Impossible to send a message on '" + channelid + "': " + str(e)) 
+		globals.logger.debug("Impossible to send a message on '{}': {}".format(channelid, e)) 
 		return
 		return

+ 0 - 1
myanimebot/myanimelist.py

@@ -17,7 +17,6 @@ def get_thumbnail(urlParam):
     image = re.search(r'(?P<url>https?://[^\s]+)', str(soup.find("img", {"itemprop": "image"}))).group("url")
     image = re.search(r'(?P<url>https?://[^\s]+)', str(soup.find("img", {"itemprop": "image"}))).group("url")
     thumbnail = "".join(image.split('"')[:1]).replace('"','')
     thumbnail = "".join(image.split('"')[:1]).replace('"','')
 	
 	
-    print(thumbnail)
     return thumbnail
     return thumbnail
 
 
 
 

+ 4 - 5
myanimebot/utils.py

@@ -154,7 +154,9 @@ def filter_name(name : str) -> str:
 
 
 # Check if the show's name ends with a show type and truncate it
 # Check if the show's name ends with a show type and truncate it
 def truncate_end_show(media_name : str):
 def truncate_end_show(media_name : str):
-    if media_name is None: return media_name
+    ''' Check if a show's name ends with a show type and truncate it '''
+
+    if media_name is None: return None
 
 
     show_types = (
     show_types = (
         '- TV',
         '- TV',
@@ -186,8 +188,7 @@ def truncate_end_show(media_name : str):
 def get_channels(server_id: int) -> dict:
 def get_channels(server_id: int) -> dict:
     ''' Returns the registered channels for a server '''
     ''' Returns the registered channels for a server '''
 
 
-    if server_id is None:
-        return None
+    if server_id is None: return None
 
 
     # TODO Make generic execute
     # TODO Make generic execute
     cursor = globals.conn.cursor(buffered=True, dictionary=True)
     cursor = globals.conn.cursor(buffered=True, dictionary=True)
@@ -292,5 +293,3 @@ def insert_user_into_db(user_name : str, service : Service, servers : str) -> bo
     globals.conn.commit()
     globals.conn.commit()
     cursor.close()
     cursor.close()
     return True
     return True
-
-# TODO Create a Feed class instead of sending a lot of parameters

+ 0 - 1
tests/test_utils.py

@@ -139,7 +139,6 @@ def test_truncate_end_show():
 
 
 
 
 def test_media_status():
 def test_media_status():
-    print('testing')
     # Testing Current
     # Testing Current
     try:
     try:
         current = MediaStatus.CURRENT
         current = MediaStatus.CURRENT