Преглед на файлове

Moving discord funcs to appropriate file

Lucas Villeneuve преди 5 години
родител
ревизия
3b25153048
променени са 5 файла, в които са добавени 59 реда и са изтрити 54 реда
  1. 3 2
      myanimebot.py
  2. 1 0
      myanimebot/__init__.py
  3. 11 10
      myanimebot/anilist.py
  4. 44 0
      myanimebot/discord.py
  5. 0 42
      myanimebot/utils.py

+ 3 - 2
myanimebot.py

@@ -8,6 +8,7 @@
 # yum install gcc MariaDB-client MariaDB-common MariaDB-shared MariaDB-devel
 # python3.7 -m pip install --upgrade pip
 # pip3.7 install discord.py mariadb pytz feedparser python-dateutil asyncio html2text bs4 PyNaCL aiodns cchardet configparser
+# TODO Remove all of that
 
 import asyncio
 # Library import
@@ -21,7 +22,6 @@ from typing import List, Tuple
 import aiohttp
 import discord
 import feedparser
-import pytz
 from aiohttp.web_exceptions import HTTPError, HTTPNotModified
 from dateutil.parser import parse as parse_datetime
 from html2text import HTML2Text
@@ -31,6 +31,7 @@ import myanimebot.anilist as anilist
 import myanimebot.globals as globals
 import myanimebot.utils as utils
 import myanimebot.myanimelist as myanimelist
+from myanimebot.discord import send_embed_wrapper, build_embed
 
 if not sys.version_info[:2] >= (3, 7):
 	print("ERROR: Requires python 3.7 or newer.")
@@ -129,7 +130,7 @@ async def background_check_feed(asyncioloop):
 									data_channel = db_srv.fetchone()
 									
 									while data_channel is not None:
-										for channel in data_channel: await utils.send_embed_wrapper(asyncioloop, channel, globals.client, utils.build_embed(user, item.title, item.link, item.description, pubDateRaw, image, utils.Service.MAL))
+										for channel in data_channel: await send_embed_wrapper(asyncioloop, channel, globals.client, build_embed(user, item.title, item.link, item.description, pubDateRaw, image, utils.Service.MAL))
 										
 										data_channel = db_srv.fetchone()
 					if feed_type == 1:

+ 1 - 0
myanimebot/__init__.py

@@ -2,3 +2,4 @@ from .utils import *
 from .anilist import *
 from .globals import *
 from .myanimelist import *
+from .discord import *

+ 11 - 10
myanimebot/anilist.py

@@ -8,6 +8,7 @@ import requests
 
 import myanimebot.globals as globals
 import myanimebot.utils as utils
+from myanimebot.discord import send_embed_wrapper, build_embed
 
 ANILIST_GRAPHQL_URL = 'https://graphql.anilist.co'
 
@@ -316,16 +317,16 @@ async def send_embed_to_channels(activity : utils.Feed):
     
         if data_channels is not None:
             for channel in data_channels:
-                await utils.send_embed_wrapper(None,
-                                                    channel["channel"],
-                                                    globals.client,
-                                                    utils.build_embed(activity.user.name,
-                                                                            activity.media.name,
-                                                                            activity.media.url,
-                                                                            activity.status,
-                                                                            activity.date_publication,
-                                                                            activity.media.image,
-                                                                            activity.service))
+                await send_embed_wrapper(None,
+                                            channel["channel"],
+                                            globals.client,
+                                            build_embed(activity.user.name,
+                                                activity.media.name,
+                                                activity.media.url,
+                                                activity.status,
+                                                activity.date_publication,
+                                                activity.media.image,
+                                                activity.service))
 
 
 def insert_feed_db(activity: utils.Feed):

+ 44 - 0
myanimebot/discord.py

