brandan-schmitz / plexbot

Plexbot - A discord bot for automating movie libraries.
0 stars 0 forks source link

Add ability to search for and download TV shows #97

Closed brandan-schmitz closed 3 years ago

brandan-schmitz commented 3 years ago

This will be difficult to do given most sites do not have API's that can be accessed. However looking at a project like (Torrents-API)[https://github.com/Ryuk-me/Torrents-Api} can help determine methods for obtaining that data.

brandan-schmitz commented 3 years ago

Episode Qulity Reference Guide:

Human Readable String API Quality String ID
SD TV sdtv 1
SD DVD sddvd 2
HD TV hdtv 4
Raw HD TV rawhdtv 8
720p WEB-DL hdwebdl 32
720p BluRay hdbluray 128
1080p HD TV fullhdtv 16
1080p WEB-DL fullhdwebdl 64
1080p BlueRay fullhdbluray 256
2160p UHD 4K uhd4kweb 1024



Steps in requesting a TV show:

  1. User search for a show on the bot and select the appropriate show. It will be based off shows, not episodes.

  2. Use TVDB ID of show user selected and add it to Sickgear:

    http://10.0.10.17:8081/api/7929e2b3d33c385d3fad06f016281f83/?cmd=sg.show.addnew&indexer=1&indexerid=258541&status=wanted&upgrade_once=false&initial%5B%5D=hdwebdl&initial%5B%5D=hdbluray&initial%5B%5D=fullhdtv&initial%5B%5D=fullhdwebdl&initial%5B%5D=fullhdbluray

    Indexerid is the TVDB ID. That should produce a return type like this:

    {
     "data": {
       "name": "Chicago Fire"
     },
     "message": "Chicago Fire has been queued to be added",
     "result": "success"
    }
  3. Display a message to the user stating the show has been added to the bot and any episodes that can be found will be added.



Periodic Task: Fetch Sickgear History

This process will need to be a scheduled task, and should run every 30 seconds. It will fetch the history from sickgear and add the history of snatched episodes to the bots download queue database so that the other peridic task, queue downloader, can download the episodes.

  1. Make a request to sickgear's history API to fetch a list of snatched episode files:

    http://10.0.10.17:8081/api/7929e2b3d33c385d3fad06f016281f83/?cmd=sg.history&type=snatched

    That request should return a list of objects that look like this:

    {
     "data": [
       {
         "date": "2021-09-06 15:02",
         "episode": 2,
         "hide": 0,
         "indexer": 1,
         "indexerid": 258541,
         "provider": "LimeTorrents",
         "quality": "1080p WEB-DL",
         "resource": "Chicago.Fire.S01E02.1080p.WEB-DL.DD5.1.H.264-KiNGS.[PublicHD]",
         "resource_path": "",
         "season": 1,
         "show_name": "Chicago Fire",
         "status": "Snatched",
         "tvdbid": 258541,
         "version": -1
       }
     ],
     "message": "",
     "result": "success"
    }
  2. Parse the objects in the list add add them to a download queue table in the database. Important fields to save is episode, season, and tvdbid in addition to the resource field. This is the filename (minus the extension) of the downloaded torrent/magnet file. During this, multiple steps will need too occur.

    1. Fetch history and convert to history objects.
    2. Check the filesystem to determine if the file is a .torrent or a .magnet file.
    3. Set the file type as determined above to the object.
    4. Save objects to the database, ones that already exist should be ignored.



Periodic Tasks: Queue Downloader

This peridic task should run every 60 seconds and is responsible for fetching the next (oldest) file in the download queue and creating a download task for it. This should perform a check to make sure that there are no more than 4 (default) of these downloads running at any given time to avoid overloading the system. That number should be read from a config item so it can be adjusted without recompiling the program.

  1. Fetch the next item from the queue table in the database then create a new task to download the file. The remainder of the steps outlined below should be executed in that task not the main task. Delete the queue item from the queue database before moving to the next queue item to avoid duplicating task assignments.
  2. Add the item to real-debrid. If the file is a torrent, use the PUT method at the /torrents/addTorrent endpoint (need to create this API method in the client). If it is a magnet file, read the first line of the magnet file then use the existing addMagnet() function to add the magnet.
  3. After the file is downloaded, delete the torrent or magnet file from the filesystem and move the downloaded file to the right location.
  4. Add the new episode to the episodes column in the database.
brandan-schmitz commented 3 years ago

Instead of deleting the queue item from the queue database when a work item is started, the database table should contain a field for the status of the item. Valid options should be queued, or processing. The bot should be able to verify that ones marked in the database as processing are indeed still processing. If they are not, it should check the file system to verify the file was completed otherwise it should set the status back to queued. This will make sure that if something gets interrupted that it does not get skipped or corrupted.

brandan-schmitz commented 3 years ago

Closed with the release of v2.3.0