Browse Source

Merge AniList branch

Penta 1 year ago
parent
commit
a2a77d4316

+ 13 - 0
.coveragerc

@@ -0,0 +1,13 @@
+[run]
+branch = True
+source = myanimebot
+
+[report]
+exclude_lines =
+    if self.debug:
+    pragma: no cover
+    raise NotImplementedError
+    if __name__ == .__main__.:
+ignore_errors = True
+omit =
+    tests/*

+ 35 - 0
.github/workflows/docker-image.yml

@@ -0,0 +1,35 @@
+name: Build Docker Image
+
+on:
+  push:
+    branches:
+      - AniList  # Déclenche l'action seulement pour la branche "main"
+  pull_request:
+    branches:
+      - AniList
+
+jobs:
+  build:
+    runs-on: ubuntu-latest
+
+    steps:
+      # Vérifie le code dans le dépôt
+      - name: Checkout code
+        uses: actions/checkout@v2
+
+      # Configure Docker pour s'authentifier sur DockerHub
+      - name: Log in to DockerHub
+        run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
+
+      # Build l'image Docker avec trois tags: un pour le SHA du commit, un pour latest, et un pour Anilist
+      - name: Build the Docker image
+        run: |
+          docker build . -t ${{ secrets.DOCKER_USERNAME }}/myanimebot:${{ github.sha }} -t ${{ secrets.DOCKER_USERNAME }}/myanimebot:anilist -t ${{ secrets.DOCKER_USERNAME }}/myanimebot:latest
+
+      # Pousse l'image Docker sur DockerHub
+      - name: Push the Docker image
+        run: docker push ${{ secrets.DOCKER_USERNAME }}/myanimebot:${{ github.sha }}
+      - name: Push the Docker image with latest tag
+        run: docker push ${{ secrets.DOCKER_USERNAME }}/myanimebot:anilist
+      - name: Push the Docker image with latest tag
+        run: docker push ${{ secrets.DOCKER_USERNAME }}/myanimebot:latest

+ 31 - 0
.travis.yml

@@ -0,0 +1,31 @@
+os: linux
+dist: xenial
+language: python
+python:
+  - 3.7
+before_install:
+  - sudo apt remove *mysql*
+  - sudo rm -rf /var/lib/mysql
+  - sudo apt install software-properties-common gnupg-curl apt-transport-https
+  - sudo apt-key adv --fetch-keys 'http://mariadb.org/mariadb_release_signing_key.asc'
+  - sudo add-apt-repository 'deb [arch=amd64,arm64,i386,ppc64el] http://mariadb.mirrors.ovh.net/MariaDB/repo/10.5/ubuntu xenial main'
+  - sudo apt update
+  - sudo apt install mariadb-client mariadb-server libmariadb-dev libmariadb3
+  - mysql --version
+  - sudo sed -i '/\[mysqld\]/ a default_time_zone='"'"'+1:00'"'"'\nevent_scheduler=ON\nuserstat=1\nperformance_schema=ON' /etc/mysql/mariadb.conf.d/50-server.cnf
+  - sudo systemctl restart mariadb
+  - python --version
+install:
+  - pip install -r requirements.txt -r tests/requirements.txt
+script:
+  # Creating the applicative database
+  - sudo mariadb -e "CREATE DATABASE $DB_NAME; CREATE USER IF NOT EXISTS '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASSWORD'; GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER'@'localhost'; FLUSH PRIVILEGES;"
+  # Import of the database architecture
+  - sudo mariadb $DB_NAME < extra/myanimebot-init.sql
+  # Create conf
+  - envsubst < myanimebot.example.conf > myanimebot.conf
+  # Run tests
+  - python -m pytest tests --cov=.
+after_success:
+  # Submit coverage
+  - bash <(curl -s https://codecov.io/bash)

+ 15 - 0
Dockerfile

@@ -0,0 +1,15 @@
+FROM python:3.7
+
+WORKDIR /opt/MyAnimeBot
+
+COPY requirements.txt .
+
+RUN pip install --no-cache-dir -r requirements.txt
+
+COPY . .
+
+RUN chown -R 0:0 /opt/MyAnimeBot && chmod -R g+rw /opt/MyAnimeBot
+
+EXPOSE 15200
+
+CMD ["python", "myanimebot.py"]

+ 29 - 674
LICENSE

@@ -1,674 +1,29 @@
-                    GNU GENERAL PUBLIC LICENSE
-                       Version 3, 29 June 2007
-
- Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
- Everyone is permitted to copy and distribute verbatim copies
- of this license document, but changing it is not allowed.
-
-                            Preamble
-
-  The GNU General Public License is a free, copyleft license for
-software and other kinds of works.
-
-  The licenses for most software and other practical works are designed
-to take away your freedom to share and change the works.  By contrast,
-the GNU General Public License is intended to guarantee your freedom to
-share and change all versions of a program--to make sure it remains free
-software for all its users.  We, the Free Software Foundation, use the
-GNU General Public License for most of our software; it applies also to
-any other work released this way by its authors.  You can apply it to
-your programs, too.
-
-  When we speak of free software, we are referring to freedom, not
-price.  Our General Public Licenses are designed to make sure that you
-have the freedom to distribute copies of free software (and charge for
-them if you wish), that you receive source code or can get it if you
-want it, that you can change the software or use pieces of it in new
-free programs, and that you know you can do these things.
-
-  To protect your rights, we need to prevent others from denying you
-these rights or asking you to surrender the rights.  Therefore, you have
-certain responsibilities if you distribute copies of the software, or if
-you modify it: responsibilities to respect the freedom of others.
-
-  For example, if you distribute copies of such a program, whether
-gratis or for a fee, you must pass on to the recipients the same
-freedoms that you received.  You must make sure that they, too, receive
-or can get the source code.  And you must show them these terms so they
-know their rights.
-
-  Developers that use the GNU GPL protect your rights with two steps:
-(1) assert copyright on the software, and (2) offer you this License
-giving you legal permission to copy, distribute and/or modify it.
-
-  For the developers' and authors' protection, the GPL clearly explains
-that there is no warranty for this free software.  For both users' and
-authors' sake, the GPL requires that modified versions be marked as
-changed, so that their problems will not be attributed erroneously to
-authors of previous versions.
-
-  Some devices are designed to deny users access to install or run
-modified versions of the software inside them, although the manufacturer
-can do so.  This is fundamentally incompatible with the aim of
-protecting users' freedom to change the software.  The systematic
-pattern of such abuse occurs in the area of products for individuals to
-use, which is precisely where it is most unacceptable.  Therefore, we
-have designed this version of the GPL to prohibit the practice for those
-products.  If such problems arise substantially in other domains, we
-stand ready to extend this provision to those domains in future versions
-of the GPL, as needed to protect the freedom of users.
-
-  Finally, every program is threatened constantly by software patents.
-States should not allow patents to restrict development and use of
-software on general-purpose computers, but in those that do, we wish to
-avoid the special danger that patents applied to a free program could
-make it effectively proprietary.  To prevent this, the GPL assures that
-patents cannot be used to render the program non-free.
-
-  The precise terms and conditions for copying, distribution and
-modification follow.
-
-                       TERMS AND CONDITIONS
-
-  0. Definitions.
-
-  "This License" refers to version 3 of the GNU General Public License.
-
-  "Copyright" also means copyright-like laws that apply to other kinds of
-works, such as semiconductor masks.
-
-  "The Program" refers to any copyrightable work licensed under this
-License.  Each licensee is addressed as "you".  "Licensees" and
-"recipients" may be individuals or organizations.
-
-  To "modify" a work means to copy from or adapt all or part of the work
-in a fashion requiring copyright permission, other than the making of an
-exact copy.  The resulting work is called a "modified version" of the
-earlier work or a work "based on" the earlier work.
-
-  A "covered work" means either the unmodified Program or a work based
-on the Program.
-
-  To "propagate" a work means to do anything with it that, without
-permission, would make you directly or secondarily liable for
-infringement under applicable copyright law, except executing it on a
-computer or modifying a private copy.  Propagation includes copying,
-distribution (with or without modification), making available to the
-public, and in some countries other activities as well.
-
-  To "convey" a work means any kind of propagation that enables other
-parties to make or receive copies.  Mere interaction with a user through
-a computer network, with no transfer of a copy, is not conveying.
-
-  An interactive user interface displays "Appropriate Legal Notices"
-to the extent that it includes a convenient and prominently visible
-feature that (1) displays an appropriate copyright notice, and (2)
-tells the user that there is no warranty for the work (except to the
-extent that warranties are provided), that licensees may convey the
-work under this License, and how to view a copy of this License.  If
-the interface presents a list of user commands or options, such as a
-menu, a prominent item in the list meets this criterion.
-
-  1. Source Code.
-
-  The "source code" for a work means the preferred form of the work
-for making modifications to it.  "Object code" means any non-source
-form of a work.
-
-  A "Standard Interface" means an interface that either is an official
-standard defined by a recognized standards body, or, in the case of
-interfaces specified for a particular programming language, one that
-is widely used among developers working in that language.
-
-  The "System Libraries" of an executable work include anything, other
-than the work as a whole, that (a) is included in the normal form of
-packaging a Major Component, but which is not part of that Major
-Component, and (b) serves only to enable use of the work with that
-Major Component, or to implement a Standard Interface for which an
-implementation is available to the public in source code form.  A
-"Major Component", in this context, means a major essential component
-(kernel, window system, and so on) of the specific operating system
-(if any) on which the executable work runs, or a compiler used to
-produce the work, or an object code interpreter used to run it.
-
-  The "Corresponding Source" for a work in object code form means all
-the source code needed to generate, install, and (for an executable
-work) run the object code and to modify the work, including scripts to
-control those activities.  However, it does not include the work's
-System Libraries, or general-purpose tools or generally available free
-programs which are used unmodified in performing those activities but
-which are not part of the work.  For example, Corresponding Source
-includes interface definition files associated with source files for
-the work, and the source code for shared libraries and dynamically
-linked subprograms that the work is specifically designed to require,
-such as by intimate data communication or control flow between those
-subprograms and other parts of the work.
-
-  The Corresponding Source need not include anything that users
-can regenerate automatically from other parts of the Corresponding
-Source.
-
-  The Corresponding Source for a work in source code form is that
-same work.
-
-  2. Basic Permissions.
-
-  All rights granted under this License are granted for the term of
-copyright on the Program, and are irrevocable provided the stated
-conditions are met.  This License explicitly affirms your unlimited
-permission to run the unmodified Program.  The output from running a
-covered work is covered by this License only if the output, given its
-content, constitutes a covered work.  This License acknowledges your
-rights of fair use or other equivalent, as provided by copyright law.
-
-  You may make, run and propagate covered works that you do not
-convey, without conditions so long as your license otherwise remains
-in force.  You may convey covered works to others for the sole purpose
-of having them make modifications exclusively for you, or provide you
-with facilities for running those works, provided that you comply with
-the terms of this License in conveying all material for which you do
-not control copyright.  Those thus making or running the covered works
-for you must do so exclusively on your behalf, under your direction
-and control, on terms that prohibit them from making any copies of
-your copyrighted material outside their relationship with you.
-
-  Conveying under any other circumstances is permitted solely under
-the conditions stated below.  Sublicensing is not allowed; section 10
-makes it unnecessary.
-
-  3. Protecting Users' Legal Rights From Anti-Circumvention Law.
-
-  No covered work shall be deemed part of an effective technological
-measure under any applicable law fulfilling obligations under article
-11 of the WIPO copyright treaty adopted on 20 December 1996, or
-similar laws prohibiting or restricting circumvention of such
-measures.
-
-  When you convey a covered work, you waive any legal power to forbid
-circumvention of technological measures to the extent such circumvention
-is effected by exercising rights under this License with respect to
-the covered work, and you disclaim any intention to limit operation or
-modification of the work as a means of enforcing, against the work's
-users, your or third parties' legal rights to forbid circumvention of
-technological measures.
-
-  4. Conveying Verbatim Copies.
-
-  You may convey verbatim copies of the Program's source code as you
-receive it, in any medium, provided that you conspicuously and
-appropriately publish on each copy an appropriate copyright notice;
-keep intact all notices stating that this License and any
-non-permissive terms added in accord with section 7 apply to the code;
-keep intact all notices of the absence of any warranty; and give all
-recipients a copy of this License along with the Program.
-
-  You may charge any price or no price for each copy that you convey,
-and you may offer support or warranty protection for a fee.
-
-  5. Conveying Modified Source Versions.
-
-  You may convey a work based on the Program, or the modifications to
-produce it from the Program, in the form of source code under the
-terms of section 4, provided that you also meet all of these conditions:
-
-    a) The work must carry prominent notices stating that you modified
-    it, and giving a relevant date.
-
-    b) The work must carry prominent notices stating that it is
-    released under this License and any conditions added under section
-    7.  This requirement modifies the requirement in section 4 to
-    "keep intact all notices".
-
-    c) You must license the entire work, as a whole, under this
-    License to anyone who comes into possession of a copy.  This
-    License will therefore apply, along with any applicable section 7
-    additional terms, to the whole of the work, and all its parts,
-    regardless of how they are packaged.  This License gives no
-    permission to license the work in any other way, but it does not
-    invalidate such permission if you have separately received it.
-
-    d) If the work has interactive user interfaces, each must display
-    Appropriate Legal Notices; however, if the Program has interactive
-    interfaces that do not display Appropriate Legal Notices, your
-    work need not make them do so.
-
-  A compilation of a covered work with other separate and independent
-works, which are not by their nature extensions of the covered work,
-and which are not combined with it such as to form a larger program,
-in or on a volume of a storage or distribution medium, is called an
-"aggregate" if the compilation and its resulting copyright are not
-used to limit the access or legal rights of the compilation's users
-beyond what the individual works permit.  Inclusion of a covered work
-in an aggregate does not cause this License to apply to the other
-parts of the aggregate.
-
-  6. Conveying Non-Source Forms.
-
-  You may convey a covered work in object code form under the terms
-of sections 4 and 5, provided that you also convey the
-machine-readable Corresponding Source under the terms of this License,
-in one of these ways:
-
-    a) Convey the object code in, or embodied in, a physical product
-    (including a physical distribution medium), accompanied by the
-    Corresponding Source fixed on a durable physical medium
-    customarily used for software interchange.
-
-    b) Convey the object code in, or embodied in, a physical product
-    (including a physical distribution medium), accompanied by a
-    written offer, valid for at least three years and valid for as
-    long as you offer spare parts or customer support for that product
-    model, to give anyone who possesses the object code either (1) a
-    copy of the Corresponding Source for all the software in the
-    product that is covered by this License, on a durable physical
-    medium customarily used for software interchange, for a price no
-    more than your reasonable cost of physically performing this
-    conveying of source, or (2) access to copy the
-    Corresponding Source from a network server at no charge.
-
-    c) Convey individual copies of the object code with a copy of the
-    written offer to provide the Corresponding Source.  This
-    alternative is allowed only occasionally and noncommercially, and
-    only if you received the object code with such an offer, in accord
-    with subsection 6b.
-
-    d) Convey the object code by offering access from a designated
-    place (gratis or for a charge), and offer equivalent access to the
-    Corresponding Source in the same way through the same place at no
-    further charge.  You need not require recipients to copy the
-    Corresponding Source along with the object code.  If the place to
-    copy the object code is a network server, the Corresponding Source
-    may be on a different server (operated by you or a third party)
-    that supports equivalent copying facilities, provided you maintain
-    clear directions next to the object code saying where to find the
-    Corresponding Source.  Regardless of what server hosts the
-    Corresponding Source, you remain obligated to ensure that it is
-    available for as long as needed to satisfy these requirements.
-
-    e) Convey the object code using peer-to-peer transmission, provided
-    you inform other peers where the object code and Corresponding
-    Source of the work are being offered to the general public at no
-    charge under subsection 6d.
-
-  A separable portion of the object code, whose source code is excluded
-from the Corresponding Source as a System Library, need not be
-included in conveying the object code work.
-
-  A "User Product" is either (1) a "consumer product", which means any
-tangible personal property which is normally used for personal, family,
-or household purposes, or (2) anything designed or sold for incorporation
-into a dwelling.  In determining whether a product is a consumer product,
-doubtful cases shall be resolved in favor of coverage.  For a particular
-product received by a particular user, "normally used" refers to a
-typical or common use of that class of product, regardless of the status
-of the particular user or of the way in which the particular user
-actually uses, or expects or is expected to use, the product.  A product
-is a consumer product regardless of whether the product has substantial
-commercial, industrial or non-consumer uses, unless such uses represent
-the only significant mode of use of the product.
-
-  "Installation Information" for a User Product means any methods,
-procedures, authorization keys, or other information required to install
-and execute modified versions of a covered work in that User Product from
-a modified version of its Corresponding Source.  The information must
-suffice to ensure that the continued functioning of the modified object
-code is in no case prevented or interfered with solely because
-modification has been made.
-
-  If you convey an object code work under this section in, or with, or
-specifically for use in, a User Product, and the conveying occurs as
-part of a transaction in which the right of possession and use of the
-User Product is transferred to the recipient in perpetuity or for a
-fixed term (regardless of how the transaction is characterized), the
-Corresponding Source conveyed under this section must be accompanied
-by the Installation Information.  But this requirement does not apply
-if neither you nor any third party retains the ability to install
-modified object code on the User Product (for example, the work has
-been installed in ROM).
-
-  The requirement to provide Installation Information does not include a
-requirement to continue to provide support service, warranty, or updates
-for a work that has been modified or installed by the recipient, or for
-the User Product in which it has been modified or installed.  Access to a
-network may be denied when the modification itself materially and
-adversely affects the operation of the network or violates the rules and
-protocols for communication across the network.
-
-  Corresponding Source conveyed, and Installation Information provided,
-in accord with this section must be in a format that is publicly
-documented (and with an implementation available to the public in
-source code form), and must require no special password or key for
-unpacking, reading or copying.
-
-  7. Additional Terms.
-
-  "Additional permissions" are terms that supplement the terms of this
-License by making exceptions from one or more of its conditions.
-Additional permissions that are applicable to the entire Program shall
-be treated as though they were included in this License, to the extent
-that they are valid under applicable law.  If additional permissions
-apply only to part of the Program, that part may be used separately
-under those permissions, but the entire Program remains governed by
-this License without regard to the additional permissions.
-
-  When you convey a copy of a covered work, you may at your option
-remove any additional permissions from that copy, or from any part of
-it.  (Additional permissions may be written to require their own
-removal in certain cases when you modify the work.)  You may place
-additional permissions on material, added by you to a covered work,
-for which you have or can give appropriate copyright permission.
-
-  Notwithstanding any other provision of this License, for material you
-add to a covered work, you may (if authorized by the copyright holders of
-that material) supplement the terms of this License with terms:
-
-    a) Disclaiming warranty or limiting liability differently from the
-    terms of sections 15 and 16 of this License; or
-
-    b) Requiring preservation of specified reasonable legal notices or
-    author attributions in that material or in the Appropriate Legal
-    Notices displayed by works containing it; or
-
-    c) Prohibiting misrepresentation of the origin of that material, or
-    requiring that modified versions of such material be marked in
-    reasonable ways as different from the original version; or
-
-    d) Limiting the use for publicity purposes of names of licensors or
-    authors of the material; or
-
-    e) Declining to grant rights under trademark law for use of some
-    trade names, trademarks, or service marks; or
-
-    f) Requiring indemnification of licensors and authors of that
-    material by anyone who conveys the material (or modified versions of
-    it) with contractual assumptions of liability to the recipient, for
-    any liability that these contractual assumptions directly impose on
-    those licensors and authors.
-
-  All other non-permissive additional terms are considered "further
-restrictions" within the meaning of section 10.  If the Program as you
-received it, or any part of it, contains a notice stating that it is
-governed by this License along with a term that is a further
-restriction, you may remove that term.  If a license document contains
-a further restriction but permits relicensing or conveying under this
-License, you may add to a covered work material governed by the terms
-of that license document, provided that the further restriction does
-not survive such relicensing or conveying.
-
-  If you add terms to a covered work in accord with this section, you
-must place, in the relevant source files, a statement of the
-additional terms that apply to those files, or a notice indicating
-where to find the applicable terms.
-
-  Additional terms, permissive or non-permissive, may be stated in the
-form of a separately written license, or stated as exceptions;
-the above requirements apply either way.
-
-  8. Termination.
-
-  You may not propagate or modify a covered work except as expressly
-provided under this License.  Any attempt otherwise to propagate or
-modify it is void, and will automatically terminate your rights under
-this License (including any patent licenses granted under the third
-paragraph of section 11).
-
-  However, if you cease all violation of this License, then your
-license from a particular copyright holder is reinstated (a)
-provisionally, unless and until the copyright holder explicitly and
-finally terminates your license, and (b) permanently, if the copyright
-holder fails to notify you of the violation by some reasonable means
-prior to 60 days after the cessation.
-
-  Moreover, your license from a particular copyright holder is
-reinstated permanently if the copyright holder notifies you of the
-violation by some reasonable means, this is the first time you have
-received notice of violation of this License (for any work) from that
-copyright holder, and you cure the violation prior to 30 days after
-your receipt of the notice.
-
-  Termination of your rights under this section does not terminate the
-licenses of parties who have received copies or rights from you under
-this License.  If your rights have been terminated and not permanently
-reinstated, you do not qualify to receive new licenses for the same
-material under section 10.
-
-  9. Acceptance Not Required for Having Copies.
-
-  You are not required to accept this License in order to receive or
-run a copy of the Program.  Ancillary propagation of a covered work
-occurring solely as a consequence of using peer-to-peer transmission
-to receive a copy likewise does not require acceptance.  However,
-nothing other than this License grants you permission to propagate or
-modify any covered work.  These actions infringe copyright if you do
-not accept this License.  Therefore, by modifying or propagating a
-covered work, you indicate your acceptance of this License to do so.
-
-  10. Automatic Licensing of Downstream Recipients.
-
-  Each time you convey a covered work, the recipient automatically
-receives a license from the original licensors, to run, modify and
-propagate that work, subject to this License.  You are not responsible
-for enforcing compliance by third parties with this License.
-
-  An "entity transaction" is a transaction transferring control of an
-organization, or substantially all assets of one, or subdividing an
-organization, or merging organizations.  If propagation of a covered
-work results from an entity transaction, each party to that
-transaction who receives a copy of the work also receives whatever
-licenses to the work the party's predecessor in interest had or could
-give under the previous paragraph, plus a right to possession of the
-Corresponding Source of the work from the predecessor in interest, if
-the predecessor has it or can get it with reasonable efforts.
-
-  You may not impose any further restrictions on the exercise of the
-rights granted or affirmed under this License.  For example, you may
-not impose a license fee, royalty, or other charge for exercise of
-rights granted under this License, and you may not initiate litigation
-(including a cross-claim or counterclaim in a lawsuit) alleging that
-any patent claim is infringed by making, using, selling, offering for
-sale, or importing the Program or any portion of it.
-
-  11. Patents.
-
-  A "contributor" is a copyright holder who authorizes use under this
-License of the Program or a work on which the Program is based.  The
-work thus licensed is called the contributor's "contributor version".
-
-  A contributor's "essential patent claims" are all patent claims
-owned or controlled by the contributor, whether already acquired or
-hereafter acquired, that would be infringed by some manner, permitted
-by this License, of making, using, or selling its contributor version,
-but do not include claims that would be infringed only as a
-consequence of further modification of the contributor version.  For
-purposes of this definition, "control" includes the right to grant
-patent sublicenses in a manner consistent with the requirements of
-this License.
-
-  Each contributor grants you a non-exclusive, worldwide, royalty-free
-patent license under the contributor's essential patent claims, to
-make, use, sell, offer for sale, import and otherwise run, modify and
-propagate the contents of its contributor version.
-
-  In the following three paragraphs, a "patent license" is any express
-agreement or commitment, however denominated, not to enforce a patent
-(such as an express permission to practice a patent or covenant not to
-sue for patent infringement).  To "grant" such a patent license to a
-party means to make such an agreement or commitment not to enforce a
-patent against the party.
-
-  If you convey a covered work, knowingly relying on a patent license,
-and the Corresponding Source of the work is not available for anyone
-to copy, free of charge and under the terms of this License, through a
-publicly available network server or other readily accessible means,
-then you must either (1) cause the Corresponding Source to be so
-available, or (2) arrange to deprive yourself of the benefit of the
-patent license for this particular work, or (3) arrange, in a manner
-consistent with the requirements of this License, to extend the patent
-license to downstream recipients.  "Knowingly relying" means you have
-actual knowledge that, but for the patent license, your conveying the
-covered work in a country, or your recipient's use of the covered work
-in a country, would infringe one or more identifiable patents in that
-country that you have reason to believe are valid.
-
-  If, pursuant to or in connection with a single transaction or
-arrangement, you convey, or propagate by procuring conveyance of, a
-covered work, and grant a patent license to some of the parties
-receiving the covered work authorizing them to use, propagate, modify
-or convey a specific copy of the covered work, then the patent license
-you grant is automatically extended to all recipients of the covered
-work and works based on it.
-
-  A patent license is "discriminatory" if it does not include within
-the scope of its coverage, prohibits the exercise of, or is
-conditioned on the non-exercise of one or more of the rights that are
-specifically granted under this License.  You may not convey a covered
-work if you are a party to an arrangement with a third party that is
-in the business of distributing software, under which you make payment
-to the third party based on the extent of your activity of conveying
-the work, and under which the third party grants, to any of the
-parties who would receive the covered work from you, a discriminatory
-patent license (a) in connection with copies of the covered work
-conveyed by you (or copies made from those copies), or (b) primarily
-for and in connection with specific products or compilations that
-contain the covered work, unless you entered into that arrangement,
-or that patent license was granted, prior to 28 March 2007.
-
-  Nothing in this License shall be construed as excluding or limiting
-any implied license or other defenses to infringement that may
-otherwise be available to you under applicable patent law.
-
-  12. No Surrender of Others' Freedom.
-
-  If conditions are imposed on you (whether by court order, agreement or
-otherwise) that contradict the conditions of this License, they do not
-excuse you from the conditions of this License.  If you cannot convey a
-covered work so as to satisfy simultaneously your obligations under this
-License and any other pertinent obligations, then as a consequence you may
-not convey it at all.  For example, if you agree to terms that obligate you
-to collect a royalty for further conveying from those to whom you convey
-the Program, the only way you could satisfy both those terms and this
-License would be to refrain entirely from conveying the Program.
-
-  13. Use with the GNU Affero General Public License.
-
-  Notwithstanding any other provision of this License, you have
-permission to link or combine any covered work with a work licensed
-under version 3 of the GNU Affero General Public License into a single
-combined work, and to convey the resulting work.  The terms of this
-License will continue to apply to the part which is the covered work,
-but the special requirements of the GNU Affero General Public License,
-section 13, concerning interaction through a network will apply to the
-combination as such.
-
-  14. Revised Versions of this License.
-
-  The Free Software Foundation may publish revised and/or new versions of
-the GNU General Public License from time to time.  Such new versions will
-be similar in spirit to the present version, but may differ in detail to
-address new problems or concerns.
-
-  Each version is given a distinguishing version number.  If the
-Program specifies that a certain numbered version of the GNU General
-Public License "or any later version" applies to it, you have the
-option of following the terms and conditions either of that numbered
-version or of any later version published by the Free Software
-Foundation.  If the Program does not specify a version number of the
-GNU General Public License, you may choose any version ever published
-by the Free Software Foundation.
-
-  If the Program specifies that a proxy can decide which future
-versions of the GNU General Public License can be used, that proxy's
-public statement of acceptance of a version permanently authorizes you
-to choose that version for the Program.
-
-  Later license versions may give you additional or different
-permissions.  However, no additional obligations are imposed on any
-author or copyright holder as a result of your choosing to follow a
-later version.
-
-  15. Disclaimer of Warranty.
-
-  THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
-APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
-HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
-OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
-THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
-IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
-ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
-
-  16. Limitation of Liability.
-
-  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
-WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
-THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
-GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
-USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
-DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
-PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
-EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
-SUCH DAMAGES.
-
-  17. Interpretation of Sections 15 and 16.
-
-  If the disclaimer of warranty and limitation of liability provided
-above cannot be given local legal effect according to their terms,
-reviewing courts shall apply local law that most closely approximates
-an absolute waiver of all civil liability in connection with the
-Program, unless a warranty or assumption of liability accompanies a
-copy of the Program in return for a fee.
-
-                     END OF TERMS AND CONDITIONS
-
-            How to Apply These Terms to Your New Programs
-
-  If you develop a new program, and you want it to be of the greatest
-possible use to the public, the best way to achieve this is to make it
-free software which everyone can redistribute and change under these terms.
-
-  To do so, attach the following notices to the program.  It is safest
-to attach them to the start of each source file to most effectively
-state the exclusion of warranty; and each file should have at least
-the "copyright" line and a pointer to where the full notice is found.
-
-    <one line to give the program's name and a brief idea of what it does.>
-    Copyright (C) <year>  <name of author>
-
-    This program is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <https://www.gnu.org/licenses/>.
-
-Also add information on how to contact you by electronic and paper mail.
-
-  If the program does terminal interaction, make it output a short
-notice like this when it starts in an interactive mode:
-
-    <program>  Copyright (C) <year>  <name of author>
-    This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
-    This is free software, and you are welcome to redistribute it
-    under certain conditions; type `show c' for details.
-
-The hypothetical commands `show w' and `show c' should show the appropriate
-parts of the General Public License.  Of course, your program's commands
-might be different; for a GUI interface, you would use an "about box".
-
-  You should also get your employer (if you work as a programmer) or school,
-if any, to sign a "copyright disclaimer" for the program, if necessary.
-For more information on this, and how to apply and follow the GNU GPL, see
-<https://www.gnu.org/licenses/>.
-
-  The GNU General Public License does not permit incorporating your program
-into proprietary programs.  If your program is a subroutine library, you
-may consider it more useful to permit linking proprietary applications with
-the library.  If this is what you want to do, use the GNU Lesser General
-Public License instead of this License.  But first, please read
-<https://www.gnu.org/licenses/why-not-lgpl.html>.
+BSD 3-Clause License
+
+Copyright (c) 2018-2021, Andy Esnard
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this
+   list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice,
+   this list of conditions and the following disclaimer in the documentation
+   and/or other materials provided with the distribution.
+
+3. Neither the name of the copyright holder nor the names of its
+   contributors may be used to endorse or promote products derived from
+   this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

+ 6 - 1
README.md

@@ -1,3 +1,8 @@
 # MyAnimeBot
 # MyAnimeBot
 
 
-Installation guide: https://wiki.pentou.eu/project/myanimebot/py/install_rhel7
+[![Build Status](https://travis-ci.com/Penta/MyAnimeBot.svg?branch=master)](https://travis-ci.com/Penta/MyAnimeBot) [![codecov](https://codecov.io/gh/Penta/MyAnimeBot/branch/master/graph/badge.svg?token=8U3NIBMGB2)](https://codecov.io/gh/Penta/MyAnimeBot)
+
+MyAnimeBot is a Discord bot that can display notifications on your Discord server when a user watch or read something and update its MyAnimeList and/or Anilist profile.
+
+## Installation guide
+[How to install MyAnimeBot](https://wiki.pentou.eu/en/project/myanimebot/install_rhel7)

+ 0 - 20
extra/malbot.service

@@ -1,20 +0,0 @@
-[Unit]
-Description=MyAnimeList Discord Bot
-After=network.target nss-lookup.target
-
-[Service]
-Type=simple
-WorkingDirectory=/opt/malbot/
-ExecStart=/usr/local/bin/python3.7 /opt/malbot/myanimebot.py
-ExecStop=/bin/kill -15 $MAINPID
-Restart=on-failure
-
-User=malbot
-Group=malbot
-
-StandardOutput=syslog
-StandardError=syslog
-SyslogIdentifier=malbot
-
-[Install]
-WantedBy=multi-user.target

+ 183 - 148
extra/initDB.sql → extra/myanimebot-init.sql

@@ -1,6 +1,7 @@
 -- --------------------------------------------------------
 -- --------------------------------------------------------
--- Server version:               10.5.5-MariaDB - MariaDB Server
--- Server OS:                    Linux
+-- Server version:               10.5.12-MariaDB-log - FreeBSD Ports
+-- Server OS:                    FreeBSD12.2
+-- HeidiSQL Version:             11.3.0.6295
 -- --------------------------------------------------------
 -- --------------------------------------------------------
 
 
 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
@@ -8,16 +9,15 @@
 /*!50503 SET NAMES utf8mb4 */;
 /*!50503 SET NAMES utf8mb4 */;
 /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
 /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
 /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
 /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
