utils.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. import datetime
  2. from enum import Enum
  3. from typing import List
  4. import myanimebot.globals as globals
  5. # TODO Redo all of the desc/status system
  6. # Media Status colors
  7. CURRENT_COLOR = "0x00CC00"
  8. PLANNING_COLOR = "0xBFBFBF"
  9. COMPLETED_COLOR = "0x000088"
  10. DROPPED_COLOR = "0xCC0000"
  11. PAUSED_COLOR = "0xDDDD00"
  12. REPEATING_COLOR = "0x007700"
  13. class Service(Enum):
  14. MAL=globals.SERVICE_MAL
  15. ANILIST=globals.SERVICE_ANILIST
  16. @staticmethod
  17. def from_str(label: str):
  18. if label is None: raise TypeError
  19. if label.upper() in ('MAL', 'MYANIMELIST', globals.SERVICE_MAL.upper()):
  20. return Service.MAL
  21. elif label.upper() in ('AL', 'ANILIST', globals.SERVICE_ANILIST.upper()):
  22. return Service.ANILIST
  23. else:
  24. raise NotImplementedError('Error: Cannot convert "{}" to a Service'.format(label))
  25. class MediaType(Enum):
  26. ANIME="ANIME"
  27. MANGA="MANGA"
  28. @staticmethod
  29. def from_str(label: str):
  30. if label is None: raise TypeError
  31. if label.upper() in ('ANIME', 'ANIME_LIST'):
  32. return MediaType.ANIME
  33. elif label.upper() in ('MANGA', 'MANGA_LIST'):
  34. return MediaType.MANGA
  35. else:
  36. raise NotImplementedError('Error: Cannot convert "{}" to a MediaType'.format(label))
  37. def get_media_count_type(self):
  38. if self == MediaType.ANIME:
  39. return 'episodes'
  40. elif self == MediaType.MANGA:
  41. return 'chapters'
  42. else:
  43. raise NotImplementedError('Unknown MediaType "{}"'.format(self))
  44. class MediaStatus(Enum):
  45. CURRENT = CURRENT_COLOR
  46. PLANNING = PLANNING_COLOR
  47. COMPLETED = COMPLETED_COLOR
  48. DROPPED = DROPPED_COLOR
  49. PAUSED = PAUSED_COLOR
  50. REPEATING = REPEATING_COLOR
  51. @staticmethod
  52. def from_str(label: str):
  53. if label is None: raise TypeError
  54. first_word = label.split(' ')[0].upper()
  55. if first_word in ['READ', 'READING', 'WATCHED', 'WATCHING']:
  56. return MediaStatus.CURRENT
  57. elif first_word in ['PLANS', 'PLAN']:
  58. return MediaStatus.PLANNING
  59. elif first_word in ['COMPLETED']:
  60. return MediaStatus.COMPLETED
  61. elif first_word in ['DROPPED']:
  62. return MediaStatus.DROPPED
  63. elif first_word in ['PAUSED', 'ON-HOLD']:
  64. return MediaStatus.PAUSED
  65. elif first_word in ['REREAD', 'REREADING', 'REWATCHED', 'REWATCHING', 'RE-READING', 'RE-WATCHING', 'RE-READ', 'RE-WATCHED']:
  66. return MediaStatus.REPEATING
  67. else:
  68. raise NotImplementedError('Error: Cannot convert "{}" to a MediaStatus'.format(label))
  69. class User():
  70. data = None
  71. def __init__(self,
  72. id : int,
  73. service_id : int,
  74. name : str,
  75. servers : List[int]):
  76. self.id = id
  77. self.service_id = service_id
  78. self.name = name
  79. self.servers = servers
  80. class Media():
  81. def __init__(self,
  82. id : int,
  83. name : str,
  84. url : str,
  85. episodes : str,
  86. image : str,
  87. type : MediaType):
  88. self.id = id
  89. self.name = name
  90. self.url = url
  91. self.episodes = episodes
  92. self.image = image
  93. self.type = type
  94. class Feed():
  95. def __init__(self,
  96. service : Service,
  97. date_publication : datetime.datetime,
  98. user : User,
  99. status : MediaStatus,
  100. description : str, # TODO Need to change
  101. progress : str,
  102. media : Media,
  103. score : float,
  104. score_format : str
  105. ):
  106. self.service = service
  107. self.date_publication = date_publication
  108. self.user = user
  109. self.status = status
  110. self.media = media
  111. self.description = description
  112. self.progress = progress
  113. self.score = score
  114. self.score_format = score_format
  115. def get_status_str(self):
  116. if self.status == MediaStatus.CURRENT \
  117. or self.status == MediaStatus.REPEATING:
  118. if self.media.type == MediaType.ANIME:
  119. status_str = 'Watching'
  120. elif self.media.type == MediaType.MANGA:
  121. status_str = 'Reading'
  122. else:
  123. raise NotImplementedError('Unknown MediaType: {}'.format(self.media.type))
  124. # Add prefix if rewatching
  125. if self.status == MediaStatus.REPEATING:
  126. status_str = 'Re-{}'.format(status_str.lower())
  127. elif self.status == MediaStatus.COMPLETED:
  128. status_str = 'Completed'
  129. elif self.status == MediaStatus.PAUSED:
  130. status_str = 'Paused'
  131. elif self.status == MediaStatus.DROPPED:
  132. status_str = 'Dropped'
  133. elif self.status == MediaStatus.PLANNING:
  134. if self.media.type == MediaType.ANIME:
  135. media_type_label = 'watch'
  136. elif self.media.type == MediaType.MANGA:
  137. media_type_label = 'read'
  138. else:
  139. raise NotImplementedError('Unknown MediaType: {}'.format(self.media.type))
  140. status_str = 'Plans to {}'.format(media_type_label)
  141. else:
  142. raise NotImplementedError('Unknown MediaStatus: {}'.format(self.status))
  143. return status_str
  144. def replace_all(text : str, replace_dic : dict) -> str:
  145. '''Replace multiple substrings from a string'''
  146. if text is None or replace_dic is None:
  147. return text
  148. for replace_key, replace_value in replace_dic.items():
  149. text = text.replace(replace_key, replace_value)
  150. return text
  151. # Check if the show's name ends with a show type and truncate it
  152. def truncate_end_show(media_name : str):
  153. '''Check if a show's name ends with a show type and truncate it'''
  154. if media_name is None: return None
  155. show_types = (
  156. '- TV',
  157. '- Movie',
  158. '- Special',
  159. '- OVA',
  160. '- ONA',
  161. '- Manga',
  162. '- Manhua',
  163. '- Manhwa',
  164. '- Novel',
  165. '- One-Shot',
  166. '- Doujinshi',
  167. '- Music',
  168. '- OEL',
  169. '- Unknown',
  170. '- Light Novel'
  171. )
  172. for show_type in show_types:
  173. if media_name.endswith(show_type):
  174. new_show = media_name[:-len(show_type)]
  175. # Check if space at the end
  176. if new_show.endswith(' '):
  177. new_show = new_show[:-1]
  178. return new_show
  179. return media_name
  180. def build_score_string(score_format : str):
  181. if score_format == "POINT_100":
  182. return "100"
  183. elif score_format == "POINT_10" or score_format == "POINT_10_DECIMAL":
  184. return "10"
  185. elif score_format == "POINT_5":
  186. return "5"
  187. elif score_format == "POINT_3":
  188. return "3"
  189. else:
  190. return "?"
  191. def build_description_string(feed : Feed):
  192. '''Build and returns a string describing the feed'''
  193. media_type_count = feed.media.type.get_media_count_type()
  194. status_str = feed.get_status_str()
  195. # Build the string
  196. desc = '{} | {} of {} {}'.format(status_str, feed.progress, feed.media.episodes, media_type_count)
  197. globals.logger.debug("Feed media score {}".format(feed.score))
  198. if feed.score is not None:
  199. desc += '\nScore: {} / {}'.format(feed.score, build_score_string(feed.score_format))
  200. return desc
  201. def get_channels(server_id: int) -> dict:
  202. '''Returns the registered channels for a server'''
  203. if server_id is None: return None
  204. # TODO Make generic execute
  205. cursor = globals.conn.cursor(buffered=True, dictionary=True)
  206. cursor.execute("SELECT channel FROM t_servers WHERE server = %s", [server_id])
  207. channels = cursor.fetchall()
  208. cursor.close()
  209. return channels
  210. def is_server_in_db(server_id : str) -> bool:
  211. '''Checks if server is registered in the database'''
  212. if server_id is None:
  213. return False
  214. cursor = globals.conn.cursor(buffered=True)
  215. cursor.execute("SELECT server FROM t_servers WHERE server=%s", [server_id])
  216. data = cursor.fetchone()
  217. cursor.close()
  218. return data is not None
  219. def get_users() -> List[dict]:
  220. '''Returns all registered users'''
  221. cursor = globals.conn.cursor(buffered=True, dictionary=True)
  222. cursor.execute('SELECT {}, service, servers FROM t_users'.format(globals.DB_USER_NAME))
  223. users = cursor.fetchall()
  224. cursor.close()
  225. return users
  226. def get_user_servers(user_name : str, service : Service) -> str:
  227. '''Returns a list of every registered servers for a user of a specific service, as a string'''
  228. if user_name is None or service is None:
  229. return
  230. cursor = globals.conn.cursor(buffered=True, dictionary=True)
  231. cursor.execute("SELECT servers FROM t_users WHERE LOWER({})=%s AND service=%s".format(globals.DB_USER_NAME),
  232. [user_name.lower(), service.value])
  233. user_servers = cursor.fetchone()
  234. cursor.close()
  235. if user_servers is not None:
  236. return user_servers["servers"]
  237. return None
  238. def remove_server_from_servers(server : str, servers : str) -> str:
  239. '''Removes the server from a comma-separated string containing multiple servers'''
  240. servers_list = servers.split(',')
  241. # If the server is not found, return None
  242. if server not in servers_list:
  243. return None
  244. # Remove every occurence of server
  245. servers_list = [x for x in servers_list if x != server]
  246. # Build server-free string
  247. return ','.join(servers_list)
  248. def delete_user_from_db(user_name : str, service : Service) -> bool:
  249. '''Removes the user from the database'''
  250. if user_name is None or service is None:
  251. globals.logger.warning("Error while trying to delete user '{}' with service '{}'".format(user_name, service))
  252. return False
  253. cursor = globals.conn.cursor(buffered=True)
  254. cursor.execute("DELETE FROM t_users WHERE LOWER({}) = %s AND service=%s".format(globals.DB_USER_NAME),
  255. [user_name.lower(), service.value])
  256. globals.conn.commit()
  257. cursor.close()
  258. return True
  259. def update_user_servers_db(user_name : str, service : Service, servers : str) -> bool:
  260. if user_name is None or service is None or servers is None:
  261. globals.logger.warning("Error while trying to update user's servers. User '{}' with service '{}' and servers '{}'".format(user_name, service, servers))
  262. return False
  263. cursor = globals.conn.cursor(buffered=True)
  264. cursor.execute("UPDATE t_users SET servers = %s WHERE LOWER({}) = %s AND service=%s".format(globals.DB_USER_NAME),
  265. [servers, user_name.lower(), service.value])
  266. globals.conn.commit()
  267. cursor.close()
  268. return True
  269. def insert_user_into_db(user_name : str, service : Service, servers : str) -> bool:
  270. '''Add the user to the database'''
  271. if user_name is None or service is None or servers is None:
  272. globals.logger.warning("Error while trying to add user '{}' with service '{}' and servers '{}'".format(user_name, service, servers))
  273. return False
  274. cursor = globals.conn.cursor(buffered=True)
  275. cursor.execute("INSERT INTO t_users ({}, service, servers) VALUES (%s, %s, %s)".format(globals.DB_USER_NAME),
  276. [user_name, service.value, servers])
  277. globals.conn.commit()
  278. cursor.close()
  279. return True
  280. def get_allowed_role(server : int) -> int:
  281. '''Return the allowed role for a given server'''
  282. cursor = globals.conn.cursor(buffered=True, dictionary=True)
  283. cursor.execute("SELECT admin_group FROM t_servers WHERE server=%s LIMIT 1", [str(server)])
  284. allowedRole = cursor.fetchone()
  285. cursor.close()
  286. if allowedRole is None:
  287. return None
  288. return allowedRole["admin_group"]
  289. def get_role_name(provided_role_id : int, server) -> str :
  290. ''' Convert a role ID into a displayable name '''
  291. role_name = None
  292. if provided_role_id is not None:
  293. for role in server.roles:
  294. if str(role.id) == str(provided_role_id):
  295. role_name = role.name
  296. if role_name is None:
  297. role_name = "[DELETED ROLE]"
  298. return role_name