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

Adding AniList feed fetching task

Lucas Villeneuve пре 5 година
родитељ
комит
44132c4665
4 измењених фајлова са 35 додато и 19 уклоњено
  1. 3 0
      myanimebot.example.conf
  2. 19 11
      src/anilist.py
  3. 6 4
      src/globals.py
  4. 7 4
      src/myanimebot.py

+ 3 - 0
myanimebot.example.conf

@@ -19,6 +19,9 @@ timezone = Europe/Paris
 # New feed since this number of minutes will be displayed (useful when the bot crash on a long period)
 secondMax = 7200
 
+# How much time should the bot need to wait between fetches
+anilist_seconds_between_fetches = 60
+
 # Discord Token
 token = 123456789ABCDEF987654321FEDCBA
 

+ 19 - 11
src/anilist.py

@@ -388,7 +388,6 @@ def get_last_activity_date_db() -> float:
     cursor.execute("SELECT published FROM t_feeds WHERE service=%s ORDER BY published DESC LIMIT 1", [globals.SERVICE_ANILIST])
     data = cursor.fetchone()
 
-    print("Getting last activity date : {}".format(data))
     if data is None or len(data) == 0:
         return 0.0
     else:
@@ -399,7 +398,6 @@ async def check_new_activities():
     """ Check if there is new activities and process them """
     
     last_activity_date = get_last_activity_date_db()
-    print(last_activity_date)
 
     # Get latest activity on AniList
     users = get_users()
@@ -407,20 +405,30 @@ async def check_new_activities():
     if latest_activity is not None:
 
         # If the latest activity is more recent than the last we stored
+        print('Last registered = {} | {} = latest feed'.format(last_activity_date, latest_activity["createdAt"]))
         if last_activity_date < latest_activity["createdAt"]:
-            print("Latest activity is more recent")
+            globals.logger.debug("Found a more recent AniList feed")
             await process_new_activities(last_activity_date, users)
 
 
-# [x] Convertir AniList ID en MAL ID
-# [ ] Recuperer utilisateurs qui nous interessent
-# [X] Recuperer activites de ces users
-# [X] Traiter les donnees et les mettre en DB
-# [X] Creer embed et envoyer messages
-# [ ] Faire task pour fetch automatiquement
-# [ ] Rajouter requests dans la liste de dependances pip (Site de Penta)
+async def background_check_feed(asyncioloop):
+    ''' Main function that check the AniList feeds '''
+
+    globals.logger.info("Starting up Anilist.background_check_feed")
+    await globals.client.wait_until_ready()	
+    globals.logger.debug("Discord client connected, unlocking Anilist.background_check_feed...")
+
+    while not globals.client.is_closed():
+        globals.logger.debug('Fetching Anilist feeds')
+        try:
+            await check_new_activities()
+        except Exception as e:
+            globals.logger.error('Error while fetching Anilist feeds : ({})'.format(e))
+
+        await asyncio.sleep(globals.ANILIST_SECONDS_BETWEEN_FETCHES)
+
 
 # TODO Bien renvoyer vers AniList (Liens/Liste/Anime)
 # TODO Comment eviter doublons MAL/AniList -> Ne pas faire je pense
 # TODO Insert anime into DB
-# TODO Uniformiser labels status feed entre MAL et ANILIST
+# TODO Uniformiser labels status feed entre MAL et ANILIST

+ 6 - 4
src/globals.py

@@ -53,9 +53,10 @@ timezone=pytz.timezone(CONFIG.get("timezone", "utc"))
 secondMax=CONFIG.getint("secondMax", 7200)
 token=CONFIG.get("token")
 prefix=CONFIG.get("prefix", "!malbot")
+iconBot=CONFIG.get("iconBot", "http://myanimebot.pentou.eu/rsc/bot_avatar.jpg")
+ANILIST_SECONDS_BETWEEN_FETCHES=CONFIG.getint("anilist_seconds_between_fetches", 60)
 MAL_ICON_URL=CONFIG.get("iconMAL", "https://cdn.myanimelist.net/img/sp/icon/apple-touch-icon-256.png")
 ANILIST_ICON_URL=CONFIG.get("iconAniList", "https://anilist.co/img/icons/android-chrome-512x512.png")
-iconBot=CONFIG.get("iconBot", "http://myanimebot.pentou.eu/rsc/bot_avatar.jpg")
 SERVICE_ANILIST="ani"
 SERVICE_MAL="mal"
 MAL_URL="https://myanimelist.net/"
@@ -149,6 +150,7 @@ except Exception as e:
 # Initialization of the Discord client
 client = discord.Client()
 
-task_feed       = None
-task_gameplayed = None
-task_thumbnail  = None
+task_feed       	= None
+task_feed_anilist	= None
+task_gameplayed 	= None
+task_thumbnail  	= None

+ 7 - 4
src/myanimebot.py

@@ -198,9 +198,10 @@ async def on_ready():
 
 	globals.logger.info("Starting all tasks...")
 
-	task_feed = globals.client.loop.create_task(background_check_feed(globals.client.loop))
-	task_thumbnail = globals.client.loop.create_task(update_thumbnail_catalog(globals.client.loop))
-	task_gameplayed = globals.client.loop.create_task(change_gameplayed(globals.client.loop))
+	globals.task_feed = globals.client.loop.create_task(background_check_feed(globals.client.loop))
+	globals.task_feed_anilist = globals.client.loop.create_task(anilist.background_check_feed(globals.client.loop))
+	globals.task_thumbnail = globals.client.loop.create_task(update_thumbnail_catalog(globals.client.loop))
+	globals.task_gameplayed = globals.client.loop.create_task(change_gameplayed(globals.client.loop))
 
 
 @globals.client.event
@@ -395,7 +396,8 @@ async def on_message(message):
 	# A user is trying to get help
 	if words[0] == globals.prefix:
 		if len(words) > 1:
-			if words[1] == "ping": await message.channel.send("pong")
+			if words[1] == "ping":
+				await message.channel.send("pong")
 			
 			elif words[1] == "here":
 				if message.author.guild_permissions.administrator:
@@ -597,6 +599,7 @@ if __name__ == "__main__":
     except:
         logging.info("Closing all tasks...")
         globals.task_feed.cancel()
+        globals.task_feed_anilist.cancel()
         globals.task_thumbnail.cancel()
         globals.task_gameplayed.cancel()