+/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
 
 
 
 
--- Dumping database structure for myanimebot
-CREATE DATABASE IF NOT EXISTS `myanimebot` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
-USE `myanimebot`;
-
 -- Dumping structure for view myanimebot.check_DuplicateFeeds
 -- Dumping structure for view myanimebot.check_DuplicateFeeds
 -- Creating temporary table to overcome VIEW dependency errors
 -- Creating temporary table to overcome VIEW dependency errors
 CREATE TABLE `check_DuplicateFeeds` (
 CREATE TABLE `check_DuplicateFeeds` (
 	`published` DATETIME NOT NULL,
 	`published` DATETIME NOT NULL,
+	`last seen` DATETIME NULL,
+	`service` TINYTEXT NOT NULL COLLATE 'utf8mb4_general_ci',
 	`title` MEDIUMTEXT NULL COLLATE 'utf8mb4_general_ci',
 	`title` MEDIUMTEXT NULL COLLATE 'utf8mb4_general_ci',
 	`user` TINYTEXT NULL COLLATE 'utf8mb4_general_ci',
 	`user` TINYTEXT NULL COLLATE 'utf8mb4_general_ci',
 	`total` BIGINT(21) NOT NULL
 	`total` BIGINT(21) NOT NULL
@@ -27,6 +27,7 @@ CREATE TABLE `check_DuplicateFeeds` (
 -- Creating temporary table to overcome VIEW dependency errors
 -- Creating temporary table to overcome VIEW dependency errors
 CREATE TABLE `check_DuplicateMedia` (
 CREATE TABLE `check_DuplicateMedia` (
 	`guid` MEDIUMTEXT NOT NULL COLLATE 'utf8mb4_general_ci',
 	`guid` MEDIUMTEXT NOT NULL COLLATE 'utf8mb4_general_ci',
+	`service` TINYTEXT NOT NULL COLLATE 'utf8mb4_general_ci',
 	`title` MEDIUMTEXT NULL COLLATE 'utf8mb4_general_ci',
 	`title` MEDIUMTEXT NULL COLLATE 'utf8mb4_general_ci',
 	`total` BIGINT(21) NOT NULL
 	`total` BIGINT(21) NOT NULL
 ) ENGINE=MyISAM;
 ) ENGINE=MyISAM;
@@ -35,6 +36,7 @@ CREATE TABLE `check_DuplicateMedia` (
 -- Creating temporary table to overcome VIEW dependency errors
 -- Creating temporary table to overcome VIEW dependency errors
 CREATE TABLE `check_EmptyThumbnail` (
 CREATE TABLE `check_EmptyThumbnail` (
 	`id` INT(11) UNSIGNED NOT NULL,
 	`id` INT(11) UNSIGNED NOT NULL,
+	`service` TINYTEXT NOT NULL COLLATE 'utf8mb4_general_ci',
 	`guid` MEDIUMTEXT NOT NULL COLLATE 'utf8mb4_general_ci',
 	`guid` MEDIUMTEXT NOT NULL COLLATE 'utf8mb4_general_ci',
 	`title` MEDIUMTEXT NULL COLLATE 'utf8mb4_general_ci',
 	`title` MEDIUMTEXT NULL COLLATE 'utf8mb4_general_ci',
 	`thumbnail` MEDIUMTEXT NULL COLLATE 'utf8mb4_general_ci'
 	`thumbnail` MEDIUMTEXT NULL COLLATE 'utf8mb4_general_ci'
@@ -59,6 +61,7 @@ CREATE TABLE `check_Index` (
 -- Creating temporary table to overcome VIEW dependency errors
 -- Creating temporary table to overcome VIEW dependency errors
 CREATE TABLE `check_OrphanMedias` (
 CREATE TABLE `check_OrphanMedias` (
 	`id` INT(11) UNSIGNED NOT NULL,
 	`id` INT(11) UNSIGNED NOT NULL,
+	`service` TINYTEXT NOT NULL COLLATE 'utf8mb4_general_ci',
 	`media` MEDIUMTEXT NULL COLLATE 'utf8mb4_general_ci'
 	`media` MEDIUMTEXT NULL COLLATE 'utf8mb4_general_ci'
 ) ENGINE=MyISAM;
 ) ENGINE=MyISAM;
 
 
@@ -76,6 +79,136 @@ CREATE TABLE `check_TablesDiskUsage` (
 DELIMITER //
 DELIMITER //
 CREATE EVENT `event_generate_DailyAveragePerUser` ON SCHEDULE EVERY 1 DAY STARTS '2020-01-05 01:30:00' ON COMPLETION PRESERVE ENABLE DO BEGIN
 CREATE EVENT `event_generate_DailyAveragePerUser` ON SCHEDULE EVERY 1 DAY STARTS '2020-01-05 01:30:00' ON COMPLETION PRESERVE ENABLE DO BEGIN
 
 
+CALL spe_generate_DailyAveragePerUser;
+
+END//
+DELIMITER ;
+
+-- Dumping structure for event myanimebot.event_generate_TopAnimes
+DELIMITER //
+CREATE EVENT `event_generate_TopAnimes` ON SCHEDULE EVERY 1 DAY STARTS '2019-12-08 05:00:00' ON COMPLETION PRESERVE ENABLE DO BEGIN
+
+CALL spe_generate_TopAnimes;
+
+END//
+DELIMITER ;
+
+-- Dumping structure for event myanimebot.event_generate_TopUniqueAnimePerUsers
+DELIMITER //
+CREATE EVENT `event_generate_TopUniqueAnimePerUsers` ON SCHEDULE EVERY 1 DAY STARTS '2019-11-05 05:00:00' ON COMPLETION PRESERVE ENABLE COMMENT 'Daily job' DO BEGIN
+
+CALL spe_generate_TopUniqueAnimePerUsers;
+
+END//
+DELIMITER ;
+
+-- Dumping structure for event myanimebot.event_generate_TotalDifferentAnimesPerUser
+DELIMITER //
+CREATE EVENT `event_generate_TotalDifferentAnimesPerUser` ON SCHEDULE EVERY 1 HOUR STARTS '2019-11-05 03:00:00' ON COMPLETION PRESERVE ENABLE COMMENT 'Daily job' DO BEGIN
+
+CALL spe_generate_TotalDifferentAnimesPerUser;
+
+END//
+DELIMITER ;
+
+-- Dumping structure for event myanimebot.event_history
+DELIMITER //
+CREATE EVENT `event_history` ON SCHEDULE EVERY 10 MINUTE STARTS '2019-11-15 00:00:00' ON COMPLETION PRESERVE ENABLE COMMENT 'Update the history table every 10 minutes' DO BEGIN
+
+# Initialization of my time variable
+SET @date = NOW();
+
+# We get the values that we want to store
+SELECT @totalFeeds          := total                  FROM v_TotalFeeds;
+SELECT @totalUniqueFeeds    := COUNT(0)               FROM job_TopUniqueAnimePerUsers;
+SELECT @totalMedia          := total                  FROM v_TotalAnimes;
+SELECT @totalUsers          := COUNT(0)               FROM t_users;
+SELECT @totalServers        := COUNT(0)               FROM t_servers;
+SELECT @totalDuplicateFeeds := COUNT(0)               FROM check_DuplicateFeeds;
+SELECT @totalDuplicateMedia := COUNT(0)               FROM check_DuplicateMedia;
+SELECT @totalEmptyThumbnail := COUNT(0)               FROM check_EmptyThumbnail;
+SELECT @totalInactiveUsers  := COUNT(0)               FROM v_ActiveUsers                 WHERE active = '0';
+SELECT @spaceFeedsTable     := total                  FROM check_TablesDiskUsage         WHERE check_TablesDiskUsage.table = "t_feeds";
+SELECT @spaceAnimesTable    := total                  FROM check_TablesDiskUsage         WHERE check_TablesDiskUsage.table = "t_animes";
+SELECT @spaceUsersTable     := total                  FROM check_TablesDiskUsage         WHERE check_TablesDiskUsage.table = "t_users";
+SELECT @spaceServersTable   := total                  FROM check_TablesDiskUsage         WHERE check_TablesDiskUsage.table = "t_servers";
+SELECT @dailyAveragePerUser := ROUND(AVG(average), 3) FROM job_DailyAveragePerUser;
+SELECT @totalOrphanMedias   := COUNT(0)               FROM check_OrphanMedias;
+SELECT @nbMediaManga        := total                  FROM v_CountMediaType              WHERE v_CountMediaType.media = "manga";
+SELECT @nbMediaAnime        := total                  FROM v_CountMediaType              WHERE v_CountMediaType.media = "anime";
+SELECT @nbLog               := -1;
+SELECT @nbErrorLog          := -1;
+
+# We insert tour values
+INSERT INTO t_history (date,  nbFeeds,     nbUniqueFeeds,     nbMedia,     nbUsers,     nbServers,     nbDuplicateFeeds,     nbDuplicateMedia,     nbEmptyThumbnail,     nbInactiveUsers,     spaceFeedsTable,  spaceAnimesTable,  spaceUsersTable,    spaceServersTable,  dailyAveragePerUser,  orphanMedias,       nbMediaManga,  nbMediaAnime,  nbLog,  nbErrorLog)
+VALUES                (@date, @totalFeeds, @totalUniqueFeeds, @totalMedia, @totalUsers, @totalServers, @totalDuplicateFeeds, @totalDuplicateMedia, @totalEmptyThumbnail, @totalInactiveUsers, @spaceFeedsTable, @spaceAnimesTable, @spaceServersTable, @spaceServersTable, @dailyAveragePerUser, @totalOrphanMedias, @nbMediaManga, @nbMediaAnime, @nbLog, @nbErrorLog);
+
+END//
+DELIMITER ;
+
+-- Dumping structure for event myanimebot.event_maintenance
+DELIMITER //
+CREATE EVENT `event_maintenance` ON SCHEDULE EVERY 1 DAY STARTS '2019-11-17 06:00:00' ON COMPLETION PRESERVE ENABLE COMMENT 'Executed at 6am, analyze the SQL tables' DO BEGIN
+
+# Using the stored procedure.
+CALL myanimebot.sp_Maintenance();
+
+END//
+DELIMITER ;
+
+-- Dumping structure for table myanimebot.job_DailyAveragePerUser
+CREATE TABLE IF NOT EXISTS `job_DailyAveragePerUser` (
+  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
+  `user` tinytext DEFAULT NULL,
+  `average` decimal(24,4) DEFAULT NULL,
+  PRIMARY KEY (`id`),
+  KEY `idx_user` (`user`(255))
+) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8mb4 COMMENT='Autogenerated - Average daily medias per user';
+
+-- Data exporting was unselected.
+
+-- Dumping structure for table myanimebot.job_TopAnimes
+CREATE TABLE IF NOT EXISTS `job_TopAnimes` (
+  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
+  `anime` mediumtext DEFAULT NULL,
+  `nbUser` bigint(21) NOT NULL DEFAULT 0,
+  `total` bigint(21) NOT NULL DEFAULT 0,
+  PRIMARY KEY (`id`),
+  KEY `idx_anime` (`anime`(768))
+) ENGINE=InnoDB AUTO_INCREMENT=5640 DEFAULT CHARSET=utf8mb4 COMMENT='Autogenerated - Top listed animes and number of users';
+
+-- Data exporting was unselected.
+
+-- Dumping structure for table myanimebot.job_TopUniqueAnimePerUsers
+CREATE TABLE IF NOT EXISTS `job_TopUniqueAnimePerUsers` (
+  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
+  `user` tinytext DEFAULT NULL,
+  `title` mediumtext DEFAULT NULL,
+  `count` bigint(21) NOT NULL DEFAULT 0,
+  PRIMARY KEY (`id`),
+  KEY `idx_user` (`user`(255)),
+  KEY `idx_title` (`title`(768))
+) ENGINE=InnoDB AUTO_INCREMENT=9629 DEFAULT CHARSET=utf8mb4 COMMENT='Autogenerated - Unique Anime feeds per users';
+
+-- Data exporting was unselected.
+
+-- Dumping structure for table myanimebot.job_TotalDifferentAnimesPerUser
+CREATE TABLE IF NOT EXISTS `job_TotalDifferentAnimesPerUser` (
+  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
+  `user` tinytext DEFAULT NULL,
+  `total` bigint(21) NOT NULL DEFAULT 0,
+  PRIMARY KEY (`id`),
+  KEY `idx_user` (`user`(255))
+) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8mb4 COMMENT='Autogenerated - Total of different media per users';
+
+-- Data exporting was unselected.
+
+-- Dumping structure for procedure myanimebot.spe_generate_DailyAveragePerUser
+DELIMITER //
+CREATE PROCEDURE `spe_generate_DailyAveragePerUser`()
+    SQL SECURITY INVOKER
+BEGIN
+
 # Create job_DailyAveragePerUser
 # Create job_DailyAveragePerUser
 
 
 # We drop the curent table
 # We drop the curent table
@@ -116,13 +249,14 @@ ANALYZE TABLE job_DailyAveragePerUser;
 END//
 END//
 DELIMITER ;
 DELIMITER ;
 
 
--- Dumping structure for event myanimebot.event_generate_TopAnimes
+-- Dumping structure for procedure myanimebot.spe_generate_TopAnimes
 DELIMITER //
 DELIMITER //
-CREATE EVENT `event_generate_TopAnimes` ON SCHEDULE EVERY 1 DAY STARTS '2019-12-08 05:00:00' ON COMPLETION PRESERVE ENABLE DO BEGIN
+CREATE PROCEDURE `spe_generate_TopAnimes`()
+    SQL SECURITY INVOKER
+BEGIN
 
 
 # Create job_TopAnimes
 # Create job_TopAnimes
 
 
-# We drop the curent table
 DROP TABLE IF EXISTS job_TopAnimes;
 DROP TABLE IF EXISTS job_TopAnimes;
 
 
 # We recreate the table with the current result of the view
 # We recreate the table with the current result of the view
@@ -150,9 +284,11 @@ ANALYZE TABLE job_TopAnimes;
 END//
 END//
 DELIMITER ;
 DELIMITER ;
 
 
--- Dumping structure for event myanimebot.event_generate_TopUniqueAnimePerUsers
+-- Dumping structure for procedure myanimebot.spe_generate_TopUniqueAnimePerUsers
 DELIMITER //
 DELIMITER //
-CREATE EVENT `event_generate_TopUniqueAnimePerUsers` ON SCHEDULE EVERY 1 DAY STARTS '2019-11-05 05:00:00' ON COMPLETION PRESERVE ENABLE COMMENT 'Daily job' DO BEGIN
+CREATE PROCEDURE `spe_generate_TopUniqueAnimePerUsers`()
+    SQL SECURITY INVOKER
+BEGIN
 
 
 # Create job_TopUniqueAnimePerUsers
 # Create job_TopUniqueAnimePerUsers
 
 
@@ -185,9 +321,11 @@ ANALYZE TABLE job_TopUniqueAnimePerUsers;
 END//
 END//
 DELIMITER ;
 DELIMITER ;
 
 
--- Dumping structure for event myanimebot.event_generate_TotalDifferentAnimesPerUser
+-- Dumping structure for procedure myanimebot.spe_generate_TotalDifferentAnimesPerUser
 DELIMITER //
 DELIMITER //
-CREATE EVENT `event_generate_TotalDifferentAnimesPerUser` ON SCHEDULE EVERY 1 HOUR STARTS '2019-11-05 03:00:00' ON COMPLETION PRESERVE ENABLE COMMENT 'Daily job' DO BEGIN
+CREATE PROCEDURE `spe_generate_TotalDifferentAnimesPerUser`()
+    SQL SECURITY INVOKER
+BEGIN
 
 
 # Create job_TotalDifferentAnimesPerUser
 # Create job_TotalDifferentAnimesPerUser
 
 
@@ -219,98 +357,6 @@ ANALYZE TABLE job_TotalDifferentAnimesPerUser;
 END//
 END//
 DELIMITER ;
 DELIMITER ;
 
 
--- Dumping structure for event myanimebot.event_history
-DELIMITER //
-CREATE EVENT `event_history` ON SCHEDULE EVERY 10 MINUTE STARTS '2019-11-15 00:00:00' ON COMPLETION PRESERVE ENABLE COMMENT 'Update the history table every 10 minutes' DO BEGIN
-
-# Initialization of my time variable
-SET @date = NOW();
-
-# We get the values that we want to store
-SELECT @totalFeeds          := total                  FROM v_TotalFeeds;
-SELECT @totalUniqueFeeds    := COUNT(0)               FROM job_TopUniqueAnimePerUsers;
-SELECT @totalMedia          := total                  FROM v_TotalAnimes;
-SELECT @totalUsers          := COUNT(0)               FROM t_users;
-SELECT @totalServers        := COUNT(0)               FROM t_servers;
-SELECT @totalDuplicateFeeds := COUNT(0)               FROM check_DuplicateFeeds;
-SELECT @totalDuplicateMedia := COUNT(0)               FROM check_DuplicateMedia;
-SELECT @totalEmptyThumbnail := COUNT(0)               FROM check_EmptyThumbnail;
-SELECT @totalInactiveUsers  := COUNT(0)               FROM v_ActiveUsers                 WHERE active = '0';
-SELECT @spaceFeedsTable     := total                  FROM check_TablesDiskUsage         WHERE check_TablesDiskUsage.table = "t_feeds";
-SELECT @spaceAnimesTable    := total                  FROM check_TablesDiskUsage         WHERE check_TablesDiskUsage.table = "t_animes";
-SELECT @spaceUsersTable     := total                  FROM check_TablesDiskUsage         WHERE check_TablesDiskUsage.table = "t_users";
-SELECT @spaceServersTable   := total                  FROM check_TablesDiskUsage         WHERE check_TablesDiskUsage.table = "t_servers";
-SELECT @dailyAveragePerUser := ROUND(AVG(average), 3) FROM job_DailyAveragePerUser;
-SELECT @totalOrphanMedias   := COUNT(0)               FROM check_OrphanMedias;
-SELECT @nbMediaManga        := total                  FROM v_CountMediaType              WHERE v_CountMediaType.media = "manga";
-SELECT @nbMediaAnime        := total                  FROM v_CountMediaType              WHERE v_CountMediaType.media = "anime";
-SELECT @nbLog               := COUNT(0)               FROM t_logs;
-SELECT @nbErrorLog          := COUNT(0)               FROM t_logs                        WHERE LEVEL >= 30;
-
-# We insert tour values
-INSERT INTO t_history (date,  nbFeeds,     nbUniqueFeeds,     nbMedia,     nbUsers,     nbServers,     nbDuplicateFeeds,     nbDuplicateMedia,     nbEmptyThumbnail,     nbInactiveUsers,     spaceFeedsTable,  spaceAnimesTable,  spaceUsersTable,    spaceServersTable,  dailyAveragePerUser,  orphanMedias,       nbMediaManga,  nbMediaAnime,  nbLog,  nbErrorLog)
-VALUES                (@date, @totalFeeds, @totalUniqueFeeds, @totalMedia, @totalUsers, @totalServers, @totalDuplicateFeeds, @totalDuplicateMedia, @totalEmptyThumbnail, @totalInactiveUsers, @spaceFeedsTable, @spaceAnimesTable, @spaceServersTable, @spaceServersTable, @dailyAveragePerUser, @totalOrphanMedias, @nbMediaManga, @nbMediaAnime, @nbLog, @nbErrorLog);
-
-END//
-DELIMITER ;
-
--- Dumping structure for event myanimebot.event_maintenance
-DELIMITER //
-CREATE EVENT `event_maintenance` ON SCHEDULE EVERY 1 DAY STARTS '2019-11-17 06:00:00' ON COMPLETION PRESERVE ENABLE COMMENT 'Executed at 6am, analyze the SQL tables' DO BEGIN
-
-# Using the stored procedure.
-CALL myanimebot.sp_Maintenance();
-
-END//
-DELIMITER ;
-
--- Dumping structure for table myanimebot.job_DailyAveragePerUser
-CREATE TABLE IF NOT EXISTS `job_DailyAveragePerUser` (
-  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
-  `user` tinytext DEFAULT NULL,
-  `average` decimal(24,4) DEFAULT NULL,
-  PRIMARY KEY (`id`),
-  KEY `idx_user` (`user`(255))
-) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8mb4 COMMENT='Autogenerated - Average daily medias per user';
-
--- Data exporting was unselected.
-
--- Dumping structure for table myanimebot.job_TopAnimes
-CREATE TABLE IF NOT EXISTS `job_TopAnimes` (
-  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
-  `anime` mediumtext DEFAULT NULL,
-  `nbUser` bigint(21) NOT NULL DEFAULT 0,
-  `total` bigint(21) NOT NULL DEFAULT 0,
-  PRIMARY KEY (`id`),
-  KEY `idx_anime` (`anime`(768))
-) ENGINE=InnoDB AUTO_INCREMENT=3237 DEFAULT CHARSET=utf8mb4 COMMENT='Autogenerated - Top listed animes and number of users';
-
--- Data exporting was unselected.
-
--- Dumping structure for table myanimebot.job_TopUniqueAnimePerUsers
-CREATE TABLE IF NOT EXISTS `job_TopUniqueAnimePerUsers` (
-  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
-  `user` tinytext DEFAULT NULL,
-  `title` mediumtext DEFAULT NULL,
-  `count` bigint(21) NOT NULL DEFAULT 0,
-  PRIMARY KEY (`id`),
-  KEY `idx_user` (`user`(255)),
-  KEY `idx_title` (`title`(768))
-) ENGINE=InnoDB AUTO_INCREMENT=4883 DEFAULT CHARSET=utf8mb4 COMMENT='Autogenerated - Unique Anime feeds per users';
-
--- Data exporting was unselected.
-
--- Dumping structure for table myanimebot.job_TotalDifferentAnimesPerUser
-CREATE TABLE IF NOT EXISTS `job_TotalDifferentAnimesPerUser` (
-  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
-  `user` tinytext DEFAULT NULL,
-  `total` bigint(21) NOT NULL DEFAULT 0,
-  PRIMARY KEY (`id`),
-  KEY `idx_user` (`user`(255))
-) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8mb4 COMMENT='Autogenerated - Total of different media per users';
-
--- Data exporting was unselected.
-
 -- Dumping structure for procedure myanimebot.sp_AnimeCountPerKeyword
 -- Dumping structure for procedure myanimebot.sp_AnimeCountPerKeyword
 DELIMITER //
 DELIMITER //
 CREATE PROCEDURE `sp_AnimeCountPerKeyword`(
 CREATE PROCEDURE `sp_AnimeCountPerKeyword`(
@@ -368,13 +414,29 @@ LIMIT limit_var
 END//
 END//
 DELIMITER ;
 DELIMITER ;
 
 
+-- Dumping structure for procedure myanimebot.sp_InitBoot
+DELIMITER //
+CREATE PROCEDURE `sp_InitBoot`()
+    SQL SECURITY INVOKER
+BEGIN
+
+# Generate all event tables
+
+CALL spe_generate_DailyAveragePerUser;
+CALL spe_generate_TopAnimes;
+CALL spe_generate_TopUniqueAnimePerUsers;
+CALL spe_generate_TotalDifferentAnimesPerUser;
+
+END//
+DELIMITER ;
+
 -- Dumping structure for procedure myanimebot.sp_Maintenance
 -- Dumping structure for procedure myanimebot.sp_Maintenance
 DELIMITER //
 DELIMITER //
 CREATE PROCEDURE `sp_Maintenance`()
 CREATE PROCEDURE `sp_Maintenance`()
 BEGIN
 BEGIN
 
 
 # Analyzing database's tables.
 # Analyzing database's tables.
-ANALYZE TABLE t_animes, t_feeds, t_history, t_servers, t_sys, t_users, t_logs, t_availability;
+ANALYZE TABLE t_animes, t_feeds, t_history, t_servers, t_sys, t_users;
 
 
 END//
 END//
 DELIMITER ;
 DELIMITER ;
@@ -474,21 +536,9 @@ CREATE TABLE IF NOT EXISTS `t_animes` (
   KEY `idx_title` (`title`(768)),
   KEY `idx_title` (`title`(768)),
   KEY `idx_discoverer` (`discoverer`(255)),
   KEY `idx_discoverer` (`discoverer`(255)),
   KEY `idx_media` (`media`(255)),
   KEY `idx_media` (`media`(255)),
+  KEY `idx_service` (`service`(255)),
   FULLTEXT KEY `idx_title_str` (`title`)
   FULLTEXT KEY `idx_title_str` (`title`)
-) ENGINE=InnoDB AUTO_INCREMENT=3329 DEFAULT CHARSET=utf8mb4 AVG_ROW_LENGTH=224;
-
--- Data exporting was unselected.
-
--- Dumping structure for table myanimebot.t_availability
-CREATE TABLE IF NOT EXISTS `t_availability` (
-  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
-  `date` datetime NOT NULL DEFAULT current_timestamp(),
-  `service` tinytext CHARACTER SET latin1 NOT NULL DEFAULT 'mal',
-  `code` smallint(6) NOT NULL DEFAULT 0,
-  PRIMARY KEY (`id`),
-  KEY `idx_date` (`date`),
-  KEY `idx_service` (`service`(255))
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+) ENGINE=InnoDB AUTO_INCREMENT=5185 DEFAULT CHARSET=utf8mb4 AVG_ROW_LENGTH=224;
 
 
 -- Data exporting was unselected.
 -- Data exporting was unselected.
 
 
@@ -502,13 +552,15 @@ CREATE TABLE IF NOT EXISTS `t_feeds` (
   `user` tinytext DEFAULT NULL,
   `user` tinytext DEFAULT NULL,
   `found` datetime NOT NULL DEFAULT current_timestamp(),
   `found` datetime NOT NULL DEFAULT current_timestamp(),
   `type` tinytext DEFAULT 'N/A',
   `type` tinytext DEFAULT 'N/A',
+  `obsolete` tinyint(3) unsigned NOT NULL DEFAULT 0,
   PRIMARY KEY (`id`),
   PRIMARY KEY (`id`),
   KEY `idx_user` (`user`(255)),
   KEY `idx_user` (`user`(255)),
   KEY `idx_title` (`title`(768)),
   KEY `idx_title` (`title`(768)),
   KEY `idx_published` (`published`),
   KEY `idx_published` (`published`),
   KEY `idx_type` (`type`(255)),
   KEY `idx_type` (`type`(255)),
+  KEY `idx_service` (`service`(255)),
   FULLTEXT KEY `idx_title_str` (`title`)
   FULLTEXT KEY `idx_title_str` (`title`)
-) ENGINE=InnoDB AUTO_INCREMENT=14769 DEFAULT CHARSET=utf8mb4 AVG_ROW_LENGTH=172;
+) ENGINE=InnoDB AUTO_INCREMENT=29821 DEFAULT CHARSET=utf8mb4 AVG_ROW_LENGTH=172;
 
 
 -- Data exporting was unselected.
 -- Data exporting was unselected.
 
 
@@ -539,25 +591,6 @@ CREATE TABLE IF NOT EXISTS `t_history` (
 
 
 -- Data exporting was unselected.
 -- Data exporting was unselected.
 
 
--- Dumping structure for table myanimebot.t_logs
-CREATE TABLE IF NOT EXISTS `t_logs` (
-  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
-  `host` tinytext CHARACTER SET latin1 NOT NULL DEFAULT 'unspecified.host',
-  `level` int(11) DEFAULT NULL,
-  `type` mediumtext CHARACTER SET utf8 DEFAULT NULL,
-  `log` text CHARACTER SET latin1 DEFAULT NULL,
-  `date` datetime NOT NULL DEFAULT current_timestamp(),
-  `source` tinytext CHARACTER SET latin1 NOT NULL DEFAULT 'unknown',
-  PRIMARY KEY (`id`),
-  KEY `idx_level` (`level`) USING BTREE,
-  KEY `idx_date` (`date`) USING BTREE,
-  KEY `idx_host` (`host`(255)),
-  KEY `idx_by` (`source`(255)) USING BTREE,
-  FULLTEXT KEY `idx_log` (`log`)
-) ENGINE=InnoDB AUTO_INCREMENT=62280 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPRESSED;
-
--- Data exporting was unselected.
-
 -- Dumping structure for table myanimebot.t_servers
 -- Dumping structure for table myanimebot.t_servers
 CREATE TABLE IF NOT EXISTS `t_servers` (
 CREATE TABLE IF NOT EXISTS `t_servers` (
   `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
   `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
@@ -587,11 +620,11 @@ CREATE TABLE IF NOT EXISTS `t_users` (
   `servers` text DEFAULT NULL,
   `servers` text DEFAULT NULL,
   `added` datetime NOT NULL DEFAULT current_timestamp(),
   `added` datetime NOT NULL DEFAULT current_timestamp(),
   PRIMARY KEY (`id`),
   PRIMARY KEY (`id`),
-  UNIQUE KEY `idx_user` (`mal_user`(255)) USING BTREE,
   KEY `idx_servers` (`servers`(768)),
   KEY `idx_servers` (`servers`(768)),
   KEY `idx_service` (`service`(255)),
   KEY `idx_service` (`service`(255)),
+  KEY `idx_user` (`mal_user`(255)) USING BTREE,
   FULLTEXT KEY `idx_servers_str` (`servers`)
   FULLTEXT KEY `idx_servers_str` (`servers`)
-) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8mb4 AVG_ROW_LENGTH=1820 COMMENT='Table where are stored the users of this bot.';
+) ENGINE=InnoDB AUTO_INCREMENT=42 DEFAULT CHARSET=utf8mb4 AVG_ROW_LENGTH=1820 COMMENT='Table where are stored the users of this bot.';
 
 
 -- Data exporting was unselected.
 -- Data exporting was unselected.
 
 
@@ -599,6 +632,7 @@ CREATE TABLE IF NOT EXISTS `t_users` (
 -- Creating temporary table to overcome VIEW dependency errors
 -- Creating temporary table to overcome VIEW dependency errors
 CREATE TABLE `v_ActiveUsers` (
 CREATE TABLE `v_ActiveUsers` (
 	`user` TINYTEXT NOT NULL COLLATE 'utf8mb4_general_ci',
 	`user` TINYTEXT NOT NULL COLLATE 'utf8mb4_general_ci',
+	`service` TINYTEXT NOT NULL COLLATE 'utf8mb4_general_ci',
 	`active` VARCHAR(1) NOT NULL COLLATE 'utf8mb4_general_ci'
 	`active` VARCHAR(1) NOT NULL COLLATE 'utf8mb4_general_ci'
 ) ENGINE=MyISAM;
 ) ENGINE=MyISAM;
 
 
@@ -686,17 +720,17 @@ CREATE TABLE `v_TotalFeeds` (
 -- Dumping structure for view myanimebot.check_DuplicateFeeds
 -- Dumping structure for view myanimebot.check_DuplicateFeeds
 -- Removing temporary table and create final VIEW structure
 -- Removing temporary table and create final VIEW structure
 DROP TABLE IF EXISTS `check_DuplicateFeeds`;
 DROP TABLE IF EXISTS `check_DuplicateFeeds`;
-CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `check_DuplicateFeeds` AS select `t_feeds`.`published` AS `published`,`t_feeds`.`title` AS `title`,`t_feeds`.`user` AS `user`,count(0) AS `total` from `t_feeds` group by `t_feeds`.`published`,`t_feeds`.`title`,`t_feeds`.`user` having count(0) > 1;
+CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `check_DuplicateFeeds` AS select `t_feeds`.`published` AS `published`,max(`t_feeds`.`found`) AS `last seen`,`t_feeds`.`service` AS `service`,`t_feeds`.`title` AS `title`,`t_feeds`.`user` AS `user`,count(0) AS `total` from `t_feeds` group by `t_feeds`.`published`,`t_feeds`.`title`,`t_feeds`.`user` having count(0) > 1;
 
 
 -- Dumping structure for view myanimebot.check_DuplicateMedia
 -- Dumping structure for view myanimebot.check_DuplicateMedia
 -- Removing temporary table and create final VIEW structure
 -- Removing temporary table and create final VIEW structure
 DROP TABLE IF EXISTS `check_DuplicateMedia`;
 DROP TABLE IF EXISTS `check_DuplicateMedia`;
-CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `check_DuplicateMedia` AS select `t_animes`.`guid` AS `guid`,`t_animes`.`title` AS `title`,count(0) AS `total` from `t_animes` group by `t_animes`.`guid`,`t_animes`.`title` having count(0) > 1;
+CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `check_DuplicateMedia` AS select `t_animes`.`guid` AS `guid`,`t_animes`.`service` AS `service`,`t_animes`.`title` AS `title`,count(0) AS `total` from `t_animes` group by `t_animes`.`guid`,`t_animes`.`title` having count(0) > 1;
 
 
 -- Dumping structure for view myanimebot.check_EmptyThumbnail
 -- Dumping structure for view myanimebot.check_EmptyThumbnail
 -- Removing temporary table and create final VIEW structure
 -- Removing temporary table and create final VIEW structure
 DROP TABLE IF EXISTS `check_EmptyThumbnail`;
 DROP TABLE IF EXISTS `check_EmptyThumbnail`;
-CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `check_EmptyThumbnail` AS select `t_animes`.`id` AS `id`,`t_animes`.`guid` AS `guid`,`t_animes`.`title` AS `title`,`t_animes`.`thumbnail` AS `thumbnail` from `t_animes` where `t_animes`.`thumbnail` = '' or `t_animes`.`thumbnail` is null;
+CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `check_EmptyThumbnail` AS select `t_animes`.`id` AS `id`,`t_animes`.`service` AS `service`,`t_animes`.`guid` AS `guid`,`t_animes`.`title` AS `title`,`t_animes`.`thumbnail` AS `thumbnail` from `t_animes` where `t_animes`.`thumbnail` = '' or `t_animes`.`thumbnail` is null;
 
 
 -- Dumping structure for view myanimebot.check_EventExecution
 -- Dumping structure for view myanimebot.check_EventExecution
 -- Removing temporary table and create final VIEW structure
 -- Removing temporary table and create final VIEW structure
@@ -711,7 +745,7 @@ CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `check_Index` AS select `in
 -- Dumping structure for view myanimebot.check_OrphanMedias
 -- Dumping structure for view myanimebot.check_OrphanMedias
 -- Removing temporary table and create final VIEW structure
 -- Removing temporary table and create final VIEW structure
 DROP TABLE IF EXISTS `check_OrphanMedias`;
 DROP TABLE IF EXISTS `check_OrphanMedias`;
-CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `check_OrphanMedias` AS select `t_animes`.`id` AS `id`,`t_animes`.`title` AS `media` from `t_animes` where !exists(select distinct `t_feeds`.`title` from `t_feeds` where `t_feeds`.`title` = `t_animes`.`title` limit 1);
+CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `check_OrphanMedias` AS select `t_animes`.`id` AS `id`,`t_animes`.`service` AS `service`,`t_animes`.`title` AS `media` from `t_animes` where !exists(select distinct `t_feeds`.`title` from `t_feeds` where `t_feeds`.`title` = `t_animes`.`title` limit 1);
 
 
 -- Dumping structure for view myanimebot.check_TablesDiskUsage
 -- Dumping structure for view myanimebot.check_TablesDiskUsage
 -- Removing temporary table and create final VIEW structure
 -- Removing temporary table and create final VIEW structure
@@ -721,7 +755,7 @@ CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `check_TablesDiskUsage` AS
 -- Dumping structure for view myanimebot.v_ActiveUsers
 -- Dumping structure for view myanimebot.v_ActiveUsers
 -- Removing temporary table and create final VIEW structure
 -- Removing temporary table and create final VIEW structure
 DROP TABLE IF EXISTS `v_ActiveUsers`;
 DROP TABLE IF EXISTS `v_ActiveUsers`;
-CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `v_ActiveUsers` AS select `t_users`.`mal_user` AS `user`,case when exists(select 1 from `t_feeds` where `t_feeds`.`user` = `t_users`.`mal_user` limit 1) then '1' else '0' end AS `active` from `t_users`;
+CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `v_ActiveUsers` AS select `t_users`.`mal_user` AS `user`,`t_users`.`service` AS `service`,case when exists(select 1 from `t_feeds` where `t_feeds`.`user` = `t_users`.`mal_user` limit 1) then '1' else '0' end AS `active` from `t_users`;
 
 
 -- Dumping structure for view myanimebot.v_CountFeedsType
 -- Dumping structure for view myanimebot.v_CountFeedsType
 -- Removing temporary table and create final VIEW structure
 -- Removing temporary table and create final VIEW structure
@@ -769,5 +803,6 @@ DROP TABLE IF EXISTS `v_TotalFeeds`;
 CREATE ALGORITHM=TEMPTABLE SQL SECURITY INVOKER VIEW `v_TotalFeeds` AS select count(0) AS `total` from `t_feeds`;
 CREATE ALGORITHM=TEMPTABLE SQL SECURITY INVOKER VIEW `v_TotalFeeds` AS select count(0) AS `total` from `t_feeds`;
 
 
 /*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
 /*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
-/*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */;
+/*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */;
 /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
 /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
+/*!40111 SET SQL_NOTES=IFNULL(@OLD_SQL_NOTES, 1) */;

+ 20 - 0
extra/myanimebot.service

@@ -0,0 +1,20 @@
+[Unit]
+Description=MyAnimeList & Anilist Discord Bot
+After=network.target nss-lookup.target
+
+[Service]
+Type=simple
+WorkingDirectory=/opt/MyAnimeBot/
+ExecStart=/usr/local/bin/python3.7 /opt/MyAnimeBot/myanimebot.py
+ExecStop=/bin/kill -2 $MAINPID
+Restart=on-failure
+
+User=mab
+Group=mab
+
+StandardOutput=syslog
+StandardError=syslog
+SyslogIdentifier=mab
+
+[Install]
+WantedBy=multi-user.target

+ 0 - 58
include/utils.py

@@ -1,58 +0,0 @@
-import urllib.request
-import re
-
-from bs4 import BeautifulSoup
-
-# Get thumbnail from an URL
-def getThumbnail(urlParam):
-	url = "/".join((urlParam).split("/")[:5])
-	
-	websource = urllib.request.urlopen(url)
-	soup = BeautifulSoup(websource.read(), "html.parser")
-	image = re.search("(?P<url>https?://[^\s]+)", str(soup.find("img", {"itemprop": "image"}))).group("url")
-	thumbnail = "".join(image.split('"')[:1]).replace('"','')
-	
-	return thumbnail
-
-# Replace multiple substrings from a string
-def replace_all(text, dic):
-	for i, j in dic.items():
-		text = text.replace(i, j)
-	return text
-
-
-# Escape special characters
-def filter_name(name):
-	dic = {
-        "♥": "\♥",
-        "♀": "\♀",
-        "♂": "\♂",
-        "♪": "\♪",
-        "☆": "\☆"
-        }
-
-	return replace_all(name, dic)
-
-# Check if the show's name ends with a show type and truncate it
-def truncate_end_show(show):
-	SHOW_TYPES = (
-        '- TV',
-		'- Movie',
-		'- Special',
-		'- OVA',
-		'- ONA',
-		'- Manga',
-		'- Manhua',
-		'- Manhwa',
-		'- Novel',
-		'- One-Shot',
-		'- Doujinshi',
-		'- Music',
-		'- OEL',
-		'- Unknown'
-    )
-    
-	if show.endswith(SHOW_TYPES):
-		return show[:show.rindex('-') - 1]
-	return show
-

+ 72 - 0
mab-refresh-thumbnail-mal.py

@@ -0,0 +1,72 @@
+#!/usr/bin/env python3
+# Copyright Penta (c) 2018/2021 - Under BSD License
+
+# Library import
+import os
+import re
+import asyncio
+import urllib.request
+import string
+import time
+import socket
+
+# Custom library
+from myanimebot.myanimelist import get_thumbnail
+import myanimebot.globals as globals
+
+# Script version
+VERSION = "1.2"
+
+globals.logger.info("Booting the MyAnimeBot Thumbnail Refresher " + VERSION + "...")
+
+def refresh_thumbnail_mal(startTime) :
+	globals.logger.info("Starting the refresher task...")
+	
+	count = 0
+	
+	cursor = globals.conn.cursor(buffered=True)
+	cursor.execute("SELECT guid, title, thumbnail FROM t_animes WHERE service = %s", [globals.SERVICE_MAL])
+	datas = cursor.fetchall()
+	
+	globals.logger.info(str(len(datas)) + " medias are going to be checked.")
+
+	for data in datas:
+		try:
+			image = get_thumbnail(data[0])
+			
+			if (image == data[2]) :
+				if (image != "") :
+					globals.logger.debug("Thumbnail for " + str(data[1]) + " already up to date.")
+				else :
+					globals.logger.info("Thumbnail for " + str(data[1]) + " still empty.")
+			else :
+				if (image != "") :
+					cursor.execute("UPDATE t_animes SET thumbnail = %s WHERE guid = %s", [image, data[0]])
+					globals.conn.commit()
+					
+					globals.logger.info("Updated thumbnail found for \"" + str(data[1]) + "\": %s", image)
+					count += 1
+				else :
+					try :
+						urllib.request.urlopen(data[2])
+						globals.logger.info("Thumbnail for \"" + str(data[1]) + "\" is now empty, avoiding change.")
+					except Exception as e :
+						globals.logger.info("Thumbnail for \"" + str(data[1]) + "\" has been deleted! (" + str(e) + ")")
+		except Exception as e :
+			globals.logger.warning("Error while updating thumbnail for '" + str(data[1]) + "': " + str(e))
+
+		time.sleep(globals.MYANIMELIST_SECONDS_BETWEEN_REQUESTS)
+	
+	globals.logger.info("All thumbnails checked!")
+	cursor.close()
+	
+	globals.logger.info(str(count) + " new thumbnails, time taken: %ss" % round((time.time() - startTime), 2))
+
+# Starting main function
+if __name__ == "__main__" :
+	refresh_thumbnail_mal(time.time())
+
+	globals.logger.info("Thumbnail refresher script stopped")
+	
+	# We close all the ressources
+	globals.conn.close()

+ 0 - 182
malbot-refresh-thumbnail.py

@@ -1,182 +0,0 @@
-#!/usr/bin/env python3
-# Copyright Penta (c) 2018/2020 - Under BSD License
-
-# Compatible for Python 3.6.X
-#
-# Check and update all the thumbnail for manga/anime in the MyAnimeBot database.
-# Can be pretty long and send a lot of request to MyAnimeList.net,
-# Use it only once in a while to clean the database.
-#
-# Dependencies (for CentOS 7):
-# yum install python3 mariadb-devel gcc python3-devel
-# python3.6 -m pip install --upgrade pip
-# pip3.6 install mysql python-dateutil asyncio html2text bs4 aiodns cchardet configparser
-# pip3.6 install mysql.connector
-
-# Library import
-import logging
-import os
-import re
-import asyncio
-import urllib.request
-import mysql.connector as mariadb
-import string
-import time
-import socket
-
-from html2text import HTML2Text
-from bs4 import BeautifulSoup
-from configparser import ConfigParser
-
-# Custom library
-import utils
-
-class ImproperlyConfigured(Exception): pass
-
-BASE_DIR = os.path.dirname(os.path.abspath(__file__))
-HOME_DIR = os.path.expanduser("~")
-
-DEFAULT_CONFIG_PATHS = [
-	os.path.join("myanimebot.conf"),
-	os.path.join(BASE_DIR, "myanimebot.conf"),
-	os.path.join("/etc/malbot/myanimebot.conf"),
-	os.path.join(HOME_DIR, "myanimebot.conf")
-]
-
-def get_config():
-	config = ConfigParser()
-	config_paths = []
-
-	for path in DEFAULT_CONFIG_PATHS:
-		if os.path.isfile(path):
-			config_paths.append(path)
-			break
-	else: raise ImproperlyConfigured("No configuration file found")
-		
-	config.read(config_paths)
-
-	return config
-
-# Loading configuration
-try:
-	config=get_config()
-except Exception as e:
-	print ("Cannot read configuration: " + str(e))
-	exit (1)
-	
-CONFIG=config["MYANIMEBOT"]
-logLevel=CONFIG.get("logLevel", "INFO")
-dbHost=CONFIG.get("dbHost", "127.0.0.1")
-dbUser=CONFIG.get("dbUser", "myanimebot")
-dbPassword=CONFIG.get("dbPassword")
-dbName=CONFIG.get("dbName", "myanimebot")
-logPath=CONFIG.get("logPath", "myanimebot.log")
-
-# class that send logs to DB
-class LogDBHandler(logging.Handler):
-	'''
-	Customized logging handler that puts logs to the database.
-	pymssql required
-	'''
-	def __init__(self, sql_conn, sql_cursor):
-		logging.Handler.__init__(self)
-		self.sql_cursor = sql_cursor
-		self.sql_conn   = sql_conn
-
-	def emit(self, record):	
-		# Clear the log message so it can be put to db via sql (escape quotes)
-		self.log_msg = str(record.msg.strip().replace('\'', '\'\''))
-		
-		# Make the SQL insert
-		try:
-			self.sql_cursor.execute("INSERT INTO t_logs (host, level, type, log, date, source) VALUES (%s, %s, %s, %s, NOW(), %s)", (str(socket.gethostname()), str(record.levelno), str(record.levelname), self.log_msg, str(record.name)))
-			self.sql_conn.commit()
-		except Exception as e:
-			print ('Error while logging into DB: ' + str(e))
-
-
-# Log configuration
-log_format='%(asctime)-13s : %(name)-15s : %(levelname)-8s : %(message)s'
-logging.basicConfig(handlers=[logging.FileHandler(logPath, 'a', 'utf-8')], format=log_format, level=logLevel)
-
-console = logging.StreamHandler()
-console.setLevel(logging.INFO)
-console.setFormatter(logging.Formatter(log_format))
-
-logger = logging.getLogger("thumbnailer")
-logger.setLevel(logLevel)
-
-logging.getLogger('').addHandler(console)
-
-# Script version
-VERSION = "1.1"
-
-logger.info("Booting the MyAnimeBot Thumbnail Refresher " + VERSION + "...")
-
-# Initialization of the database
-try:
-	conn = mariadb.connect(host=dbHost, user=dbUser, password=dbPassword, database=dbName, buffered=True)
-	
-	# We initialize the logs into the DB.
-	log_conn   = mariadb.connect(host=dbHost, user=dbUser, password=dbPassword, database=dbName, buffered=True)
-	log_cursor = log_conn.cursor()
-	logdb = LogDBHandler(log_conn, log_cursor)
-	logging.getLogger('').addHandler(logdb)
-except Exception as e :
-	logger.critical("Can't connect to the database: " + str(e))
-	
-	httpclient.close()
-	quit()
-
-def main() :
-	logger.info("Starting the refresher task...")
-	
-	count = 0
-	
-	cursor = conn.cursor(buffered=True)
-	cursor.execute("SELECT guid, title, thumbnail FROM t_animes")
-	datas = cursor.fetchall()
-	
-	logger.info(str(len(datas)) + " medias are going to be checked.")
-
-	for data in datas:
-		try:
-			image = utils.getThumbnail(data[0])
-			
-			if (image == data[2]) :
-				if (image != "") :
-					logger.debug("Thumbnail for " + str(data[1]) + " already up to date.")
-				else :
-					logger.info("Thumbnail for " + str(data[1]) + " still empty.")
-			else :
-				if (image != "") :
-					cursor.execute("UPDATE t_animes SET thumbnail = %s WHERE guid = %s", [image, data[0]])
-					conn.commit()
-					
-					logger.info("Updated thumbnail found for \"" + str(data[1]) + "\": %s", image)
-					count += 1
-				else :
-					try :
-						urllib.request.urlopen(data[2])
-						logger.info("Thumbnail for \"" + str(data[1]) + "\" is now empty, avoiding change.")
-					except :
-						logger.info("Thumbnail for \"" + str(data[1]) + "\" has been deleted!")
-		except Exception as e :
-			logger.warning("Error while updating thumbnail for '" + str(data[1]) + "': " + str(e))
-
-		time.sleep(3)
-	
-	logger.info("All thumbnails checked!")
-	cursor.close()
-	
-	logger.info(str(count) + " new thumbnails, time taken: %ss" % round((time.time() - startTime), 2))
-
-# Starting main function
-if __name__ == "__main__" :
-	startTime = time.time()
-	main()
-
-	logger.info("Thumbnail refresher script stopped")
-	
-	# We close all the ressources
-	conn.close()

+ 26 - 8
myanimebot.example.conf

@@ -5,27 +5,45 @@
 logLevel = INFO
 logLevel = INFO
 
 
 # Path of the log file
 # Path of the log file
-logPath = logs/myanimebot.log
+logPath = myanimebot.log
 
 
 # Database configuration
 # Database configuration
-dbHost = localhost
-dbUser = myanimebot
-dbPassword = myPassword
-dbName = myanimebot
+mariadb.host = $DB_HOST
+mariadb.user = $DB_USER
+mariadb.password = $DB_PASSWORD
+mariadb.name = $DB_NAME
+
+# SSL configuration for MariaDB
+mariadb.ssl = false
+mariadb.ssl.ca =
+mariadb.ssl.cert =
+mariadb.ssl.key =
 
 
 # timezone (should be the same as the DB and your Linux system)
 # timezone (should be the same as the DB and your Linux system)
 timezone = Europe/Paris
 timezone = Europe/Paris
 
 
-# New feed since this number of minutes will be displayed (useful when the bot crash on a long period)
+# New feed since this number of minutes will be displayed (useful when the bot crashed for a long period)
 secondMax = 7200
 secondMax = 7200
 
 
+# How much time should the bot need to wait between fetches
+anilist_seconds_between_fetches = 60
+
+# Delay (in seconds) between each requests on MAL website (increase this value in case of timeout)
+myanimelist_seconds_between_requests = 2
+
 # Discord Token
 # Discord Token
-token = 123456789ABCDEF987654321FEDCBA
+token = $DISCORD_TOKEN
 
 
 # Prefix used by the bot
 # Prefix used by the bot
-prefix = !malbot
+prefix = !mab
 
 
 # Bot icons
 # Bot icons
 iconMAL = https://cdn.myanimelist.net/img/sp/icon/apple-touch-icon-256.png
 iconMAL = https://cdn.myanimelist.net/img/sp/icon/apple-touch-icon-256.png
+iconAniList = https://anilist.co/img/icons/android-chrome-512x512.png
 iconBot = http://myanimebot.pentou.eu/rsc/bot_avatar.jpg
 iconBot = http://myanimebot.pentou.eu/rsc/bot_avatar.jpg
 
 
+# Healthcheck web page configuration
+# TO BE USED FOR INTERNAL USE ONLY, NOT PRODUCTION READY
+healthcheck_enabled = false
+healthcheck_port = 15200
+healthcheck_ip = 127.0.0.1

+ 41 - 612
myanimebot.py

@@ -1,641 +1,70 @@
 #!/usr/bin/env python3
 #!/usr/bin/env python3
-# Copyright Penta (c) 2018/2020 - Under BSD License - Based on feed2discord.py by Eric Eisenhart
+# Copyright Penta & lulu (c) 2018/2022 - Under BSD License - Based on feed2discord.py by Eric Eisenhart
 
 
 # Compatible for Python 3.7.X
 # Compatible for Python 3.7.X
-#
-# Dependencies (for CentOS 7):
-# curl -LsS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash
-# 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
 
 
 # Library import
 # Library import
+import asyncio
 import logging
 import logging
-import os
 import sys
 import sys
-import discord
-import feedparser
-import pytz
-import aiohttp
-import asyncio
 import urllib.request
 import urllib.request
-import mariadb
-import string
-import time
-import socket
-
-# Custom libraries
-sys.path.append('include/')
-import utils
-
+import signal
 from configparser import ConfigParser
 from configparser import ConfigParser
 from datetime import datetime
 from datetime import datetime
+from typing import List, Tuple
+
+import aiohttp
+import discord
+import feedparser
+from aiohttp.web_exceptions import HTTPError, HTTPNotModified
 from dateutil.parser import parse as parse_datetime
 from dateutil.parser import parse as parse_datetime
 from html2text import HTML2Text
 from html2text import HTML2Text
-from aiohttp.web_exceptions import HTTPError, HTTPNotModified
+
+# Our modules
+import myanimebot.anilist as anilist
+import myanimebot.globals as globals
+import myanimebot.utils as utils
+import myanimebot.myanimelist as myanimelist
+import myanimebot.commands as commands
+from myanimebot.discord import send_embed_wrapper, build_embed, MyAnimeBot
+
 
 
 if not sys.version_info[:2] >= (3, 7):
 if not sys.version_info[:2] >= (3, 7):
 	print("ERROR: Requires python 3.7 or newer.")
 	print("ERROR: Requires python 3.7 or newer.")
 	exit(1)
 	exit(1)
 
 
-class ImproperlyConfigured(Exception): pass
-
-BASE_DIR = os.path.dirname(os.path.abspath(__file__))
-HOME_DIR = os.path.expanduser("~")
-
-DEFAULT_CONFIG_PATHS = [
-	os.path.join("myanimebot.conf"),
-	os.path.join(BASE_DIR, "myanimebot.conf"),
-	os.path.join("/etc/malbot/myanimebot.conf"),
-	os.path.join(HOME_DIR, "myanimebot.conf")
-]
-
-def get_config():
-	config = ConfigParser()
-	config_paths = []
-
-	for path in DEFAULT_CONFIG_PATHS:
-		if os.path.isfile(path):
-			config_paths.append(path)
-			break
-	else: raise ImproperlyConfigured("No configuration file found")
-		
-	config.read(config_paths)
-
-	return config
-
-
-# Loading configuration
-try:
-	config=get_config()
-except Exception as e:
-	print ("Cannot read configuration: " + str(e))
-	exit (1)
-
-CONFIG=config["MYANIMEBOT"]
-logLevel=CONFIG.get("logLevel", "INFO")
-dbHost=CONFIG.get("dbHost", "127.0.0.1")
-dbUser=CONFIG.get("dbUser", "myanimebot")
-dbPassword=CONFIG.get("dbPassword")
-dbName=CONFIG.get("dbName", "myanimebot")
-logPath=CONFIG.get("logPath", "myanimebot.log")
-timezone=pytz.timezone(CONFIG.get("timezone", "utc"))
-secondMax=CONFIG.getint("secondMax", 7200)
-token=CONFIG.get("token")
-prefix=CONFIG.get("prefix", "!malbot")
-iconMAL=CONFIG.get("iconMAL", "https://cdn.myanimelist.net/img/sp/icon/apple-touch-icon-256.png")
-iconBot=CONFIG.get("iconBot", "http://myanimebot.pentou.eu/rsc/bot_avatar.jpg")
-
-# class that send logs to DB
-class LogDBHandler(logging.Handler):
-	def __init__(self, sql_conn, sql_cursor):
-		logging.Handler.__init__(self)
-		self.sql_cursor = sql_cursor
-		self.sql_conn   = sql_conn
-
-	def emit(self, record):	
-		# Clear the log message so it can be put to db via sql (escape quotes)
-		self.log_msg = str(record.msg.strip().replace('\'', '\'\''))
-		
-		# Make the SQL insert
-		try:
-			self.sql_cursor.execute("INSERT INTO t_logs (host, level, type, log, date, source) VALUES (%s, %s, %s, %s, NOW(), %s)", (str(socket.gethostname()), str(record.levelno), str(record.levelname), self.log_msg, str(record.name)))
-			self.sql_conn.commit()
-		except Exception as e:
-			print ('Error while logging into DB: ' + str(e))
-
-# Log configuration
-log_format='%(asctime)-13s : %(name)-15s : %(levelname)-8s : %(message)s'
-logging.basicConfig(handlers=[logging.FileHandler(logPath, 'a', 'utf-8')], format=log_format, level=logLevel)
-
-console = logging.StreamHandler()
-console.setLevel(logging.INFO)
-console.setFormatter(logging.Formatter(log_format))
-
-logger = logging.getLogger("myanimebot")
-logger.setLevel(logLevel)
-
-logging.getLogger('').addHandler(console)
-
-# Script version
-VERSION = "0.9.6.2"
-
-# The help message
-HELP = 	"""**Here's some help for you:**
-```
-- here :
-Type this command on the channel where you want to see the activity of the MAL profiles.
-
-- stop :
-Cancel the here command, no message will be displayed.
-
-- add :
-Followed by a username, add a MAL user into the database to be displayed on this server.
-ex: !malbot add MyUser
-
-- delete :
-Followed by a username, remove a user from the database.
-ex: !malbot delete MyUser
-
-- group :
-Specify a group that can use the add and delete commands.
-
-- info :
-Get the users already in the database for this server.
-
-- about :
-Get some information about this bot.
-```"""
-
-logger.info("Booting MyAnimeBot " + VERSION + "...")
-logger.debug("DEBUG log: OK")
-
-feedparser.PREFERRED_XML_PARSERS.remove("drv_libxml2")
 
 
-# Initialization of the database
-try:
-	# Main database connection
-	conn = mariadb.connect(host=dbHost, user=dbUser, password=dbPassword, database=dbName)
+def exit_app(signum=None, frame=None):
+	globals.logger.debug("Received signal {}".format(signum))
+	globals.logger.info("Closing all tasks...")
 	
 	
-	# We initialize the logs into the DB.
-	log_conn   = mariadb.connect(host=dbHost, user=dbUser, password=dbPassword, database=dbName)
-	log_cursor = log_conn.cursor()
-	logdb = LogDBHandler(log_conn, log_cursor)
-	logging.getLogger('').addHandler(logdb)
-	
-	logger.info("The database logger is running.")
-except Exception as e:
-	logger.critical("Can't connect to the database: " + str(e))
-	quit()
-
-
-# Initialization of the Discord client
-client = discord.Client()
-
-task_feed       = None
-task_gameplayed = None
-task_thumbnail  = None
-
-# Function used to make the embed message related to the animes status
-def build_embed(user, item, channel, pubDate, image):
-	try:	
-		embed = discord.Embed(colour=0xEED000, url=item.link, description="[" + utils.filter_name(item.title) + "](" + item.link + ")\n```" + item.description + "```", timestamp=pubDate.astimezone(pytz.timezone("utc")))
-		embed.set_thumbnail(url=image)
-		embed.set_author(name=user + "'s MyAnimeList", url="https://myanimelist.net/profile/" + user, icon_url=iconMAL)
-		embed.set_footer(text="MyAnimeBot", icon_url=iconBot)
-		
-		return embed
-	except Exception as e:
-		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)
-		logger.info("Message sent in channel: " + channelid)
-	except Exception as e:
-		logger.debug("Impossible to send a message on '" + channelid + "': " + str(e)) 
-		return
-	
-# Main function that check the RSS feeds from MyAnimeList
-async def background_check_feed(asyncioloop):
-	logger.info("Starting up background_check_feed")
-	
-	# We configure the http header
-	http_headers = { "User-Agent": "MyAnimeBot Discord Bot v" + VERSION, }
-	
-	await client.wait_until_ready()
-	
-	logger.debug("Discord client connected, unlocking background_check_feed...")
-	
-	while not client.is_closed():
-		try:
-			db_user = conn.cursor(buffered=True)
-			db_user.execute("SELECT mal_user, servers FROM t_users")
-			data_user = db_user.fetchone()
-		except Exception as e:
-			logger.critical("Database unavailable! (" + str(e) + ")")
-			quit()
-
-		while data_user is not None:
-			user=data_user[0]
-			stop_boucle = 0
-			feed_type = 1
-			
-			logger.debug("checking user: " + user)
-			
-			try:
-				while stop_boucle == 0 :
-					try:
-						async with aiohttp.ClientSession() as httpclient:
-							if feed_type == 1 :
-								http_response = await httpclient.request("GET", "https://myanimelist.net/rss.php?type=rm&u=" + user, headers=http_headers)
-								media = "manga"
-							else : 
-								http_response = await httpclient.request("GET", "https://myanimelist.net/rss.php?type=rw&u=" + user, headers=http_headers)
-								media = "anime"
-					except Exception as e:
-						logger.error("Error while loading RSS (" + str(feed_type) + ") of '" + user + "': " + str(e))
-						break
-
-					http_data = await http_response.read()
-					feed_data = feedparser.parse(http_data)
-					
-					for item in feed_data.entries:
-						pubDateRaw = datetime.strptime(item.published, '%a, %d %b %Y %H:%M:%S %z').astimezone(timezone)
-						DateTimezone = pubDateRaw.strftime("%z")[:3] + ':' + pubDateRaw.strftime("%z")[3:]
-						pubDate = pubDateRaw.strftime("%Y-%m-%d %H:%M:%S")
-						
-						cursor = conn.cursor(buffered=True)
-						cursor.execute("SELECT published, title, url FROM t_feeds WHERE published=%s AND title=%s AND user=%s", [pubDate, item.title, user])
-						data = cursor.fetchone()
-
-						if data is None:
-							var = datetime.now(timezone) - pubDateRaw
-							
-							logger.debug(" - " + item.title + ": " + str(var.total_seconds()))
-						
-							if var.total_seconds() < secondMax:
-								logger.info(user + ": Item '" + item.title + "' not seen, processing...")
-								
-								if item.description.startswith('-') :
-									if feed_type == 1 :	item.description = "Re-Reading " + item.description
-									else :				item.description = "Re-Watching " + item.description
-								
-								cursor.execute("SELECT thumbnail FROM t_animes WHERE guid=%s LIMIT 1", [item.guid])
-								data_img = cursor.fetchone()
-								
-								if data_img is None:
-									try:
-										image = utils.getThumbnail(item.link)
-										
-										logger.info("First time seeing this " + media + ", adding thumbnail into database: " + image)
-									except Exception as e:
-										logger.warning("Error while getting the thumbnail: " + str(e))
-										image = ""
-										
-									cursor.execute("INSERT INTO t_animes (guid, title, thumbnail, found, discoverer, media) VALUES (%s, %s, %s, NOW(), %s, %s)", [item.guid, item.title, image, user, media])
-									conn.commit()
-								else: image = data_img[0]
-
-								type = item.description.partition(" - ")[0]
-								
-								cursor.execute("INSERT INTO t_feeds (published, title, url, user, found, type) VALUES (%s, %s, %s, %s, NOW(), %s)", (pubDate, item.title, item.guid, user, type))
-								conn.commit()
-								
-								for server in data_user[1].split(","):
-									db_srv = conn.cursor(buffered=True)
-									db_srv.execute("SELECT channel FROM t_servers WHERE server = %s", [server])
-									data_channel = db_srv.fetchone()
-									
-									while data_channel is not None:
-										for channel in data_channel: await send_embed_wrapper(asyncioloop, channel, client, build_embed(user, item, channel, pubDateRaw, image))
-										
-										data_channel = db_srv.fetchone()
-					if feed_type == 1:
-						feed_type = 0
-						await asyncio.sleep(1)
-					else:
-						stop_boucle = 1
-					
-			except Exception as e:
-				logger.error("Error when parsing RSS for '" + user + "': " + str(e))
-			
-			await asyncio.sleep(1)
+	if globals.MAL_ENABLED:
+		globals.task_feed.cancel()
 
 
-			data_user = db_user.fetchone()
+	if globals.ANI_ENABLED:
+		globals.task_feed_anilist.cancel()
 
 
-@client.event
-async def on_ready():
-	logger.info("Logged in as " + client.user.name + " (" + str(client.user.id) + ")")
+	globals.task_thumbnail.cancel()
+	globals.task_gameplayed.cancel()
 
 
-	logger.info("Starting all tasks...")
-
-	task_feed = client.loop.create_task(background_check_feed(client.loop))
-	task_thumbnail = client.loop.create_task(update_thumbnail_catalog(client.loop))
-	task_gameplayed = client.loop.create_task(change_gameplayed(client.loop))
-
-
-@client.event
-async def on_error(event, *args, **kwargs):
-    logger.exception("Crap! An unknown Discord error occured...")
-
-@client.event
-async def on_message(message):
-	if message.author == client.user: return
-
-	words = message.content.split(" ")
-	author = str('{0.author.mention}'.format(message))
-
-	# A user is trying to get help
-	if words[0] == prefix:
-		if len(words) > 1:
-			if words[1] == "ping": await message.channel.send("pong")
-			
-			elif words[1] == "here":
-				if message.author.guild_permissions.administrator:
-					cursor = conn.cursor(buffered=True)
-					cursor.execute("SELECT server, channel FROM t_servers WHERE server=%s", [str(message.guild.id)])
-					data = cursor.fetchone()
-					
-					if data is None:
-						cursor.execute("INSERT INTO t_servers (server, channel) VALUES (%s,%s)", [str(message.guild.id), str(message.channel.id)])
-						conn.commit()
-						
-						await message.channel.send("Channel **" + str(message.channel) + "** configured for **" + str(message.guild) + "**.")
-					else:
-						if(data[1] == str(message.channel.id)): await message.channel.send("Channel **" + str(message.channel) + "** already in use for this server.")
-						else:
-							cursor.execute("UPDATE t_servers SET channel = %s WHERE server = %s", [str(message.channel.id), str(message.guild.id)])
-							conn.commit()
-							
-							await message.channel.send("Channel updated to: **" + str(message.channel) + "**.")
-							
-					cursor.close()
-				else: await message.channel.send("Only server's admins can use this command!")
-				
-			elif words[1] == "add":
-				if len(words) > 2:
-					if (len(words) == 3):
-						user = words[2]
-						
-						if(len(user) < 15):
-							try:
-								urllib.request.urlopen('https://myanimelist.net/profile/' + user)
-								
-								cursor = conn.cursor(buffered=True)
-								cursor.execute("SELECT servers FROM t_users WHERE LOWER(mal_user)=%s", [user.lower()])
-								data = cursor.fetchone()
-
-								if data is None:
-									cursor.execute("INSERT INTO t_users (mal_user, servers) VALUES (%s, %s)", [user, str(message.guild.id)])
-									conn.commit()
-									
-									await message.channel.send("**" + user + "** added to the database for the server **" + str(message.guild) + "**.")
-								else:
-									var = 0
-									
-									for server in data[0].split(","):
-										if (server == str(message.guild.id)): var = 1
-									
-									if (var == 1):
-										await message.channel.send("User **" + user + "** already in our database for this server!")
-									else:
-										cursor.execute("UPDATE t_users SET servers = %s WHERE LOWER(mal_user) = %s", [data[0] + "," + str(message.guild.id), user.lower()])
-										conn.commit()
-										
-										await message.channel.send("**" + user + "** added to the database for the server **" + str(message.guild) + "**.")
-										
-								cursor.close()
-							except urllib.error.HTTPError as e:
-								if (e.code == 404): await message.channel.send("User **" + user + "** doesn't exist on MyAnimeList!")
-								else:
-									await message.channel.send("An error occured when we checked this username on MyAnimeList, maybe the website is down?")
-									logger.warning("HTTP Code " + str(e.code) + " while checking to add for the new user '" + user + "'")
-							except Exception as e:
-								await message.channel.send("An unknown error occured while addind this user, the error has been logged.")
-								logger.warning("Error while adding user '" + user + "' on server '" + message.guild + "': " + str(e))
-						else: await message.channel.send("Username too long!")
-					else: await message.channel.send("Too many arguments! You have to specify only one username.")
-				else: await message.channel.send("You have to specify a **MyAnimeList** username!")
-				
-			elif words[1] == "delete":
-				if len(words) > 2:
-					if (len(words) == 3):
-						user = words[2]
-						
-						cursor = conn.cursor(buffered=True)
-						cursor.execute("SELECT servers FROM t_users WHERE LOWER(mal_user)=%s", [user.lower()])
-						data = cursor.fetchone()
-						
-						if data is not None:
-							srv_string = ""
-							present = 0
-							
-							for server in data[0].split(","):
-								if server != str(message.guild.id):
-									if srv_string == "": srv_string = server
-									else: srv_string += "," + server
-								else: present = 1
-							
-							if present == 1:
-								if srv_string == "": cursor.execute("DELETE FROM t_users WHERE LOWER(mal_user) = %s", [user.lower()])
-								else: cursor.execute("UPDATE t_users SET servers = %s WHERE LOWER(mal_user) = %s", [srv_string, user.lower()])
-								conn.commit()
-								
-								await message.channel.send("**" + user + "** deleted from the database for this server.")
-							else: await message.channel.send("The user **" + user + "** is not in our database for this server!")
-						else: await message.channel.send("The user **" + user + "** is not in our database for this server!")
-			
-						cursor.close()
-					else: await message.channel.send("Too many arguments! You have to specify only one username.")
-				else: await message.channel.send("You have to specify a **MyAnimeList** username!")
-				
-			elif words[1] == "stop":
-				if message.author.guild_permissions.administrator:
-					if (len(words) == 2):
-						cursor = conn.cursor(buffered=True)
-						cursor.execute("SELECT server FROM t_servers WHERE server=%s", [str(message.guild.id)])
-						data = cursor.fetchone()
-					
-						if data is None: await client.send_message(message.channel, "The server **" + str(message.guild) + "** is not in our database.")
-						else:
-							cursor.execute("DELETE FROM t_servers WHERE server = %s", [message.guild.id])
-							conn.commit()
-							await message.channel.send("Server **" + str(message.guild) + "** deleted from our database.")
-						
-						cursor.close()
-					else: await message.channel.send("Too many arguments! Only type *stop* if you want to stop this bot on **" + message.guild + "**")
-				else: await message.channel.send("Only server's admins can use this command!")
-				
-			elif words[1] == "info":
-				cursor = conn.cursor(buffered=True)
-				cursor.execute("SELECT server FROM t_servers WHERE server=%s", [str(message.guild.id)])
-				data = cursor.fetchone()
-				
-				if data is None: await message.channel.send("The server **" + str(message.guild) + "** is not in our database.")
-				else:
-					user = ""
-					cursor = conn.cursor(buffered=True)
-					cursor.execute("SELECT mal_user, servers FROM t_users")
-					data = cursor.fetchone()
-					
-					cursor_channel = conn.cursor(buffered=True)
-					cursor_channel.execute("SELECT channel FROM t_servers WHERE server=%s", [str(message.guild.id)])
-					data_channel = cursor_channel.fetchone()
-					
-					if data_channel is None: await message.channel.send("No channel assigned for this bot in this server.")
-					else:
-						while data is not None:
-							if (str(message.guild.id) in data[1].split(",")):
-								if (user == ""): user = data[0]
-								else: user += ", " + data[0]
-						
-							data = cursor.fetchone()
-						
-						if (user == ""): await message.channel.send("No user in this server.")
-						else: await message.channel.send("Here's the user(s) in the **" + str(message.guild) + "**'s server:\n```" + user + "```\nAssigned channel: **" + str(client.get_channel(int(data_channel[0]))) + "**")
-
-					cursor.close()
-					cursor_channel.close()
-			elif words[1] == "about": await message.channel.send(embed=discord.Embed(colour=0x777777, title="MyAnimeBot version " + VERSION + " by Penta", description="This bot check the MyAnimeList's RSS for each user specified, and send a message if there is something new.\nMore help with the **!malbot help** command.\n\nAdd me on steam: http://steamcommunity.com/id/Penta_Pingouin").set_thumbnail(url="https://cdn.discordapp.com/avatars/415474467033317376/2d847944aab2104923c18863a41647da.jpg?size=64"))
-			
-			elif words[1] == "help": await message.channel.send(HELP)
-			
-			elif words[1] == "top":
-				if len(words) == 2:
-					try:
-						cursor = conn.cursor(buffered=True)
-						cursor.execute("SELECT * FROM v_Top")
-						data = cursor.fetchone()
-						
-						if data is None: await message.channel.send("It seems that there is no statistics... (what happened?!)")
-						else:
-							topText = "**__Here is the global statistics of this bot:__**\n\n"
-							
-							while data is not None:
-								topText += " - " + str(data[0]) + ": " + str(data[1]) + "\n"
-									
-								data = cursor.fetchone()
-								
-							cursor = conn.cursor(buffered=True)
-							cursor.execute("SELECT * FROM v_TotalFeeds")
-							data = cursor.fetchone()
-							
-							topText += "\n***Total user entry***: " + str(data[0])
-							
-							cursor = conn.cursor(buffered=True)
-							cursor.execute("SELECT * FROM v_TotalAnimes")
-							data = cursor.fetchone()
-							
-							topText += "\n***Total unique manga/anime***: " + str(data[0])
-							
-							await message.channel.send(topText)
-						
-						cursor.close()
-					except Exception as e:
-						logger.warning("An error occured while displaying the global top: " + str(e))
-						await message.channel.send("Unable to reply to your request at the moment...")
-				elif len(words) > 2:
-					keyword = str(' '.join(words[2:]))
-					logger.info("Displaying the global top for the keyword: " + keyword)
-					
-					try:
-						cursor = conn.cursor(buffered=True)
-						cursor.callproc('sp_UsersPerKeyword', [str(keyword), '20'])
-						for result in cursor.stored_results():
-							data = result.fetchone()
-							
-							if data is None: await message.channel.send("It seems that there is no statistics for the keyword **" + keyword + "**.")
-							else:
-								topKeyText = "**__Here is the statistics for the keyword " + keyword + ":__**\n\n"
-								
-								while data is not None:
-									topKeyText += " - " + str(data[0]) + ": " + str(data[1]) + "\n"
-										
-									data = result.fetchone()
-									
-								await message.channel.send(topKeyText)
-							
-						cursor.close()
-					except Exception as e:
-						logger.warning("An error occured while displaying the global top for keyword '" + keyword + "': " + str(e))
-						await message.channel.send("Unable to reply to your request at the moment...")
-			
-			elif words[1] == "group":
-				if len(words) > 2:
-					if message.author.guild_permissions.administrator:
-						group = words[2]
-						await message.channel.send("admin OK")
-					else: await message.channel.send("Only server's admins can use this command!")
-				else:
-					await message.channel.send("You have to specify a group!")
-		
-	# If mentioned
-	elif client.user in message.mentions:
-		await message.channel.send(":heart:")
-
-# Get a random anime name and change the bot's activity
-async def change_gameplayed(asyncioloop):
-	logger.info("Starting up change_gameplayed")
+	# Closing all ressources
+	globals.conn.close()
 	
 	
-	await client.wait_until_ready()
-	await asyncio.sleep(1)
+	globals.logger.critical("Script halted.")
 
 
-	while not client.is_closed():
-		# Get a random anime name from the users' list
-		cursor = conn.cursor(buffered=True)
-		cursor.execute("SELECT title FROM t_animes ORDER BY RAND() LIMIT 1")
-		data = cursor.fetchone()
-		anime = utils.truncate_end_show(data[0])
-		
-		# Try to change the bot's activity
-		try:
-			if data is not None: await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=anime))
-		except Exception as e:
-			logger.warning("An error occured while changing the displayed anime title: " + str(e))
-			
-		cursor.close()
-		# Do it every minute
-		await asyncio.sleep(60)
 
 
-async def update_thumbnail_catalog(asyncioloop):
-	logger.info("Starting up update_thumbnail_catalog")
-	
-	while not client.is_closed():
-		await asyncio.sleep(43200)
-		
-		logger.info("Automatic check of the thumbnail database on going...")
-		reload = 0
-		
-		cursor = conn.cursor(buffered=True)
-		cursor.execute("SELECT guid, title, thumbnail FROM t_animes")
-		data = cursor.fetchone()
-
-		while data is not None:
-			try:
-				if (data[2] != "") : urllib.request.urlopen(data[2])
-				else: reload = 1
-			except urllib.error.HTTPError as e:
-				logger.warning("HTTP Error while getting the current thumbnail of '" + str(data[1]) + "': " + str(e))
-				reload = 1
-			except Exception as e:
-				logger.debug("Error while getting the current thumbnail of '" + str(data[1]) + "': " + str(e))
-			
-			if (reload == 1) :
-				try:
-					image = utils.getThumbnail(data[0])
-						
-					cursor.execute("UPDATE t_animes SET thumbnail = %s WHERE guid = %s", [image, data[0]])
-					conn.commit()
-						
-					logger.info("Updated thumbnail found for \"" + str(data[1]) + "\": %s", image)
-				except Exception as e:
-					logger.warning("Error while downloading updated thumbnail for '" + str(data[1]) + "': " + str(e))
-
-			await asyncio.sleep(3)
-			data = cursor.fetchone()
-
-		cursor.close()
-
-		logger.info("Thumbnail database checked.")
-	
 # Starting main function	
 # Starting main function	
 if __name__ == "__main__":
 if __name__ == "__main__":
-    try:
-        client.run(token)
-    except:
-        logging.info("Closing all tasks...")
-        task_feed.cancel()
-        task_thumbnail.cancel()
-        task_gameplayed.cancel()
+	
+	# Catch SIGINT signal (Ctrl-C)
+	signal.signal(signal.SIGINT, exit_app)
+	
+	# Run the app
+	try:
+		globals.client = MyAnimeBot()
+		globals.client.run(globals.token)
+	except Exception as e:
+		globals.logger.error("Encountered exception while running the bot: {}".format(e))
 
 
-    logger.critical("Script halted.")
+	exit_app()
 
 
-	# We close all the ressources
-    conn.close()
-    log_cursor.close()
-    log_conn.close()

+ 6 - 0
myanimebot/__init__.py

@@ -0,0 +1,6 @@
+from .utils import * 
+from .anilist import *
+from .globals import *
+from .myanimelist import *
+from .discord import *
+from .commands import *

+ 435 - 0
myanimebot/anilist.py

@@ -0,0 +1,435 @@
+import asyncio
+import datetime
+import time
+from enum import Enum
+from typing import Dict, List
+from discord import activity
+
+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'
+    
+
+def get_media_name(activity):
+    ''' Returns the media name in english if possible '''
+
+    english_name = activity["media"]["title"]["english"]
+    if english_name is not None:
+        return english_name
+
+    romaji_name = activity["media"]["title"]["romaji"]
+    if romaji_name is not None:
+        return romaji_name
+
+    native_name = activity["media"]["title"]["native"]
+    if native_name is not None:
+        return native_name
+
+    return ''
+
+
+def get_progress(feed : utils.Feed, activity : dict):
+    ''' Tries to get progress from activity '''
+
+    progress = activity["progress"]
+    if progress is None:
+        if feed.status == utils.MediaStatus.COMPLETED:
+            return feed.media.episodes
+        elif feed.status == utils.MediaStatus.PLANNING:
+            return '0'
+        else:
+            return '?'
+    return progress
+
+def get_number_episodes(activity, media_type : utils.MediaType):
+    episodes = '?'
+    if media_type == utils.MediaType.ANIME:
+        episodes = activity["media"]["episodes"]
+    elif media_type == utils.MediaType.MANGA:
+        episodes = activity["media"]["chapters"]
+    else:
+        raise NotImplementedError('Error: Unknown media type "{}"'.format(media_type))
+    if episodes is None:
+        episodes = '?'
+    return episodes
+
+
+def build_feed_from_activity(activity, user : utils.User) -> utils.Feed:
+    if activity is None: return None
+
+    media_type = utils.MediaType.from_str(activity["type"])
+
+    media = utils.Media(id=activity["media"]["id"],
+                        name=get_media_name(activity),
+                        url=activity["media"]["siteUrl"],
+                        episodes=get_number_episodes(activity, media_type),
+                        image=activity["media"]["coverImage"]["large"],
+                        type=media_type)
+    feed = utils.Feed(service=utils.Service.ANILIST,
+                        date_publication=datetime.datetime.fromtimestamp(activity["createdAt"], globals.timezone),
+                        user=user,
+                        status=utils.MediaStatus.from_str(activity["status"]),
+                        description=None,
+                        media=media,
+                        progress=None,
+                        score=None,
+                        score_format=None)
+    feed.progress = get_progress(feed, activity)
+    return feed
+ 
+
+def get_user_score_from_media(media_id : int, user_id : int) -> int:
+    """ Look for the user's score on a specific media """
+    
+    query = '''query($userId: Int, $mediaId: Int){
+        MediaList(userId: $userId, mediaId: $mediaId) {
+            score
+            user {
+                mediaListOptions {
+                    scoreFormat
+                }
+            }
+        }
+    }'''
+    
+    variables = {
+        'userId': user_id,
+        'mediaId': media_id,
+    }
+
+    try:
+        response = requests.post(ANILIST_GRAPHQL_URL, json={'query': query, 'variables': variables})
+        response.raise_for_status()
+        media_list = response.json()["data"]["MediaList"]
+        score_fmt = media_list["user"]["mediaListOptions"]["scoreFormat"]
+        score = media_list["score"]
+        if media_list is None or score == 0: # Not scored
+            return None, score_fmt
+        else:
+            return score, score_fmt
+    except requests.HTTPError as e:
+        globals.logging.error('''HTTP Error while getting the user's score (ID="{}") for media "{}". Error: {}'''.format(user_id, media_id, e))
+    except Exception as e:
+        globals.logging.error('''Unknown error while getting the user's score (ID="{}") for media "{}". Error: {}'''.format(user_id, media_id, e))
+    return None, None
+
+
+def get_anilist_userId_from_name(user_name : str) -> int:
+    """ Searches an AniList user by its name and returns its ID """
+
+    query = '''query($userName: String){
+        User(name: $userName) {
+            id
+        }
+    }'''
+
+    variables = {
+        'userName': user_name
+    }
+
+    try:
+        response = requests.post(ANILIST_GRAPHQL_URL, json={'query': query, 'variables': variables})
+        response.raise_for_status()
+        return response.json()["data"]["User"]["id"]
+    except requests.HTTPError as e:
+        globals.logging.error('HTTP Error while getting the AniList user ID for "{}". Error: {}'.format(user_name, e))
+    except Exception as e:
+        globals.logging.error('Unknown error while getting the AniList user ID for "{}". Error: {}'.format(user_name, e))
+    return None
+
+
+def get_latest_users_activities(users : List[utils.User], page: int, perPage = 5) -> List[utils.Feed]:
+    """ Get latest users' activities """
+
+    query = '''query ($userIds: [Int], $page: Int, $perPage: Int) {
+        Page (page: $page, perPage: $perPage) {
+            activities (userId_in: $userIds, sort: ID_DESC) {
+                __typename
+                ... on ListActivity {
+                    id
+                    type
+                    status
+                    progress
+                    isLocked
+                    createdAt
+                    
+                    user {
+                        id
+                        name
+                    }
+
+                    media {
+                        id
+                        siteUrl
+                        episodes
+                        chapters
+                        type
+                        title {
+                            romaji
+                            english
+                            native
+                        }
+                        coverImage {
+                            large
+                        }
+                    }
+                } 
+            } 
+        }
+    }'''
+
+    variables = {
+        "userIds": [user.service_id for user in users],
+        "perPage": perPage,
+        "page": page
+    }
+
+    try:
+        # Execute GraphQL query
+        response = requests.post(ANILIST_GRAPHQL_URL, json={'query': query, 'variables': variables})
+        response.raise_for_status()
+        data = response.json()["data"]["Page"]["activities"]
+
+        # Create feeds from data
+        feeds = []
+        for activity in data:
+            # Check if activity is a ListActivity
+            if activity["__typename"] != 'ListActivity':
+                continue
+            
+            # Find corresponding user for this ListActivity
+            user = next((user for user in users if user.name == activity["user"]["name"]), None)
+            if user is None:
+                raise RuntimeError('Cannot find {} in our registered users'.format(activity["user"]["name"]))
+
+            # Add new builded feed
+            feeds.append(build_feed_from_activity(activity, user))
+        return feeds
+
+    except requests.HTTPError as e:
+        globals.logging.error('HTTP 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:
+        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 None
+
+
+def check_username_validity(username) -> bool:
+    """ Check if the AniList username exists """
+
+    query = '''query($name: String) {
+        User(name: $name) {
+            name
+        }
+    }'''
+
+    variables = {
+        'name': username
+    }
+
+    try:
+        response = requests.post(ANILIST_GRAPHQL_URL, json={'query': query, 'variables': variables})
+        response.raise_for_status()
+        return response.json()["data"]["User"]["name"] == username
+    except requests.HTTPError as e:
+        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:
+        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]):
+    """ Get the latest users' activity """
+
+    # TODO Will fail if last activity is not a ListActivity
+    query = '''query ($userIds: [Int]) {
+        Activity(userId_in: $userIds, sort: ID_DESC) {
+            __typename
+            ... on ListActivity {
+                id
+                userId
+                createdAt
+            } 
+        }
+    }'''
+
+    variables = {
+        "userIds": [user.service_id for user in users]
+    }
+
+    try:
+        response = requests.post(ANILIST_GRAPHQL_URL, json={'query': query, 'variables': variables})
+        response.raise_for_status()
+        return response.json()["data"]["Activity"]
+    except requests.HTTPError as e:
+        globals.logging.error('HTTP Error while getting the latest AniList activity : {}'.format(e))
+    except Exception as e:
+        globals.logging.error('Unknown error while getting the latest AniList activity : {}'.format(e))
+    return None
+
+
+def get_users_db():
+    ''' Returns the registered users using AniList '''
+
+	# TODO Make generic execute
+    cursor = globals.conn.cursor(buffered=True, dictionary=True)
+    cursor.execute("SELECT id, {}, servers FROM t_users WHERE service = %s".format(globals.DB_USER_NAME), [globals.SERVICE_ANILIST])
+    users_data = cursor.fetchall()
+    cursor.close()
+    return users_data
+
+
+def get_users() -> List[utils.User]:
+    users = []
+    users_data = get_users_db()
+    if users_data is not None:
+        for user_data in users_data:
+            users.append(utils.User(id=user_data["id"],
+                            service_id=get_anilist_userId_from_name(user_data[globals.DB_USER_NAME]),
+                            name=user_data[globals.DB_USER_NAME],
+                            servers=user_data["servers"].split(',')))
+    return users
+
+
+def get_users_id(users_data) -> List[int]:
+    ''' Returns the id of the registered users using AniList '''
+
+    users_ids = []
+
+    # Get users using AniList
+    if users_data is not None:
+        for user_data in users_data:
+            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
+
+    return users_ids
+
+
+async def send_embed_to_channels(activity : utils.Feed):
+    ''' Send an embed message describing the activity to user's channel '''
+
+    for server in activity.user.servers:
+        data_channels = utils.get_channels(server)
+    
+        if data_channels is not None:
+            for channel in data_channels:
+                await send_embed_wrapper(None,
+                                            channel["channel"],
+                                            globals.client,
+                                            build_embed(activity))
+
+
+def insert_feed_db(feed: utils.Feed):
+    ''' Insert an AniList feed into database '''
+
+    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)",
+                    (feed.date_publication.timestamp(),
+                     feed.media.name,
+                     feed.media.url,
+                     feed.user.name,
+                     feed.get_status_str(),
+                     globals.SERVICE_ANILIST))
+    globals.conn.commit()
+
+
+async def process_new_activities(last_activity_date, users : List[utils.User]):
+    """ Fetch and process all newest activities """
+    
+    continue_fetching = True
+    page_number = 1
+    while continue_fetching:
+        # Get activities
+        activities = get_latest_users_activities(users, page_number)
+
+        if activities == None: # An error occured, break the loop
+            return
+
+        # Processing them
+        for activity in activities:
+            # Get time difference between now and activity creation date
+            diffTime = datetime.datetime.now(globals.timezone) - activity.date_publication
+
+            # 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
+            if activity.date_publication.timestamp() <= last_activity_date \
+                 or diffTime.total_seconds() > globals.secondMax:
+                # FIXME If two or more feeds are published at the same time, this would skip them
+                continue_fetching = False
+                break
+
+            # Process activity
+            globals.logger.info('Adding new feed for "{}({})" about "{}"'.format(activity.user.name, activity.service.name, activity.media.name))
+            insert_feed_db(activity)
+            
+            if activity.status == utils.MediaStatus.COMPLETED:
+                activity.score, activity.score_format = get_user_score_from_media(activity.media.id, activity.user.service_id)
+
+            await send_embed_to_channels(activity)
+
+        # Load next activities page
+        # TODO How can I avoid duplicate if insertion in between? With storing ids?
+        if continue_fetching:
+            page_number += 1
+            time.sleep(1)
+
+
+def get_last_activity_date_db() -> float:
+    # Refresh database
+    globals.conn.commit()
+
+    # Get last activity date
+    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])
+    data = cursor.fetchone()
+
+    if data is None or len(data) == 0:
+        return 0.0
+    else:
+        return data["published"].timestamp()
+
+
+async def check_new_activities():
+    """ Check if there is new activities and process them """
+    
+    last_activity_date = get_last_activity_date_db()
+
+    # Get latest activity on AniList
+    users = get_users()
+    latest_activity = get_latest_activity(users)
+    if latest_activity is not None:
+
+        # If the latest activity is more recent than the last we stored
+        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"]:
+            globals.logger.debug("Found a more recent AniList feed")
+            await process_new_activities(last_activity_date, users)
+
+
+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

+ 414 - 0
myanimebot/commands.py

@@ -0,0 +1,414 @@
+import discord
+import urllib
+import datetime
+
+from typing import List, Tuple
+
+import myanimebot.utils as utils
+import myanimebot.globals as globals
+import myanimebot.anilist as anilist
+
+
+def build_info_cmd_message(users, server, channels, role, filters : List[utils.Service]) -> str:
+    ''' Build the corresponding message for the info command '''
+
+    registered_channel = globals.client.get_channel(int(channels[0]["channel"]))
+
+    # Store users
+    mal_users = []
+    anilist_users = []
+    for user in users:
+        # If user is part of the server, add it to the message
+        if str(server.id) in user['servers'].split(','):
+            try:
+                user_service = utils.Service.from_str(user["service"])
+                if user_service == utils.Service.MAL:
+                    mal_users.append(user[globals.DB_USER_NAME])
+                elif user_service == utils.Service.ANILIST:
+                    anilist_users.append(user[globals.DB_USER_NAME])
+            except NotImplementedError:
+                pass # Nothing to do here
+
+    if not mal_users and not anilist_users:
+        return "No users registered on this server. Try to add one."
+    else:
+        message =  'Registered user(s) on **{}**\n\n'.format(server)
+        if mal_users: # If not empty
+            # Don't print if there is filters and MAL is not in them
+            if not filters or (filters and utils.Service.MAL in filters): 
+                message += '**MyAnimeList** users:\n'
+                message += '```{}```\n'.format(', '.join(mal_users))
+        if anilist_users: # If not empty
+            # Don't print if there is filters and MAL is not in them
+            if not filters or (filters and utils.Service.ANILIST in filters):
+                message += '**AniList** users:\n'
+                message += '```{}```\n'.format(', '.join(anilist_users))
+        message += 'Assigned channel : **{}**'.format(registered_channel)
+        if role is not None:
+            message += '\nAllowed role: **{}**'.format(role)
+    return message
+
+
+def get_service_filters_list(filters : str) -> List[utils.Service]:
+    ''' Creates and returns a service filter list from a comma-separated string '''
+
+    filters_list = []
+    for filter in filters.split(','):
+        try:
+            filters_list.append(utils.Service.from_str(filter))
+        except NotImplementedError:
+            pass # Ignore incorrect filter
+    return filters_list
+
+
+def in_allowed_role(user : discord.Member, server : int) -> bool :
+    ''' Check if a user has the permissions to configure the bot on a specific server '''
+
+    targetRole = utils.get_allowed_role(server.id)
+    globals.logger.debug ("Role target: " + str(targetRole))
+
+    if user.guild_permissions.administrator:
+        globals.logger.debug (str(user) + " is server admin on " + str(server) + "!")
+        return True
+    elif (targetRole is None):
+        globals.logger.debug ("No group specified for " + str(server))
+        return True
+    else:
+        for role in user.roles:
+            if str(role.id) == str(targetRole):
+                globals.logger.debug ("Permissions validated for " + str(user))
+                return True
+
+    return False
+
+
+def check_user_name_validity(user_name: str, service : utils.Service) -> Tuple[bool, str]:
+    """ Check if user_name exists on a specific service.
+        
+        Returns:
+            - bool: 	True if user_name exists
+            - str:		Error string if the user does not exist
+    """
+
+    if service == utils.Service.MAL:
+        try:
+            # Ping user profile to check validity
+            urllib.request.urlopen('{}{}'.format(globals.MAL_PROFILE_URL, user_name))
+        except urllib.error.HTTPError as e:
+            if (e.code == 404): # URL profile not found
+                return False, "User **{}** doesn't exist on MyAnimeList!".format(user_name)
+            else:
+                globals.logger.warning("HTTP Code {} while trying to add user '{}' and checking its validity.".format(e.code, user_name))
+                return False, "An error occured when we checked this username on MyAnimeList, maybe the website is down?"
+    elif service == utils.Service.ANILIST:
+        is_user_valid = anilist.check_username_validity(user_name)
+        if is_user_valid == False:
+            globals.logger.warning("No results returned while trying to add user '{}' and checking its validity.".format(user_name))
+            return False, "User **{}** doesn't exist on AniList!".format(user_name)
+    return True, None
+
+
+async def add_user_cmd(words, message):
+    ''' Processes the command "add" and add a user to fetch the data for '''
+
+    # Check if command is valid
+    if len(words) != 4:
+        if (len(words) < 4):
+            return await message.channel.send("Usage: {} add **{}**/**{}** **username**".format(globals.prefix, globals.SERVICE_MAL, globals.SERVICE_ANILIST))
+        return await message.channel.send("Too many arguments! You have to specify only one username.")
+
+    # Verify that the user is allowed
+    if in_allowed_role(message.author, message.guild) is False:
+        return await message.channel.send("Only allowed users can use this command!")
+
+    try:
+        service = utils.Service.from_str(words[2])
+    except NotImplementedError:
+        return await message.channel.send('Incorrect service. Use **"{}"** or **"{}"** for example'.format(globals.SERVICE_MAL, globals.SERVICE_ANILIST))
+    user = words[3]
+    server_id = str(message.guild.id)
+
+    if(len(user) > 14):
+        return await message.channel.send("Username too long!")
+
+    try:
+        # Check user validity
+        is_valid, error_string = check_user_name_validity(user, service)
+        if is_valid == False:
+            return await message.channel.send(error_string)
+
+        # Get user's servers
+        user_servers = utils.get_user_servers(user, service)
+        # User not present in database
+        if user_servers is None: 
+            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)))
+        else: # User present in database
+
+            is_server_present = server_id in user_servers.split(',')
+            if is_server_present == True: # The user already has registered this server
+                return await message.channel.send("User **{}** is already registered in our database for this server!".format(user))
+            else:
+                new_servers = '{},{}'.format(user_servers, server_id)
+                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)))
+    except Exception as e:
+        globals.logger.warning("Error while adding user '{}' on server '{}': {}".format(user, message.guild, str(e)))
+        return await message.channel.send("An unknown error occured while addind this user, the error has been logged.")
+
+
+async def delete_user_cmd(words, message):
+    ''' Processes the command "delete" and remove a registered user '''
+
+    # Check if command is valid
+    if len(words) != 4:
+        if (len(words) < 4):
+            return await message.channel.send("Usage: {} delete **{}**/**{}** **username**".format(globals.prefix, globals.SERVICE_MAL, globals.SERVICE_ANILIST))
+        return await message.channel.send("Too many arguments! You have to specify only one username.")
+
+    # Verify that the user is allowed
+    if in_allowed_role(message.author, message.guild) is False:
+        return await message.channel.send("Only allowed users can use this command!")
+
+    try:
+        service = utils.Service.from_str(words[2])
+    except NotImplementedError:
+        return await message.channel.send('Incorrect service. Use **"{}"** or **"{}"** for example'.format(globals.SERVICE_MAL, globals.SERVICE_ANILIST))
+    user = words[3]
+    server_id = str(message.guild.id)
+    
+    user_servers = utils.get_user_servers(user, service)
+    # If user is not present in the database
+    if user_servers is None:
+        return await message.channel.send("The user **" + user + "** is not in our database for this server!")
+
+    # Else if present, update the servers for this user
+    srv_string = utils.remove_server_from_servers(server_id, user_servers)
+    
+    if srv_string is None: # Server not present in the user's servers
+        return await message.channel.send("The user **" + user + "** is not in our database for this server!")
+
+    if srv_string == "":
+        utils.delete_user_from_db(user, service)
+    else:
+        utils.update_user_servers_db(user, service, srv_string)
+
+    return await message.channel.send("**" + user + "** deleted from the database for this server.")
+
+
+async def info_cmd(message, words):
+    ''' Processes the command "info" and sends a message '''
+
+    # Get filters if available
+    filters = []
+    if (len(words) >= 3): # If filters are specified
+        filters = get_service_filters_list(words[2])
+
+    server = message.guild
+    if utils.is_server_in_db(server.id) == False:
+         await message.channel.send("The server **{}** is not in our database.".format(server))
+    else:
+        users = utils.get_users()
+        channels = utils.get_channels(server.id)
+        role = utils.get_allowed_role(server.id)
+        if channels is None:
+            await message.channel.send("No channel assigned for this bot on this server.")
+        else:
+            await message.channel.send(build_info_cmd_message(users, server, channels, utils.get_role_name(role, server), filters))
+
+
+async def ping_cmd(message, channel):
+    ''' Responds to ping command '''
+    messageTimestamp = message.created_at
+    currentTimestamp = datetime.datetime.utcnow()
+    delta = round((currentTimestamp - messageTimestamp).total_seconds() * 1000)
+
+    await message.reply("pong ({}ms)".format(delta))
+
+
+async def about_cmd(channel):
+    ''' Responds to about command with a brief description of this bot '''
+
+    embed = discord.Embed(title="***MyAnimeBot Commands***", colour=0xEED000)
+    embed.title = "MyAnimeBot version {} by Penta & lulu".format(globals.VERSION)
+    embed.colour = 0xEED000
+    embed.description = """MyAnimeBot checks MyAnimeList and Anilist profiles for specified users, and send a message for every new activities found.
+        More help with the **{} help** command.
+        
+        Check our GitHub page for more informations: https://github.com/Penta/MyAnimeBot
+        """.format(globals.prefix)
+    embed.set_thumbnail(url=globals.iconBot)
+
+    await channel.send(embed=embed)
+
+
+async def help_cmd(channel):
+    ''' Responds to help command '''
+
+    embed = discord.Embed(title="***MyAnimeBot Commands***", colour=0xEED000)
+    embed.add_field(name="`here`", value="Register this channel. The bot will send new activities on registered channels.")
+    embed.add_field(name="`stop`", value="Un-register this channel. The bot will now stop sending new activities for this channel.")
+    embed.add_field(name="`info [mal|ani]`", value="Get the registered users for this server. Users can be filtered by specifying a service.")
+    embed.add_field(name="`add {mal|ani} <user>`", value="Register a user for a specific service.\nEx: `add mal MyUser`")
+    embed.add_field(name="`delete {mal|ani} <user>`", value="Remove a user for a specific service.\nEx: `delete ani MyUser`")
+    embed.add_field(name="`role <@discord_role>`", value="Specify a role that is able to manage the bot.\nEx: `role @Moderator`, `role @everyone`")
+    embed.add_field(name="`top`", value="Show statistics for this server.")
+    embed.add_field(name="`ping`", value="Ping the bot.")
+    embed.add_field(name="`about`", value="Get some information about this bot")
+    await channel.send(embed=embed)
+
+
+async def here_cmd(author, server, channel):
+    ''' Processes the command "here" and registers a channel to send new found feeds '''
+
+    # Verify that the user is allowed
+    if in_allowed_role(author, server) is False:
+        return await channel.send("Only allowed users can use this command!")
+    
+    if utils.is_server_in_db(server.id):
+        # Server in DB, so we need to update the channel
+
+        # Check if channel already registered
+        channels = utils.get_channels(server.id)
+        channels_id = [channel["channel"] for channel in channels]
+        globals.logger.debug("Channels {} and channel id {}".format(channels_id, channel.id))
+        if (str(channel.id) in channels_id):
+            await channel.send("Channel **{}** already in use for this server.".format(channel))
+        else:
+            cursor = globals.conn.cursor(buffered=True)
+            cursor.execute("UPDATE t_servers SET channel = {} WHERE server = {}".format(channel.id, server.id))
+            globals.conn.commit()
+            
+            await channel.send("Channel updated to: **{}**.".format(channel))
+            
+        cursor.close()
+    else:
+        # No server found in DB, so register it
+        cursor = globals.conn.cursor(buffered=True)
+        cursor.execute("INSERT INTO t_servers (server, channel) VALUES ({},{})".format(server.id, channel.id))
+        globals.conn.commit() # TODO Move to corresponding file
+        
+        await channel.send("Channel **{}** configured for **{}**.".format(channel, server))
+
+
+async def stop_cmd(author, server, channel):
+    ''' Processes the command "stop" and unregisters a channel '''
+
+    # Verify that the user is allowed
+    if in_allowed_role(author, server) is False:
+        return await channel.send("Only allowed users can use this command!")
+
+    if utils.is_server_in_db(server.id):
+        # Remove server from DB
+        cursor = globals.conn.cursor(buffered=True)
+        cursor.execute("DELETE FROM t_servers WHERE server = {}".format(server.id))
+        globals.conn.commit()
+
+        await channel.send("Server **{}** is now unregistered from our database.".format(server))
+    else:
+        await channel.send("Server **{}** was already not registered.".format(server))
+
+
+async def role_cmd(words, message, author, server, channel):
+    ''' Processes the command "role" and registers a role to be able to use the bot's commands '''
+
+    if len(words) <= 2:
+        return await channel.send("A role must be specified.")
+
+    if not author.guild_permissions.administrator:
+        return await channel.send("Only server's admins can use this command.")
+
+
+    role_str = words[2]
+    if (role_str == "everyone") or (role_str == "@everyone"):
+        cursor = globals.conn.cursor(buffered=True)
+        cursor.execute("UPDATE t_servers SET admin_group = NULL WHERE server = %s", [str(server.id)])
+        globals.conn.commit()
+        cursor.close()
+
+        await channel.send("Everyone is now allowed to use the bot.")
+    else: # A role is found
+        rolesFound = message.role_mentions
+
+        if (len(rolesFound) == 0):
+            return await channel.send("Please specify a correct role.")
+        elif (len(rolesFound) > 1):
+            return await channel.send("Please specify only 1 role.")
+        else:
+            roleFound = rolesFound[0]
+            # Update db with newly added role
+            cursor = globals.conn.cursor(buffered=True)
+            cursor.execute("UPDATE t_servers SET admin_group = %s WHERE server = %s", [str(roleFound.id), str(server.id)])
+            globals.conn.commit()
+            cursor.close()
+
+            await channel.send("The role **{}** is now allowed to use this bot!".format(roleFound.name))
+
+
+async def top_cmd(words, channel):
+    ''' Processes the command "top" and returns statistics on registered feeds '''
+
+    # TODO Redo this function
+
+    if len(words) == 2:
+        try:
+            cursor = globals.conn.cursor(buffered=True)
+            cursor.execute("SELECT * FROM v_Top")
+            data = cursor.fetchone()
+            
+            if data is None: await message.channel.send("It seems that there is no statistics... (what happened?!)")
+            else:
+                topText = "**__Here is the global statistics of this bot:__**\n\n"
+                
+                while data is not None:
+                    topText += " - " + str(data[0]) + ": " + str(data[1]) + "\n"
+                        
+                    data = cursor.fetchone()
+                    
+                cursor = globals.conn.cursor(buffered=True)
+                cursor.execute("SELECT * FROM v_TotalFeeds")
+                data = cursor.fetchone()
+                
+                topText += "\n***Total user entry***: " + str(data[0])
+                
+                cursor = globals.conn.cursor(buffered=True)
+                cursor.execute("SELECT * FROM v_TotalAnimes")
+                data = cursor.fetchone()
+                
+                topText += "\n***Total unique manga/anime***: " + str(data[0])
+                
+                await channel.send(topText)
+            
+            cursor.close()
+        except Exception as e:
+            globals.logger.warning("An error occured while displaying the global top: " + str(e))
+            await channel.send("Unable to reply to your request at the moment...")
+    elif len(words) > 2:
+        keyword = str(' '.join(words[2:]))
+        globals.logger.info("Displaying the global top for the keyword: " + keyword)
+        
+        try:
+            cursor = globals.conn.cursor(buffered=True)
+            cursor.callproc('sp_UsersPerKeyword', [str(keyword), '20'])
+            for result in cursor.stored_results():
+                data = result.fetchone()
+                
+                if data is None: await message.channel.send("It seems that there is no statistics for the keyword **" + keyword + "**.")
+                else:
+                    topKeyText = "**__Here is the statistics for the keyword " + keyword + ":__**\n\n"
+                    
+                    while data is not None:
+                        topKeyText += " - " + str(data[0]) + ": " + str(data[1]) + "\n"
+                            
+                        data = result.fetchone()
+                        
+                    await channel.send(topKeyText)
+                
+            cursor.close()
+        except Exception as e:
+            globals.logger.warning("An error occured while displaying the global top for keyword '" + keyword + "': " + str(e))
+            await channel.send("Unable to reply to your request at the moment...")
+
+
+async def on_mention(channel):
+    return await channel.send(":heart:")

+ 328 - 0
myanimebot/discord.py

@@ -0,0 +1,328 @@
+import asyncio
+import urllib.request
+from configparser import ConfigParser
+from datetime import datetime
+from typing import List, Tuple
+
+import aiohttp
+import feedparser
+import pytz
+from dateutil.parser import parse as parse_datetime
+
+import discord
+
+
+# Our modules
+import myanimebot.anilist as anilist
+import myanimebot.healthcheck as healthcheck
+import myanimebot.commands as commands
+import myanimebot.globals as globals  # TODO Rename globals module
+import myanimebot.myanimelist as myanimelist
+import myanimebot.utils as utils
+
+
+class MyAnimeBot(discord.Client):
+    async def on_ready(self):
+        globals.logger.info("Logged in as " + globals.client.user.name + " (" + str(globals.client.user.id) + ")")
+
+        globals.logger.info("Executing InitBoot procedure on database.")
+
+        try:
+            cursor = globals.conn.cursor(buffered=True)
+            cursor.callproc('sp_InitBoot')
+            cursor.close()
+        except Exception as e:
+            globals.logger.fatal(str(e))
+            quit()
+
+        globals.logger.info("Starting all tasks...")
+
+        if globals.MAL_ENABLED:
+            globals.task_feed = globals.client.loop.create_task(background_check_feed(globals.client.loop))
+
+        if globals.ANI_ENABLED:
+            globals.task_feed_anilist = globals.client.loop.create_task(anilist.background_check_feed(globals.client.loop))
+
+        if globals.HEALTHCHECK_ENABLED:
+            globals.task_healthcheck = globals.client.loop.create_task(healthcheck.main(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))
+
+    async def on_error(self, event, *args, **kwargs):
+        globals.logger.exception("Crap! An unknown Discord error occured...")
+
+    async def on_message(self, message):
+        if message.author == globals.client.user: return
+
+        words = message.content.strip().split()
+        channel = message.channel
+        author = str('{0.author.mention}'.format(message))
+
+        # Check input validity
+        if len(words) == 0:
+            return
+
+        # A user is trying to get help
+        if words[0] == globals.prefix:
+            if len(words) > 1:
+                if words[1] == "ping":
+                    await commands.ping_cmd(message, channel)
+
+                elif words[1] == "here":
+                    await commands.here_cmd(message.author, message.guild, channel)
+
+                elif words[1] == "add":
+                    await commands.add_user_cmd(words, message)
+
+                elif words[1] == "delete":
+                    await commands.delete_user_cmd(words, message)
+
+                elif words[1] == "stop":
+                    await commands.stop_cmd(message.author, message.guild, channel)
+
+                elif words[1] == "info":
+                    await commands.info_cmd(message, words)
+
+                elif words[1] == "about":
+                    await commands.about_cmd(channel)
+
+                elif words[1] == "help":
+                    await commands.help_cmd(channel)
+
+                elif words[1] == "top":
+                    await commands.top_cmd(words, channel)
+                
+                elif words[1] == "role":
+                    await commands.role_cmd(words, message, message.author, message.guild, channel)
+
+        # If mentioned
+        elif globals.client.user in message.mentions:
+            await commands.on_mention(channel)
+
+
+def build_embed(feed : utils.Feed):
+    ''' Build the embed message related to the anime's status '''
+
+    # Get service
+    if feed.service == utils.Service.MAL:
+        service_name = 'MyAnimeList'
+        profile_url = "{}{}".format(globals.MAL_PROFILE_URL, feed.user.name)
+        icon_url = globals.MAL_ICON_URL
+    elif feed.service == utils.Service.ANILIST:
+        service_name = 'AniList'
+        profile_url = "{}{}".format(globals.ANILIST_PROFILE_URL, feed.user.name)
+        icon_url = globals.ANILIST_ICON_URL
+    else:
+        raise NotImplementedError('Unknown service {}'.format(feed.service))
+    description = utils.build_description_string(feed)
+    content = "[{}]({})\n```{}```".format(utils.filter_name(feed.media.name), feed.media.url, description)
+    profile_url_label = "{}'s {}".format(feed.user.name, service_name)
+
+    try:
+        embed = discord.Embed(colour=int(feed.status.value, 16), url=feed.media.url, description=content, timestamp=feed.date_publication.astimezone(pytz.timezone("utc")))
+        embed.set_thumbnail(url=feed.media.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
+
+
+async def send_embed_wrapper(asyncioloop, channelid, client, embed):
+    ''' Send an embed message to a channel '''
+
+    channel = client.get_channel(int(channelid))
+
+    try:
+        await channel.send(embed=embed)
+        globals.logger.info("Message sent in channel: {}".format(channelid))
+    except Exception as e:
+        globals.logger.debug("Impossible to send a message on '{}': {}".format(channelid, e)) 
+        return
+
+    # Main function that check the RSS feeds from MyAnimeList
+async def background_check_feed(asyncioloop):
+    globals.logger.info("Starting up background_check_feed")
+    
+    # We configure the http header
+    http_headers = { "User-Agent": "MyAnimeBot Discord Bot v" + globals.VERSION, }
+    timeout = aiohttp.ClientTimeout(total=5)
+    
+    await globals.client.wait_until_ready()
+    
+    globals.logger.debug("Discord client connected, unlocking background_check_feed...")
+    
+    while not globals.client.is_closed():
+        try:
+            db_user = globals.conn.cursor(buffered=True, dictionary=True)
+            db_user.execute("SELECT mal_user, servers FROM t_users WHERE service=%s", [globals.SERVICE_MAL])
+            data_user = db_user.fetchone()
+        except Exception as e:
+            globals.logger.critical("Database unavailable! (" + str(e) + ")")
+            quit()
+
+        while data_user is not None:
+            user = utils.User(id=None,
+                                service_id=None,
+                                name=data_user[globals.DB_USER_NAME],
+                                servers=data_user["servers"].split(','))
+            stop_boucle = 0
+            feed_type = 1
+
+            try:
+                while stop_boucle == 0 :
+                    try:
+                        async with aiohttp.ClientSession() as httpclient:
+                            if feed_type == 1 :
+                                http_response = await httpclient.request("GET", "https://myanimelist.net/rss.php?type=rm&u=" + user.name, headers=http_headers, timeout=timeout)
+                                media = "manga"
+                            else : 
+                                http_response = await httpclient.request("GET", "https://myanimelist.net/rss.php?type=rw&u=" + user.name, headers=http_headers, timeout=timeout)
+                                media = "anime"
+                        http_data = await http_response.read()
+                    except asyncio.TimeoutError:
+                        globals.logger.error("Error while loading RSS of '{}': Timeout".format(user.name))
+                        break
+                    except Exception as e:
+                        globals.logger.exception("Error while loading RSS ({}) of '{}':\n".format(feed_type, user.name))
+                        break
+
+                    feeds_data = feedparser.parse(http_data)
+                    
+                    for feed_data in feeds_data.entries:
+                        pubDateRaw = datetime.strptime(feed_data.published, '%a, %d %b %Y %H:%M:%S %z').astimezone(globals.timezone)
+                        pubDate = pubDateRaw.strftime("%Y-%m-%d %H:%M:%S")
+                        if feed_type == 1:
+                            media_type = utils.MediaType.MANGA
+                        else:
+                            media_type = utils.MediaType.ANIME
+
+                        feed = myanimelist.build_feed_from_data(feed_data, user, None, pubDateRaw.timestamp(), media_type)
+                        
+                        cursor = globals.conn.cursor(buffered=True)
+                        cursor.execute("SELECT published, title, url, type FROM t_feeds WHERE published=%s AND title=%s AND user=%s AND type=%s AND obsolete=0 AND service=%s", [pubDate, feed.media.name, user.name, feed.get_status_str(), globals.SERVICE_MAL])
+                        data = cursor.fetchone()
+
+                        if data is None:
+                            var = datetime.now(globals.timezone) - pubDateRaw
+                            
+                            globals.logger.debug(" - " + feed.media.name + ": " + str(var.total_seconds()))
+                        
+                            if var.total_seconds() < globals.secondMax:
+                                globals.logger.info(user.name + ": Item '" + feed.media.name + "' not seen, processing...")
+                                
+                                cursor.execute("SELECT thumbnail FROM t_animes WHERE guid=%s AND service=%s LIMIT 1", [feed.media.url, globals.SERVICE_MAL]) # TODO Change that ?
+                                data_img = cursor.fetchone()
+                                
+                                if data_img is None:
+                                    try:
+                                        image = myanimelist.get_thumbnail(feed.media.url)
+                                        
+                                        globals.logger.info("First time seeing this " + media + ", adding thumbnail into database: " + image)
+                                    except Exception as e:
+                                        globals.logger.warning("Error while getting the thumbnail: " + str(e))
+                                        image = ""
+                                        
+                                    cursor.execute("INSERT INTO t_animes (guid, service, title, thumbnail, found, discoverer, media) VALUES (%s, %s, %s, %s, NOW(), %s, %s)", [feed.media.url, globals.SERVICE_MAL, feed.media.name, image, user.name, media])
+                                    globals.conn.commit()
+                                else: image = data_img[0]
+                                feed.media.image = image
+
+                                cursor.execute("UPDATE t_feeds SET obsolete=1 WHERE published=%s AND title=%s AND user=%s AND service=%s", [pubDate, feed.media.name, user.name, globals.SERVICE_MAL])
+                                cursor.execute("INSERT INTO t_feeds (published, title, service, url, user, found, type) VALUES (%s, %s, %s, %s, %s, NOW(), %s)", (pubDate, feed.media.name, globals.SERVICE_MAL, feed.media.url, user.name, feed.get_status_str()))
+                                globals.conn.commit()
+                                
+                                for server in user.servers:
+                                    db_srv = globals.conn.cursor(buffered=True)
+                                    db_srv.execute("SELECT channel FROM t_servers WHERE server = %s", [server])
+                                    data_channel = db_srv.fetchone()
+                                    
+                                    while data_channel is not None:
+                                        for channel in data_channel: await send_embed_wrapper(asyncioloop, channel, globals.client, build_embed(feed))
+                                        
+                                        data_channel = db_srv.fetchone()
+                    if feed_type == 1:
+                        feed_type = 0
+                        await asyncio.sleep(globals.MYANIMELIST_SECONDS_BETWEEN_REQUESTS)
+                    else:
+                        stop_boucle = 1
+                    
+            except Exception as e:
+                globals.logger.exception("Error when parsing RSS for '{}':\n".format(user.name))
+            
+            await asyncio.sleep(globals.MYANIMELIST_SECONDS_BETWEEN_REQUESTS)
+
+            data_user = db_user.fetchone()
+
+
+async def fetch_activities_anilist():
+    await anilist.check_new_activities()
+
+
+# Get a random anime name and change the bot's activity
+async def change_gameplayed(asyncioloop):
+    globals.logger.info("Starting up change_gameplayed")
+    
+    await globals.client.wait_until_ready()
+    await asyncio.sleep(1)
+
+    while not globals.client.is_closed():
+        # Get a random anime name from the users' list
+        cursor = globals.conn.cursor(buffered=True)
+        cursor.execute("SELECT title FROM t_animes ORDER BY RAND() LIMIT 1")
+        data = cursor.fetchone()
+        anime = utils.truncate_end_show(data[0])
+        
+        # Try to change the bot's activity
+        try:
+            if data is not None: await globals.client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=anime))
+        except Exception as e:
+            globals.logger.warning("An error occured while changing the displayed anime title: " + str(e))
+            
+        cursor.close()
+        # Do it every minute
+        await asyncio.sleep(60)
+
+async def update_thumbnail_catalog(asyncioloop):
+    globals.logger.info("Starting up update_thumbnail_catalog")
+    
+    while not globals.client.is_closed():
+        await asyncio.sleep(43200)
+        
+        globals.logger.info("Automatic check of the thumbnail database on going...")
+        reload = 0
+        
+        cursor = globals.conn.cursor(buffered=True)
+        cursor.execute("SELECT guid, title, thumbnail FROM t_animes")
+        data = cursor.fetchone()
+
+        while data is not None:
+            try:
+                if (data[2] != "") : urllib.request.urlopen(data[2])
+                else: reload = 1
+            except urllib.error.HTTPError as e:
+                globals.logger.warning("HTTP Error while getting the current thumbnail of '" + str(data[1]) + "': " + str(e))
+                reload = 1
+            except Exception as e:
+                globals.logger.debug("Error while getting the current thumbnail of '" + str(data[1]) + "': " + str(e))
+            
+            if (reload == 1) :
+                try:
+                    image = myanimelist.get_thumbnail(data[0])
+                        
+                    cursor.execute("UPDATE t_animes SET thumbnail = %s WHERE guid = %s", [image, data[0]])
+                    globals.conn.commit()
+                        
+                    globals.logger.info("Updated thumbnail found for \"" + str(data[1]) + "\": %s", image)
+                except Exception as e:
+                    globals.logger.warning("Error while downloading updated thumbnail for '" + str(data[1]) + "': " + str(e))
+
+            await asyncio.sleep(3)
+            data = cursor.fetchone()
+
+        cursor.close()
+
+        globals.logger.info("Thumbnail database checked.")

+ 121 - 0
myanimebot/globals.py

@@ -0,0 +1,121 @@
+import logging
+import os
+import socket
+from configparser import ConfigParser
+
+import discord
+import pytz
+import feedparser
+import mariadb
+import pytz
+
+
+class ImproperlyConfigured(Exception): pass
+
+BASE_DIR = os.path.dirname(os.path.abspath(__file__))
+HOME_DIR = os.path.expanduser("~")
+
+DEFAULT_CONFIG_PATHS = [
+	os.path.join("myanimebot.conf"),
+	os.path.join(BASE_DIR, "myanimebot.conf"),
+	os.path.join("/etc/myanimebot/myanimebot.conf"),
+	os.path.join(HOME_DIR, "myanimebot.conf")
+]
+
+def get_config():
+	config = ConfigParser()
+	config_paths = []
+
+	for path in DEFAULT_CONFIG_PATHS:
+		if os.path.isfile(path):
+			config_paths.append(path)
+			break
+	else: raise ImproperlyConfigured("No configuration file found")
+		
+	config.read(config_paths)
+
+	return config
+
+# Loading configuration
+try:
+	config=get_config()
+except Exception as e:
+	print ("Cannot read configuration: " + str(e))
+	exit (1)
+
+def get_env_var(name, default=None):
+    return os.getenv(name, default)
+
+CONFIG=config["MYANIMEBOT"]
+logLevel = get_env_var("MYANIMEBOT_LOGLEVEL", CONFIG.get("logLevel", "INFO"))
+dbHost = get_env_var("MYANIMEBOT_DBHOST", CONFIG.get("mariadb.host", "127.0.0.1"))
+dbUser = get_env_var("MYANIMEBOT_DBUSER", CONFIG.get("mariadb.user", "myanimebot"))
+dbPassword = get_env_var("MYANIMEBOT_DBPASSWORD", CONFIG.get("mariadb.password"))
+dbName = get_env_var("MYANIMEBOT_DBNAME", CONFIG.get("mariadb.name", "myanimebot"))
+dbSSLenabled = get_env_var("MYANIMEBOT_DBSSL", CONFIG.getboolean("mariadb.ssl", False))
+dbSSLca = get_env_var("MYANIMEBOT_DBSSLCACERT", CONFIG.get("mariadb.ssl.ca"))
+dbSSLcert = get_env_var("MYANIMEBOT_DBSSLCERT", CONFIG.get("mariadb.ssl.cert"))
+dbSSLkey = get_env_var("MYANIMEBOT_DBSSLKEY", CONFIG.get("mariadb.ssl.key"))
+logPath = get_env_var("MYANIMEBOT_LOGPATH", CONFIG.get("logPath", "myanimebot.log"))
+timezone = pytz.timezone(get_env_var("MYANIMEBOT_TIMEZONE", CONFIG.get("timezone", "utc")))
+secondMax = int(get_env_var("MYANIMEBOT_SECOND_MAX", CONFIG.getint("secondMax", 7200)))
+token = get_env_var("MYANIMEBOT_TOKEN", CONFIG.get("token"))
+prefix = get_env_var("MYANIMEBOT_PREFIX", CONFIG.get("prefix", "!mab"))
+MYANIMELIST_SECONDS_BETWEEN_REQUESTS = int(get_env_var("MYANIMEBOT_MAL_REQUESTS", CONFIG.getint("myanimelist_seconds_between_requests", 2)))
+iconBot = get_env_var("MYANIMEBOT_ICONBOT", CONFIG.get("iconBot", "http://myanimebot.pentou.eu/rsc/bot_avatar.jpg"))
+ANILIST_SECONDS_BETWEEN_FETCHES = int(get_env_var("MYANIMEBOT_ANILIST_FETCHES", CONFIG.getint("anilist_seconds_between_fetches", 60)))
+MAL_ICON_URL = get_env_var("MYANIMEBOT_MAL_ICON", CONFIG.get("iconMAL", "https://cdn.myanimelist.net/img/sp/icon/apple-touch-icon-256.png"))
+ANILIST_ICON_URL = get_env_var("MYANIMEBOT_ANILIST_ICON", CONFIG.get("iconAniList", "https://anilist.co/img/icons/android-chrome-512x512.png"))
+MAL_ENABLED = get_env_var("MYANIMEBOT_MAL_ENABLED", CONFIG.getboolean("mal_enabled", True))
+ANI_ENABLED = get_env_var("MYANIMEBOT_ANI_ENABLED", CONFIG.getboolean("ani_enabled", True))
+HEALTHCHECK_ENABLED = get_env_var("MYANIMEBOT_HEALTHCHECK_ENABLED", CONFIG.getboolean("healthcheck_enabled", False))
+HEALTHCHECK_PORT = int(get_env_var("MYANIMEBOT_HEALTHCHECK_PORT", CONFIG.getint("healthcheck_port", 15200)))
+HEALTHCHECK_IP = get_env_var("MYANIMEBOT_HEALTHCHECK_IP", CONFIG.get("healthcheck_ip", "0.0.0.0"))
+
+SERVICE_ANILIST = "ani"
+SERVICE_MAL = "mal"
+MAL_URL = "https://myanimelist.net/"
+MAL_PROFILE_URL = "https://myanimelist.net/profile/"
+ANILIST_PROFILE_URL = "https://anilist.co/user/"
+DB_USER_NAME = "mal_user"  # Nom de la colonne pour les noms d'utilisateur dans la table t_users
+
+# Log configuration
+log_format='%(asctime)-13s : %(name)-15s : %(levelname)-8s : %(message)s'
+logging.basicConfig(handlers=[logging.FileHandler(logPath, 'a', 'utf-8')], format=log_format, level=logLevel)
+
+console = logging.StreamHandler()
+console.setLevel(logging.INFO)
+console.setFormatter(logging.Formatter(log_format))
+
+logger = logging.getLogger("myanimebot")
+logger.setLevel(logLevel)
+
+logging.getLogger('').addHandler(console)
+
+# Script version
+VERSION = "1.0.0a"
+
+logger.info("Booting MyAnimeBot " + VERSION + "...")
+logger.debug("DEBUG log: OK")
+
+# feedparser.PREFERRED_XML_PARSERS.remove("drv_libxml2")
+
+# Initialization of the database
+try:
+	# Main database connection
+	if (dbSSLenabled) :
+		conn = mariadb.connect(host=dbHost, user=dbUser, password=dbPassword, database=dbName, ssl_ca=dbSSLca, ssl_cert=dbSSLcert, ssl_key=dbSSLkey)
+	else :
+		conn = mariadb.connect(host=dbHost, user=dbUser, password=dbPassword, database=dbName)
+except Exception as e:
+	logger.critical("Can't connect to the database: " + str(e))
+	quit()
+
+
+# Initialization of the Discord client
+client = None
+
+task_feed       	= None
+task_feed_anilist	= None
+task_gameplayed 	= None
+task_thumbnail  	= None

+ 183 - 0
myanimebot/healthcheck.py

@@ -0,0 +1,183 @@
+import requests
+import socket
+import threading
+import discord
+import math
+
+from http.server import BaseHTTPRequestHandler, HTTPServer
+from datetime import datetime
+from tcp_latency import measure_latency
+
+
+import myanimebot.globals as globals
+import myanimebot.utils as utils
+import myanimebot.anilist as anilist
+
+webtext = ""
+uptime = datetime.now().strftime("%H:%M:%S %d/%m/%Y")
+
+class MyServer(BaseHTTPRequestHandler):
+    def do_GET(self):
+
+        try:
+            timestamp_request = datetime.now()
+            webtext = "<html><head><title>MyAnimeBot Healthcheck status</title><link rel='icon' type='image/gif'' href='{}' /></head><body><h1>MyAnimeBot Healthcheck status</h1><table>".format(globals.iconBot)
+            code = 200
+
+            code, webtext = get_version(code, webtext)
+            code, webtext = get_uptime(code, webtext)
+            code, webtext = get_db_status(code, webtext)
+            code, webtext = get_discord_websocket_status(code, webtext)
+            code, webtext = get_anilist_status(code, webtext)
+            code, webtext = get_myanimelist_status(code, webtext)
+
+            generation_time = (datetime.now() - timestamp_request).total_seconds() * 1000
+            webtext += "</table><p><em>Healthcheck generated in {}ms.</em></p></body></html>".format(round(generation_time))
+        except:
+            webtext = "<html><head><title>MyAnimeBot Healthcheck status</title></head><body><h1>MyAnimeBot Healthcheck status</h1><p>An unexpected error as occured when we tried to generate the healthcheck page, check the logs for more information.</p></body></html>"
+            code = 503
+
+            globals.logger.exception("Error on the healthcheck:\n")
+
+        self.send_response(code)
+        self.send_header("Content-type", "text/html")
+        self.end_headers()
+
+        self.wfile.write(bytes(webtext, "utf-8"))
+
+
+def line_formatter (desc : str, state : str, level : int):
+    # Levels : 0 OK, 1 Error, 2 Warning, 3 Disabled
+
+    if (level == 0):
+        color = "7FFF00"
+    elif (level == 1):
+        color = "CD5C5C"
+    elif (level == 2):
+        color = "FFD700"
+    else:
+        color = "888888"
+
+    result = "<tr><td>{}: </td><td bgcolor='{}' ><strong>{}</strong></td></tr>".format(desc, color, state)
+    return result
+
+
+def ping(hostname : str):
+    latencies = measure_latency(host=hostname, runs=1, wait=0)
+    total = 0
+
+    for value in latencies:
+        total += value
+
+    result = math.trunc(total/len(latencies))
+    return result
+
+
+def get_anilist_status (code : int, webtext : str):
+    if (globals.ANI_ENABLED):
+        try:
+            ani_status_code = requests.post(anilist.ANILIST_GRAPHQL_URL, timeout=3, allow_redirects=False).status_code
+
+            if (ani_status_code == 400):
+                ani_ping = ping("graphql.anilist.co")
+
+                if (ani_ping < 300):
+                    webtext += line_formatter("AniList API status", "OK ({}ms)".format(ani_ping), 0)
+                else:
+                    webtext += line_formatter("AniList API status", "SLOW ({}ms)".format(ani_ping), 2)
+            else: 
+                webtext += line_formatter("AniList API status", "KO ({})".format(ani_status_code), 1)
+                if (code == 200): code = 500
+        except Exception as e:
+            webtext += line_formatter("AniList API status", "KO ({})".format(e), 1)
+            if (code == 200): code = 500
+    else:
+        webtext += line_formatter("AniList API status", "DISABLED", 3)
+    return code, webtext
+
+
+def get_myanimelist_status (code : int, webtext : str):
+    if (globals.MAL_ENABLED):
+        try:
+            mal_status_code = requests.head(globals.MAL_URL, timeout=3, allow_redirects=False).status_code
+
+            if (mal_status_code == 200):
+                mal_ping = ping("myanimelist.net")
+
+                if (mal_ping < 300):
+                    webtext += line_formatter("MyAnimeList status", "OK ({}ms)".format(mal_ping), 0)
+                else:
+                    webtext += line_formatter("MyAnimeList status", "SLOW ({}ms)".format(mal_ping), 2)
+            else: 
+                webtext += line_formatter("MyAnimeList status", "KO ({})".format(mal_status_code), 1)
+                if (code == 200): code = 500
+        except Exception as e:
+            webtext += line_formatter("MyAnimeList status", "KO ({})".format(e), 1)
+            if (code == 200): code = 500
+    else:
+        webtext += line_formatter("MyAnimeList status", "DISABLED", 3)
+    return code, webtext
+
+
+def get_uptime (code : int, webtext : str):
+    webtext += line_formatter("Script uptime", uptime, 0)
+    return code, webtext
+
+
+def get_version (code : int, webtext : str):
+    webtext += line_formatter("Script version", globals.VERSION, 0)
+    return code, webtext
+
+
+def get_discord_websocket_status (code : int, webtext : str):
+    if (globals.client.is_closed()) or (not globals.client.is_ready()):
+        webtext += line_formatter("Discord status", "KO", 1)
+        if (code == 200): code = 500
+    else:
+        if (globals.client.is_ws_ratelimited()): webtext += line_formatter("Discord status", "NOT OK (Rate limited)", 2)
+        else: webtext += line_formatter("Discord status", "OK (v{})".format(discord.__version__), 0)
+
+    return code, webtext
+
+
+def get_db_status (code : int, webtext : str):
+    try:
+        cursor = globals.conn.cursor(buffered=False)
+        cursor.execute("SELECT * FROM t_feeds LIMIT 1;")
+        cursor.fetchone()
+        cursor.close()
+
+        cursor = globals.conn.cursor(buffered=True, dictionary=True)
+        cursor.execute("SELECT @@VERSION AS ver;")
+        data = cursor.fetchone()
+        cursor.close()
+
+        webtext += line_formatter("Database status", "OK ({})".format(data["ver"]), 0)
+    except Exception as e:
+        webtext += line_formatter("Database status", "KO", 1)
+        globals.logger.error("The healthcheck cannot access to the database: {}".format(e))
+        if (code == 200): code = 500
+
+    return code, webtext
+
+
+def start_healthcheck(ip, port):
+    webServer = HTTPServer((ip, port), MyServer)
+    globals.logger.info("Healthcheck started on http://{}:{}".format(ip, port))
+
+    try:
+        webServer.serve_forever()
+    except KeyboardInterrupt:
+        pass
+    except Exception as e:
+        globals.logger.error("The healthcheck crashed: {}".format(e))
+
+async def main(asyncioloop):
+    ''' Main function that starts the Healthcheck web page '''
+
+    globals.logger.info("Starting up Healtcheck...")
+
+    healthcheck_thread = threading.Thread(name='healthcheck', target=start_healthcheck, args=(globals.HEALTHCHECK_IP, globals.HEALTHCHECK_PORT))
+    healthcheck_thread.setDaemon(True) 
+    healthcheck_thread.start()
+    

+ 85 - 0
myanimebot/myanimelist.py

@@ -0,0 +1,85 @@
+import re
+import urllib
+import datetime
+
+from bs4 import BeautifulSoup
+
+import myanimebot.utils as utils
+import myanimebot.globals as globals
+
+def get_thumbnail(urlParam):
+    ''' Returns the MAL media thumbnail from a link '''
+
+    url = "/".join((urlParam).split("/")[:5])
+
+    websource = urllib.request.urlopen(url)
+    soup = BeautifulSoup(websource.read(), "html.parser")
+    image = re.search(r'(?P<url>https?://[^\s]+)', str(soup.find("img", {"itemprop": "image"}))).group("url")
+    thumbnail = "".join(image.split('"')[:1]).replace('"','')
+
+    return thumbnail
+
+
+def build_feed_from_data(data, user : utils.User, image, pubDateRaw, type : utils.MediaType) -> utils.Feed:
+    if data is None: return None
+
+    if data.description.startswith('-') :
+        if type == utils.MediaType.MANGA:
+            data.description = "Re-reading " + data.description
+        else:
+            data.description = "Re-watching " + data.description
+
+    status, progress, episodes = break_rss_description_string(data.description)
+
+    media = utils.Media(id=None,
+                        name=data.title,
+                        url=data.link,
+                        episodes=episodes,
+                        image=image,
+                        type=type)
+
+    feed = utils.Feed(service=utils.Service.MAL,
+                        date_publication=datetime.datetime.fromtimestamp(pubDateRaw, globals.timezone),
+                        user=user,
+                        status=status,
+                        description=data.description, # TODO To remove, useless now
+                        media=media,
+                        progress=progress,
+                        score=None,
+                        score_format=None)
+    return feed
+
+
+def break_rss_description_string(description : str):
+    ''' Break a MyAnimeList RSS description from a feed into a Status, the progress and the number of episodes '''
+
+    # Description example: "Completed - 12 of 12 episodes"
+
+    # Split the description starting from the dash
+    split_desc = description.rsplit('-', 1)
+    if (len(split_desc) != 2):
+        globals.logger.error("Error while trying to break MAL RSS description. No '-' found in '{}'.".format(description))
+        return None, None, None
+
+    status_str = split_desc[0]
+    episodes_progress_and_count = split_desc[1]
+    status = utils.MediaStatus.from_str(status_str)
+
+    # Split the second part of the string (E.g. "12 of 12 episodes") to get the progress
+    episodes_progress_and_count_split = episodes_progress_and_count.split('of', 1)
+    if (len(episodes_progress_and_count_split) != 2):
+        globals.logger.error("Error while trying to break MAL RSS description. No 'of' found between the progress and the episode count in '{}'.".format(description))
+        return None, None, None
+
+    progress = episodes_progress_and_count_split[0].strip()
+    episodes_count_str = episodes_progress_and_count_split[1].strip()
+
+    # Remove the episodes label from our string
+    episode_count_split = episodes_count_str.split(' ', 1)
+    if (len(episode_count_split) != 2):
+        globals.logger.error("Error while trying to break MAL RSS description. No space found between the episode count and the label episodes in '{}'.".format(description))
+        return None, None, None
+
+    episodes_count = episode_count_split[0]
+
+    return status, progress, episodes_count

+ 398 - 0
myanimebot/utils.py

@@ -0,0 +1,398 @@
+import datetime
+from enum import Enum
+from typing import List
+
+import myanimebot.globals as globals
+
+
+# TODO Redo all of the desc/status system
+
+# Media Status colors
+CURRENT_COLOR     = "0x00CC00"
+PLANNING_COLOR    = "0xBFBFBF"
+COMPLETED_COLOR   = "0x000088"
+DROPPED_COLOR     = "0xCC0000"
+PAUSED_COLOR      = "0xDDDD00"
+REPEATING_COLOR   = "0x007700"
+
+
+class Service(Enum):
+    MAL=globals.SERVICE_MAL
+    ANILIST=globals.SERVICE_ANILIST
+
+    @staticmethod
+    def from_str(label: str):
+        if label is None: raise TypeError
+
+        if label.upper() in ('MAL', 'MYANIMELIST', globals.SERVICE_MAL.upper()):
+            return Service.MAL
+        elif label.upper() in ('AL', 'ANILIST', globals.SERVICE_ANILIST.upper()):
+            return Service.ANILIST
+        else:
+            raise NotImplementedError('Error: Cannot convert "{}" to a Service'.format(label))
+
+
+class MediaType(Enum):
+    ANIME="ANIME"
+    MANGA="MANGA"
+
+
+    @staticmethod
+    def from_str(label: str):
+        if label is None: raise TypeError
+
+        if label.upper() in ('ANIME', 'ANIME_LIST'):
+            return MediaType.ANIME
+        elif label.upper() in ('MANGA', 'MANGA_LIST'):
+            return MediaType.MANGA
+        else:
+            raise NotImplementedError('Error: Cannot convert "{}" to a MediaType'.format(label))
+
+
+    def get_media_count_type(self):
+        if self == MediaType.ANIME:
+            return 'episodes'
+        elif self == MediaType.MANGA:
+            return 'chapters'
+        else:
+            raise NotImplementedError('Unknown MediaType "{}"'.format(self))
+
+
+class MediaStatus(Enum):
+    CURRENT     = CURRENT_COLOR
+    PLANNING    = PLANNING_COLOR
+    COMPLETED   = COMPLETED_COLOR
+    DROPPED     = DROPPED_COLOR
+    PAUSED      = PAUSED_COLOR
+    REPEATING   = REPEATING_COLOR
+
+    @staticmethod
+    def from_str(label: str):
+        if label is None: raise TypeError
+
+        first_word = label.split(' ')[0].upper()
+
+        if first_word in ['READ', 'READING', 'WATCHED', 'WATCHING']:
+            return MediaStatus.CURRENT
+        elif first_word in ['PLANS', 'PLAN']:
+            return MediaStatus.PLANNING
+        elif first_word in ['COMPLETED']:
+            return MediaStatus.COMPLETED
+        elif first_word in ['DROPPED']:
+            return MediaStatus.DROPPED
+        elif first_word in ['PAUSED', 'ON-HOLD']:
+            return MediaStatus.PAUSED
+        elif first_word in ['REREAD', 'REREADING', 'REWATCHED', 'REWATCHING', 'RE-READING', 'RE-WATCHING', 'RE-READ', 'RE-WATCHED']:
+            return MediaStatus.REPEATING
+        else:
+            raise NotImplementedError('Error: Cannot convert "{}" to a MediaStatus'.format(label))
+
+
+class User():
+    data = None
+
+    def __init__(self,
+                  id            : int,
+                  service_id    : int,
+                  name          : str,
+                  servers       : List[int]):
+        self.id = id
+        self.service_id = service_id
+        self.name = name
+        self.servers = servers
+
+class Media():
+    def __init__(self,
+                 id         : int,
+                 name       : str,
+                 url        : str,
+                 episodes   : str,
+                 image      : str,
+                 type       : MediaType):
+        self.id = id
+        self.name = name
+        self.url = url
+        self.episodes = episodes
+        self.image = image
+        self.type = type
+
+
+class Feed():
+    def __init__(self,
+                 service        : Service,
+                 date_publication : datetime.datetime,
+                 user           : User,
+                 status         : MediaStatus,
+                 description    : str, # TODO Need to change
+                 progress       : str,
+                 media          : Media,
+                 score          : float,
+                 score_format   : str
+                 ):
+        self.service = service
+        self.date_publication = date_publication
+        self.user = user
+        self.status = status
+        self.media = media
+        self.description = description
+        self.progress = progress
+        self.score = score
+        self.score_format = score_format
+
+    
+    def get_status_str(self):
+
+        if self.status == MediaStatus.CURRENT \
+        or self.status == MediaStatus.REPEATING:
+
+            if self.media.type == MediaType.ANIME:
+                status_str = 'Watching'
+            elif self.media.type == MediaType.MANGA:
+                status_str = 'Reading'
+            else:
+                raise NotImplementedError('Unknown MediaType: {}'.format(self.media.type))
+
+            # Add prefix if rewatching
+            if self.status == MediaStatus.REPEATING:
+                status_str = 'Re-{}'.format(status_str.lower())
+
+        elif self.status == MediaStatus.COMPLETED:
+            status_str = 'Completed'
+        elif self.status == MediaStatus.PAUSED:
+            status_str = 'Paused'
+        elif self.status == MediaStatus.DROPPED:
+            status_str = 'Dropped'
+        elif self.status == MediaStatus.PLANNING:
+
+            if self.media.type == MediaType.ANIME:
+                media_type_label = 'watch'
+            elif self.media.type == MediaType.MANGA:
+                media_type_label = 'read'
+            else:
+                raise NotImplementedError('Unknown MediaType: {}'.format(self.media.type))
+
+            status_str = 'Plans to {}'.format(media_type_label)
+        else:
+            raise NotImplementedError('Unknown MediaStatus: {}'.format(self.status))
+
+        return status_str
+
+
+def replace_all(text : str, replace_dic : dict) -> str:
+    '''Replace multiple substrings from a string'''
+    
+    if text is None or replace_dic is None:
+        return text
+
+    for replace_key, replace_value in replace_dic.items():
+        text = text.replace(replace_key, replace_value)
+    return text
+
+
+def filter_name(name : str) -> str:
+    '''Escapes special characters from name'''
+
+    dic = {
+        "♥": "\♥",
+        "♀": "\♀",
+        "♂": "\♂",
+        "♪": "\♪",
+        "☆": "\☆"
+        }
+
+    return replace_all(name, dic)
+
+# Check if the show's name ends with a show type and truncate it
+def truncate_end_show(media_name : str):
+    '''Check if a show's name ends with a show type and truncate it'''
+
+    if media_name is None: return None
+
+    show_types = (
+        '- TV',
+        '- Movie',
+        '- Special',
+        '- OVA',
+        '- ONA',
+        '- Manga',
+        '- Manhua',
+        '- Manhwa',
+        '- Novel',
+        '- One-Shot',
+        '- Doujinshi',
+        '- Music',
+        '- OEL',
+        '- Unknown',
+        '- Light Novel'
+    )
+
+    for show_type in show_types:
+        if media_name.endswith(show_type):
+            new_show = media_name[:-len(show_type)]
+            # Check if space at the end
+            if new_show.endswith(' '):
+                new_show = new_show[:-1]
+            return new_show
+    return media_name
+
+
+def build_score_string(score_format : str):
+    if score_format == "POINT_100":
+        return "100"
+    elif score_format == "POINT_10" or score_format == "POINT_10_DECIMAL":
+        return "10"
+    elif score_format == "POINT_5":
+        return "5"
+    elif score_format == "POINT_3":
+        return "3"
+    else:
+        return "?"    
+
+
+def build_description_string(feed : Feed):
+    '''Build and returns a string describing the feed'''
+
+    media_type_count = feed.media.type.get_media_count_type()
+    status_str = feed.get_status_str()
+
+    # Build the string
+    desc = '{} | {} of {} {}'.format(status_str, feed.progress, feed.media.episodes, media_type_count)
+    globals.logger.error("Feed media score {}".format(feed.score))
+    if feed.score is not None:
+        desc += '\nScore: {} / {}'.format(feed.score, build_score_string(feed.score_format))
+    return desc
+
+
+def get_channels(server_id: int) -> dict:
+    '''Returns the registered channels for a server'''
+
+    if server_id is None: return None
+
+    # TODO Make generic execute
+    cursor = globals.conn.cursor(buffered=True, dictionary=True)
+    cursor.execute("SELECT channel FROM t_servers WHERE server = %s", [server_id])
+    channels = cursor.fetchall()
+    cursor.close()
+    return channels
+
+
+def is_server_in_db(server_id : str) -> bool:
+    '''Checks if server is registered in the database'''
+
+    if server_id is None:
+        return False
+
+    cursor = globals.conn.cursor(buffered=True)
+    cursor.execute("SELECT server FROM t_servers WHERE server=%s", [server_id])
+    data = cursor.fetchone()
+    cursor.close()
+    return data is not None
+
+
+def get_users() -> List[dict]:
+    '''Returns all registered users'''
+
+    cursor = globals.conn.cursor(buffered=True, dictionary=True)
+    cursor.execute('SELECT {}, service, servers FROM t_users'.format(globals.DB_USER_NAME))
+    users = cursor.fetchall()
+    cursor.close()
+    return users
+
+def get_user_servers(user_name : str, service : Service) -> str:
+    '''Returns a list of every registered servers for a user of a specific service, as a string'''
+
+    if user_name is None or service is None:
+        return
+
+    cursor = globals.conn.cursor(buffered=True, dictionary=True)
+    cursor.execute("SELECT servers FROM t_users WHERE LOWER({})=%s AND service=%s".format(globals.DB_USER_NAME),
+                     [user_name.lower(), service.value])
+    user_servers = cursor.fetchone()
+    cursor.close()
+
+    if user_servers is not None:
+        return user_servers["servers"]
+    return None
+
+
+def remove_server_from_servers(server : str, servers : str) -> str:
+    '''Removes the server from a comma-separated string containing multiple servers'''
+
+    servers_list = servers.split(',')
+
+    # If the server is not found, return None
+    if server not in servers_list:
+        return None
+
+    # Remove every occurence of server
+    servers_list = [x for x in servers_list if x != server]
+    # Build server-free string
+    return ','.join(servers_list)
+
+
+def delete_user_from_db(user_name : str, service : Service) -> bool:
+    '''Removes the user from the database'''
+
+    if user_name is None or service is None:
+        globals.logger.warning("Error while trying to delete user '{}' with service '{}'".format(user_name, service))
+        return False
+
+    cursor = globals.conn.cursor(buffered=True)
+    cursor.execute("DELETE FROM t_users WHERE LOWER({}) = %s AND service=%s".format(globals.DB_USER_NAME),
+                         [user_name.lower(), service.value])
+    globals.conn.commit()
+    cursor.close()
+    return True
+
+
+def update_user_servers_db(user_name : str, service : Service, servers : str) -> bool:
+    if user_name is None or service is None or servers is None:
+        globals.logger.warning("Error while trying to update user's servers. User '{}' with service '{}' and servers '{}'".format(user_name, service, servers))
+        return False
+
+    cursor = globals.conn.cursor(buffered=True)
+    cursor.execute("UPDATE t_users SET servers = %s WHERE LOWER({}) = %s AND service=%s".format(globals.DB_USER_NAME),
+                          [servers, user_name.lower(), service.value])
+    globals.conn.commit()
+    cursor.close()
+    return True
+
+
+def insert_user_into_db(user_name : str, service : Service, servers : str) -> bool:
+    '''Add the user to the database'''
+
+    if user_name is None or service is None or servers is None:
+        globals.logger.warning("Error while trying to add user '{}' with service '{}' and servers '{}'".format(user_name, service, servers))
+        return False
+
+    cursor = globals.conn.cursor(buffered=True)
+    cursor.execute("INSERT INTO t_users ({}, service, servers) VALUES (%s, %s, %s)".format(globals.DB_USER_NAME),
+                        [user_name, service.value, servers])
+    globals.conn.commit()
+    cursor.close()
+    return True
+
+def get_allowed_role(server : int) -> int:
+    '''Return the allowed role for a given server'''
+
+    cursor = globals.conn.cursor(buffered=True, dictionary=True)
+    cursor.execute("SELECT admin_group FROM t_servers WHERE server=%s LIMIT 1", [str(server)])
+    allowedRole = cursor.fetchone()
+    cursor.close()
+
+    if allowedRole is None:
+        return None
+
+    return allowedRole["admin_group"]
+
+def get_role_name(provided_role_id : int, server) -> str :
+    ''' Convert a role ID into a displayable name '''
+
+    role_name = None
+    if provided_role_id is not None:
+        for role in server.roles:
+            if str(role.id) == str(provided_role_id):
+                role_name = role.name
+        if role_name is None:
+            role_name = "[DELETED ROLE]"
+    return role_name

+ 37 - 0
requirements.txt

@@ -0,0 +1,37 @@
+aiodns==2.0.0
+aiohttp==3.6.3
+async-timeout==3.0.1
+asyncio==3.4.3
+attrs==20.3.0
+beautifulsoup4==4.9.3
+cchardet==2.1.7
+certifi==2020.12.5
+cffi==1.14.4
+chardet==3.0.4
+discord.py==1.6.0
+feedparser==6.0.2
+html2text==2020.1.16
+idna==2.10
+importlib-metadata==3.3.0
+iniconfig==1.1.1
+mariadb==1.0.5
+multidict==4.7.6
+packaging==20.8
+pluggy==0.13.1
+py==1.10.0
+pycares==3.1.1
+pycparser==2.20
+PyNaCl==1.4.0
+pyparsing==2.4.7
+python-dateutil==2.8.1
+pytz==2020.5
+requests==2.25.1
+sgmllib3k==1.0.0
+six==1.15.0
+soupsieve==2.1
+toml==0.10.2
+typing-extensions==3.7.4.3
+urllib3==1.26.2
+yarl==1.5.1
+zipp==3.4.0
+tcp-latency==0.0.10

+ 6 - 0
tests/requirements.txt

@@ -0,0 +1,6 @@
+codecov==2.1.11
+coverage==5.3.1
+pytest==6.2.1
+pytest-asyncio==0.14.0
+pytest-cov
+git+https://github.com/bravosierra99/dpytest.git#egg=dpytest # Installing a fork of dpytest to make it work with new versions of discord.py

+ 77 - 0
tests/test_commands.py

@@ -0,0 +1,77 @@
+import pytest
+import discord
+import myanimebot.commands as commands
+import myanimebot.globals as globals
+from myanimebot.discord import MyAnimeBot
+import discord.ext.test as dpytest
+
+
+@pytest.fixture
+def client(event_loop):
+    ''' Create our mock client to be used for testing purposes '''
+
+    intents = discord.Intents.default()
+    intents.members = True
+
+    c = MyAnimeBot(loop=event_loop, intents=intents)
+    dpytest.configure(c)
+
+    return c
+
+
+@pytest.mark.asyncio
+async def test_about_cmd(client):
+    guild = client.guilds[0]
+    channel = guild.text_channels[0]
+
+    await commands.about_cmd(channel)
+
+    embed = discord.Embed(title="***MyAnimeBot Commands***", colour=0xEED000)
+    embed.title = "MyAnimeBot version {} by Penta & lulu".format(globals.VERSION)
+    embed.colour = 0xEED000
+    embed.description = """MyAnimeBot checks MyAnimeList and Anilist profiles for specified users, and send a message for every new activities found.
+        More help with the **{} help** command.
+        
+        Check our GitHub page for more informations: https://github.com/Penta/MyAnimeBot
+        """.format(globals.prefix)
+
+    dpytest.verify_embed(embed=embed)
+
+    await dpytest.empty_queue()
+
+
+@pytest.mark.asyncio
+async def test_help_cmd(client):
+    guild = client.guilds[0]
+    channel = guild.text_channels[0]
+
+    await commands.help_cmd(channel)
+
+    embed = discord.Embed(title="***MyAnimeBot Commands***", colour=0xEED000)
+    embed.add_field(name="`here`", value="Register this channel. The bot will send new activities on registered channels.")
+    embed.add_field(name="`stop`", value="Un-register this channel. The bot will now stop sending new activities for this channel.")
+    embed.add_field(name="`info [mal|ani]`", value="Get the registered users for this server. Users can be filtered by specifying a service.")
+    embed.add_field(name="`add {mal|ani} <user>`", value="Register a user for a specific service.\nEx: `add mal MyUser`")
+    embed.add_field(name="`delete {mal|ani} <user>`", value="Remove a user for a specific service.\nEx: `delete ani MyUser`")
+    embed.add_field(name="`role <@discord_role>`", value="Specify a role that is able to manage the bot.\nEx: `role @Moderator`, `role @everyone`")
+    embed.add_field(name="`top`", value="Show statistics for this server.")
+    embed.add_field(name="`ping`", value="Ping the bot.")
+    embed.add_field(name="`about`", value="Get some information about this bot")
+
+    dpytest.verify_embed(embed=embed)
+
+    await dpytest.empty_queue()
+
+
+@pytest.mark.asyncio
+async def test_ping_cmd(client):
+    guild = client.guilds[0]
+    channel = guild.text_channels[0]
+
+    message = await channel.send("Test Message")
+    await dpytest.empty_queue()
+
+    await commands.ping_cmd(message, channel)
+    dpytest.verify_message(text="pong", contains=True)
+
+    await dpytest.empty_queue()

+ 95 - 0
tests/test_myanimelist.py

@@ -0,0 +1,95 @@
+import pytest
+
+from myanimebot.myanimelist import break_rss_description_string, get_thumbnail
+from myanimebot.utils import MediaStatus
+
+def test_get_thumbnail():
+    # Test manga
+    try:
+        link = "https://myanimelist.net/manga/103890/Bokutachi_wa_Benkyou_ga_Dekinai"
+        expected_thumbnail = "https://cdn.myanimelist.net/images/manga/3/197080.jpg"
+        assert get_thumbnail(link) == expected_thumbnail
+    except Exception:
+        pytest.fail("Should not raise Exception")
+
+    # Test anime
+    try:
+        link = "https://myanimelist.net/anime/40028/Shingeki_no_Kyojin__The_Final_Season"
+        expected_thumbnail = "https://cdn.myanimelist.net/images/anime/1000/110531.jpg"
+        assert get_thumbnail(link) == expected_thumbnail
+    except Exception:
+        pytest.fail("Should not raise Exception")
+
+    # Test anime 2
+    try:
+        link = "https://myanimelist.net/anime/40028"
+        expected_thumbnail = "https://cdn.myanimelist.net/images/anime/1000/110531.jpg"
+        assert get_thumbnail(link) == expected_thumbnail
+    except Exception:
+        pytest.fail("Should not raise Exception")
+
+    # Test fail
+    with pytest.raises(Exception):
+        get_thumbnail('')
+
+    with pytest.raises(Exception):
+        get_thumbnail('https://myanimelist.net/anime/test/')
+        
+    with pytest.raises(Exception):
+        get_thumbnail('https://anilist.co/anime/110277/Attack-on-Titan-Final-Season/')
+
+
+def test_break_rss_description_string():
+
+    status, progress, episodes = break_rss_description_string('Completed - 12 of 12 episodes')
+    assert status == MediaStatus.COMPLETED
+    assert progress == '12'
+    assert episodes == '12'
+
+    status, progress, episodes = break_rss_description_string('Completed - 192 of 192 chapters')
+    assert status == MediaStatus.COMPLETED
+    assert progress == '192'
+    assert episodes == '192'
+
+    status, progress, episodes = break_rss_description_string('Paused - 24 of 192 chapters')
+    assert status == MediaStatus.PAUSED
+    assert progress == '24'
+    assert episodes == '192'
+
+    status, progress, episodes = break_rss_description_string('On-hold - 23 of 27 episodes')
+    assert status == MediaStatus.PAUSED
+    assert progress == '23'
+    assert episodes == '27'
+
+    status, progress, episodes = break_rss_description_string('Dropped - 17 of 11 episodes')
+    assert status == MediaStatus.DROPPED
+    assert progress == '17'
+    assert episodes == '11'
+
+    status, progress, episodes = break_rss_description_string('Watching - 1 of 2 episodes')
+    assert status == MediaStatus.CURRENT
+    assert progress == '1'
+    assert episodes == '2'
+
+    status, progress, episodes = break_rss_description_string('Reading - 192 of ? chapters')
+    assert status == MediaStatus.CURRENT
+    assert progress == '192'
+    assert episodes == '?'
+
+    status, progress, episodes = break_rss_description_string('Rewatching - 0 of 1 episodes')
+    assert status == MediaStatus.REPEATING
+    assert progress == '0'
+    assert episodes == '1'
+
+    # Incorrect cases
+    status, progress, episodes = break_rss_description_string('Toto')
+    assert status == None and progress == None and episodes == None
+
+    status, progress, episodes = break_rss_description_string('Completed - blabla')
+    assert status == None and progress == None and episodes == None
+
+    status, progress, episodes = break_rss_description_string('Completed - 24 of 32')
+    assert status == None and progress == None and episodes == None
+
+    with pytest.raises(NotImplementedError):
+        break_rss_description_string('Toto - 24 of 32 episodes')

+ 318 - 0
tests/test_utils.py

@@ -0,0 +1,318 @@
+import pytest
+
+from myanimebot.utils import *
+from myanimebot.globals import SERVICE_MAL, SERVICE_ANILIST
+
+def test_MediaType_from_str():
+    try:
+        # Testing for ANIME
+        assert MediaType.ANIME == MediaType.from_str('ANIME')
+        assert MediaType.ANIME == MediaType.from_str('anime')
+        assert MediaType.ANIME == MediaType.from_str('ANiMe')
+        assert MediaType.ANIME == MediaType.from_str('anime_list')
+        assert MediaType.ANIME == MediaType.from_str('ANIME_LIST')
+        assert MediaType.ANIME == MediaType.from_str('ANiMe_LiST')
+
+        # Testing for MANGA
+        assert MediaType.MANGA == MediaType.from_str('MANGA')
+        assert MediaType.MANGA == MediaType.from_str('manga')
+        assert MediaType.MANGA == MediaType.from_str('ManGA')
+        assert MediaType.MANGA == MediaType.from_str('manga_list')
+        assert MediaType.MANGA == MediaType.from_str('MANGA_LIST')
+        assert MediaType.MANGA == MediaType.from_str('ManGA_LiSt')
+    except Exception as e:
+        pytest.fail("Unexpected Exception : {}".format(e))
+
+    # Testing incorrect MediaType
+    with pytest.raises(NotImplementedError, match='Cannot convert "TEST_LIST" to a MediaType'):
+        MediaType.from_str('TEST_LIST')
+    with pytest.raises(NotImplementedError, match='Cannot convert "Blabla" to a MediaType'):
+        MediaType.from_str('Blabla')
+    with pytest.raises(NotImplementedError, match='Cannot convert "Toto" to a MediaType'):
+        MediaType.from_str('Toto')
+    with pytest.raises(NotImplementedError, match='Cannot convert "ANIMU" to a MediaType'):
+        MediaType.from_str('ANIMU')
+    with pytest.raises(NotImplementedError, match='Cannot convert "mango" to a MediaType'):
+        MediaType.from_str('mango')
+    with pytest.raises(NotImplementedError):
+        MediaType.from_str('')
+    with pytest.raises(TypeError):
+        MediaType.from_str(None)
+
+
+def test_Service_from_str():
+    try:
+        # Testing for MAL
+        assert Service.MAL == Service.from_str('MAL')
+        assert Service.MAL == Service.from_str('MYANIMELIST')
+        assert Service.MAL == Service.from_str(SERVICE_MAL)
+        assert Service.MAL == Service.from_str('MaL')
+        assert Service.MAL == Service.from_str('mal')
+        assert Service.MAL == Service.from_str('myanimelist')
+        assert Service.MAL == Service.from_str('mYANimEliST')
+
+        # Testing for Anilist
+        assert Service.ANILIST == Service.from_str('AniList')
+        assert Service.ANILIST == Service.from_str('AL')
+        assert Service.ANILIST == Service.from_str(SERVICE_ANILIST)
+        assert Service.ANILIST == Service.from_str('ANILIST')
+        assert Service.ANILIST == Service.from_str('anilist')
+        assert Service.ANILIST == Service.from_str('al')
+        assert Service.ANILIST == Service.from_str('Al')
+    except Exception as e:
+        pytest.fail("Unexpected Exception : {}".format(e))
+
+    # Testing incorrect Services
+    with pytest.raises(NotImplementedError, match='Cannot convert "Kitsu" to a Service'):
+        Service.from_str('Kitsu')
+    with pytest.raises(NotImplementedError, match='Cannot convert "toto" to a Service'):
+        Service.from_str('toto')
+    with pytest.raises(NotImplementedError, match='Cannot convert "ani list" to a Service'):
+        Service.from_str('ani list')
+    with pytest.raises(NotImplementedError, match='Cannot convert "mla" to a Service'):
+        Service.from_str('mla')
+    with pytest.raises(TypeError):
+        Service.from_str(None)
+
+
+def test_replace_all():
+    with pytest.raises(AttributeError):
+        replace_all("texte", [])
+
+    assert replace_all(None, {}) == None
+    assert replace_all("toto", None) == "toto"
+
+    assert replace_all("texte", {}) == "texte"
+    assert replace_all("I is a string", {"is": "am"}) == "I am a string"
+    assert replace_all("abcdef abcdef 123", {
+        "a": "z",
+        "2": "5",
+        "c": "-"
+        }) == "zb-def zb-def 153"
+
+    assert replace_all("toto", {
+        "to": "ta",
+        "z": "ZUUU"
+        }) == "tata"
+    assert replace_all("", {
+        "to": "ta",
+        "z": "ZUUU"
+        }) == ""
+    assert replace_all("abcdcba", {
+        "a": "0",
+        "b": "1",
+        "0": "z"
+        }) == "z1cdc1z"
+
+
+def test_filter_name():
+    assert filter_name("Bonjour") == "Bonjour"
+    assert filter_name("") == ""
+    assert filter_name("Bonjour ♥") == "Bonjour \♥"
+    assert filter_name("♥ Bonjour ♥") == "\♥ Bonjour \♥"
+    assert filter_name("♥♪☆♂☆♀♀ ♥") == "\♥\♪\☆\♂\☆\♀\♀ \♥"
+    assert filter_name("♣") == "♣"
+    assert filter_name(None) == None
+
+
+def test_truncate_end_show():
+    assert truncate_end_show("Toto - TV") == "Toto"
+    assert truncate_end_show("Toto - Movie") == "Toto"
+    assert truncate_end_show("Toto - Special") == "Toto"
+    assert truncate_end_show("Toto - OVA") == "Toto"
+    assert truncate_end_show("Toto - ONA") == "Toto"
+    assert truncate_end_show("Toto - Manga") == "Toto"
+    assert truncate_end_show("Toto - Manhua") == "Toto"
+    assert truncate_end_show("Toto - Manhwa") == "Toto"
+    assert truncate_end_show("Toto - Novel") == "Toto"
+    assert truncate_end_show("Toto - One-Shot") == "Toto"
+    assert truncate_end_show("Toto - Doujinshi") == "Toto"
+    assert truncate_end_show("Toto - Music") == "Toto"
+    assert truncate_end_show("Toto - OEL") == "Toto"
+    assert truncate_end_show("Toto - Unknown") == "Toto"
+    
+    assert truncate_end_show("Toto- TV") == "Toto"
+    assert truncate_end_show("Toto- Music") == "Toto"
+    assert truncate_end_show("Titi-Music") == "Titi-Music"
+    assert truncate_end_show("- Music") == ""
+    assert truncate_end_show(None) == None
+
+
+def test_media_status():
+    # Testing Current
+    try:
+        current = MediaStatus.CURRENT
+        assert MediaStatus.from_str('read') == current
+        assert MediaStatus.from_str('READ') == current
+        assert MediaStatus.from_str('ReaD') == current
+        assert MediaStatus.from_str('Reading') == current
+        assert MediaStatus.from_str('READING') == current
+        assert MediaStatus.from_str('reading') == current
+        assert MediaStatus.from_str('watched') == current
+        assert MediaStatus.from_str('WATCHING') == current
+        assert MediaStatus.from_str('WATCHED') == current
+        assert MediaStatus.from_str('watChing') == current
+        assert current.value == CURRENT_COLOR
+    except Exception as e:
+        pytest.fail("Unexpected Exception : {}".format(e))
+
+    with pytest.raises(NotImplementedError):
+        MediaStatus.from_str('Watchh')
+    with pytest.raises(NotImplementedError):
+        MediaStatus.from_str('Watches')
+    with pytest.raises(NotImplementedError):
+        MediaStatus.from_str('Red')
+    with pytest.raises(NotImplementedError):
+        MediaStatus.from_str('Watc hed')
+
+    # Testing Planning
+    try:
+        planning = MediaStatus.PLANNING
+        assert MediaStatus.from_str('PLANS') == planning
+        assert MediaStatus.from_str('plans') == planning
+        assert MediaStatus.from_str('PlAns') == planning
+        assert MediaStatus.from_str('Plan') == planning
+        assert MediaStatus.from_str('plan') == planning
+        assert MediaStatus.from_str('PLAN') == planning
+        assert planning.value == PLANNING_COLOR
+    except Exception as e:
+        pytest.fail("Unexpected Exception : {}".format(e))
+    with pytest.raises(NotImplementedError):
+        MediaStatus.from_str('planned')
+    with pytest.raises(NotImplementedError):
+        MediaStatus.from_str('Pla')
+    with pytest.raises(NotImplementedError):
+        MediaStatus.from_str('pla n')
+
+    # Testing Completed
+    try:
+        completed = MediaStatus.COMPLETED
+        assert MediaStatus.from_str('Completed') == completed
+        assert MediaStatus.from_str('COMPLETED') == completed
+        assert MediaStatus.from_str('completed') == completed
+        assert completed.value == COMPLETED_COLOR
+    except Exception as e:
+        pytest.fail("Unexpected Exception : {}".format(e))
+
+    with pytest.raises(NotImplementedError):
+        MediaStatus.from_str('Complete')
+    with pytest.raises(NotImplementedError):
+        MediaStatus.from_str('Compl eted')
+
+    # Testing Dropped
+    try:
+        dropped = MediaStatus.DROPPED
+        assert MediaStatus.from_str('DroPPed') == dropped
+        assert MediaStatus.from_str('DROPPED') == dropped
+        assert MediaStatus.from_str('dropped') == dropped
+        assert dropped.value == DROPPED_COLOR
+    except Exception as e:
+        pytest.fail("Unexpected Exception : {}".format(e))
+
+    with pytest.raises(NotImplementedError):
+        MediaStatus.from_str('Drop')
+    with pytest.raises(NotImplementedError):
+        MediaStatus.from_str('Drops')
+    with pytest.raises(NotImplementedError):
+        MediaStatus.from_str(' Dropped')
+
+    # Testing Paused
+    try:
+        paused = MediaStatus.PAUSED
+        assert MediaStatus.from_str('PAUSED') == paused
+        assert MediaStatus.from_str('paused') == paused
+        assert MediaStatus.from_str('PaUSed') == paused
+        assert MediaStatus.from_str('ON-HOLD') == paused
+        assert MediaStatus.from_str('on-hold') == paused
+        assert MediaStatus.from_str('ON-hold') == paused
+        assert MediaStatus.from_str('on-HOLD') == paused
+        assert paused.value == PAUSED_COLOR
+    except Exception as e:
+        pytest.fail("Unexpected Exception : {}".format(e))
+
+    with pytest.raises(NotImplementedError):
+        MediaStatus.from_str('pauses')
+    with pytest.raises(NotImplementedError):
+        MediaStatus.from_str('on hold')
+    with pytest.raises(NotImplementedError):
+        MediaStatus.from_str('onhold')
+
+    # Testing Repeating
+    try:
+        repeating = MediaStatus.REPEATING
+        assert MediaStatus.from_str('reread') == repeating
+        assert MediaStatus.from_str('REREAD') == repeating
+        assert MediaStatus.from_str('reReaD') == repeating
+        assert MediaStatus.from_str('reReading') == repeating
+        assert MediaStatus.from_str('REREADING') == repeating
+        assert MediaStatus.from_str('rereading') == repeating
+        assert MediaStatus.from_str('rewatched') == repeating
+        assert MediaStatus.from_str('REWATCHING') == repeating
+        assert MediaStatus.from_str('reWATCHED') == repeating
+        assert MediaStatus.from_str('RewatChing') == repeating
+        assert MediaStatus.from_str('Re-watChing') == repeating
+        assert MediaStatus.from_str('Re-watChed') == repeating
+        assert MediaStatus.from_str('Re-readiNg') == repeating
+        assert MediaStatus.from_str('Re-Read') == repeating
+        assert repeating.value == REPEATING_COLOR
+    except Exception as e:
+        pytest.fail("Unexpected Exception : {}".format(e))
+
+    with pytest.raises(NotImplementedError):
+        MediaStatus.from_str('rreread')
+    with pytest.raises(NotImplementedError):
+        MediaStatus.from_str('rewatches')
+    with pytest.raises(NotImplementedError):
+        MediaStatus.from_str('re read')
+
+    # Testing incorrect uses cases
+    with pytest.raises(NotImplementedError):
+        MediaStatus.from_str('')
+    with pytest.raises(TypeError):
+        MediaStatus.from_str(None)
+
+
+def test_feed_get_status_str():
+    user = User(id=0, service_id=0, name='test', servers=[])
+
+    media = Media(id=None,
+                  name='Random anime',
+                    url=None,
+                    episodes='?',
+                    image=None,
+                    type=MediaType.ANIME)
+
+    feed = Feed(service=Service.MAL,
+                    date_publication=None,
+                    user=user,
+                    status=MediaStatus.COMPLETED,
+                    description=None,
+                    media=media,
+                    progress='?',
+                    score=None,
+                    score_format=None)
+
+    assert feed.get_status_str() == 'Completed'
+    feed.status = MediaStatus.PLANNING
+    assert feed.get_status_str() == 'Plans to watch'
+    feed.status = MediaStatus.DROPPED
+    assert feed.get_status_str() == 'Dropped'
+    feed.status = MediaStatus.PAUSED
+    assert feed.get_status_str() == 'Paused'
+    feed.status = MediaStatus.CURRENT
+    assert feed.get_status_str() == 'Watching'
+    feed.status = MediaStatus.REPEATING
+    assert feed.get_status_str() == 'Re-watching'
+
+    feed.media.type = MediaType.MANGA
+    assert feed.get_status_str() == 'Re-reading'
+    feed.status = MediaStatus.COMPLETED
+    assert feed.get_status_str() == 'Completed'
+    feed.status = MediaStatus.PLANNING
+    assert feed.get_status_str() == 'Plans to read'
+    feed.status = MediaStatus.DROPPED
+    assert feed.get_status_str() == 'Dropped'
+    feed.status = MediaStatus.PAUSED
+    assert feed.get_status_str() == 'Paused'
+    feed.status = MediaStatus.CURRENT
+    assert feed.get_status_str() == 'Reading'