aboutsummaryrefslogtreecommitdiff
path: root/app/commands/number_response_request.py
blob: bb9958cbc027574796bce5d50e425cd138e4be62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import datetime
import requests
import sqlite3

import initialize_variables
from create_message import create_message


def number_response_request(from_number, message):
    if from_number not in initialize_variables.temp_movie_ids.keys():
        create_message(from_number, "There is no current request that you can decide on. It might be that your /request command timed out due since you took too long to response. Please try again. If this issue persists, please contact Parker.")
        return

    # If its been 5 minutes since prompt was sent, alert user of timed out request
    if (datetime.datetime.now() - initialize_variables.temp_movie_ids[from_number]['time']).total_seconds() / 60 > 5:
        del initialize_variables.temp_movie_ids[from_number]
        create_message(from_number, "You waited too long and therefore your request has timed out.\n\nPlease try again by re-running the /request command. If this issue persists, please contact Parker.")
        return

    # Otherwise, all checks have been completed
    create_message(from_number, "Just a moment while I add your movie to the library...")
    movie_number = initialize_variables.numbers_responses[message.strip()]
    try:
        tmdb_id = initialize_variables.temp_movie_ids[from_number]['ids'][movie_number - 1]
    except IndexError:
        create_message(from_number, "You did not enter a valid number. Please re-send the /request command and try again. If you believe this is an error, please contact Parker.")
        del initialize_variables.temp_movie_ids[from_number]
        return

    data = requests.get(f'{initialize_variables.radarr_host_url}/api/v3/movie/lookup/tmdb?tmdbId={tmdb_id}', headers=initialize_variables.headers)

    data = data.json()
    movie_title = data['title']
    # Change the qualityProfileId, monitored, and rootFolderPath values
    data['qualityProfileId'] = initialize_variables.quality_profile_id
    data['monitored'] = True
    data['rootFolderPath'] = initialize_variables.root_folder_path
    # Send data to Radarr API
    response = requests.post(f'{initialize_variables.radarr_host_url}/api/v3/movie', headers=initialize_variables.headers, json=data)
    data = response.json()
    movie_id = data['id']
    # Send message to user alerting them that the movie was added to the library
    create_message(from_number, f"🎉 {data['title']} has been added to the library!\n\nTo check up on the status of your movie(s) send /status - please wait at least 5 minutes before running this command in order to get an accurate time.")

    # After everything is completed, send Radarr a request to search indexers for new movie
    requests.post(f'{initialize_variables.radarr_host_url}/api/v3/command', headers=initialize_variables.headers, json={'name': 'MoviesSearch', 'movieIds': [int(movie_id)]})

    # Add the movie_id to the database so that users can check up on the status of their movie
    db = sqlite3.connect(initialize_variables.db_path)
    cursor = db.cursor()
    cursor.execute('''
        INSERT INTO movies(from_number, movie_id, movie_title)
        VALUES(?, ?, ?)
    ''', (from_number, movie_id, movie_title))
    db.commit()
    db.close()

    del initialize_variables.temp_movie_ids[from_number]
    return