@@ -0,0 +1,44 @@
+import discord
+import pytz
+
+import myanimebot.globals as globals # TODO Rename globals module
+import myanimebot.utils as utils
+
+def build_embed(user, item_title, item_link, item_description, pub_date, image, service: utils.Service):
+	''' Build the embed message related to the anime's status '''
+
+	# Get service
+	if service == utils.Service.MAL:
+		service_name = 'MyAnimeList'
+		profile_url = "{}{}".format(globals.MAL_PROFILE_URL, user)
+		icon_url = globals.MAL_ICON_URL
+	elif service == utils.Service.ANILIST:
+		service_name = 'AniList'
+		profile_url = "{}{}".format(globals.ANILIST_PROFILE_URL, user)
+		icon_url = globals.ANILIST_ICON_URL
+	else:
+		raise NotImplementedError('Unknown service {}'.format(service))
+	description = "[{}]({})\n```{}```".format(utils.filter_name(item_title), item_link, item_description)
+	profile_url_label = "{}'s {}".format(user, service_name)
+
+	try:	
+		embed = discord.Embed(colour=0xEED000, url=item_link, description=description, timestamp=pub_date.astimezone(pytz.timezone("utc")))
+		embed.set_thumbnail(url=image)
+		embed.set_author(name=profile_url_label, url=profile_url, icon_url=icon_url)
+		embed.set_footer(text="MyAnimeBot", icon_url=globals.iconBot)
+		
+		return embed
+	except Exception as e:
+		globals.logger.error("Error when generating the message: " + str(e))
+		return
+
+# Function used to send the embed
+async def send_embed_wrapper(asyncioloop, channelid, client, embed):
+	channel = client.get_channel(int(channelid))
+	
+	try:
+		await channel.send(embed=embed)
+		globals.logger.info("Message sent in channel: " + channelid)
+	except Exception as e:
+		globals.logger.debug("Impossible to send a message on '" + channelid + "': " + str(e)) 
+		return

+ 0 - 42
myanimebot/utils.py

@@ -1,6 +1,4 @@
 import datetime
-import discord
-import pytz
 from enum import Enum
 from typing import List
 
@@ -253,44 +251,4 @@ def insert_user_into_db(user_name : str, service : Service, servers : str) -> bo
 	cursor.close()
 	return True
 
-# TODO Move those functions somewhere else (E.g. discord.py)
-
 # TODO Create a Feed class instead of sending a lot of parameters
-def build_embed(user, item_title, item_link, item_description, pub_date, image, service: Service):
-	''' Build the embed message related to the anime's status '''
-
-	# Get service
-	if service == Service.MAL:
-		service_name = 'MyAnimeList'
-		profile_url = "{}{}".format(globals.MAL_PROFILE_URL, user)
-		icon_url = globals.MAL_ICON_URL
-	elif service == Service.ANILIST:
-		service_name = 'AniList'
-		profile_url = "{}{}".format(globals.ANILIST_PROFILE_URL, user)
-		icon_url = globals.ANILIST_ICON_URL
-	else:
-		raise NotImplementedError('Unknown service {}'.format(service))
-	description = "[{}]({})\n```{}```".format(filter_name(item_title), item_link, item_description)
-	profile_url_label = "{}'s {}".format(user, service_name)
-
-	try:	
-		embed = discord.Embed(colour=0xEED000, url=item_link, description=description, timestamp=pub_date.astimezone(pytz.timezone("utc")))
-		embed.set_thumbnail(url=image)
-		embed.set_author(name=profile_url_label, url=profile_url, icon_url=icon_url)
-		embed.set_footer(text="MyAnimeBot", icon_url=globals.iconBot)
-		
-		return embed
-	except Exception as e:
-		globals.logger.error("Error when generating the message: " + str(e))
-		return
-
-# Function used to send the embed
-async def send_embed_wrapper(asyncioloop, channelid, client, embed):
-	channel = client.get_channel(int(channelid))
-	
-	try:
-		await channel.send(embed=embed)
-		globals.logger.info("Message sent in channel: " + channelid)
-	except Exception as e:
-		globals.logger.debug("Impossible to send a message on '" + channelid + "': " + str(e)) 
-		return