Last active
December 15, 2020 17:02
-
-
Save Eclairemoy/9fcc7f04de76c6b9f573118532d80c31 to your computer and use it in GitHub Desktop.
Completed Code for What to Watch Bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import os | |
import pandas as pd | |
import requests | |
from flask import Flask, request, send_file | |
app = Flask(__name__) | |
app.config['SECRET_KEY'] = os.environ.get('FLASK_SECRET_KEY') | |
MOVIE_DB_API_KEY = os.environ.get('MOVIE_DB_API_KEY') | |
@app.route('/show-greeting', methods=['POST']) | |
def show_greeting(): | |
return send_file('show_greeting.json') | |
@app.route('/title-description', methods=['POST']) | |
def title_description(): | |
return send_file('title_description.json') | |
@app.route('/collect', methods=['POST']) | |
def collect(): | |
memory = json.loads(request.form.get('Memory')) | |
answers = memory['twilio']['collected_data']['what_to_watch']['answers'] | |
media_type = answers['media_type']['answer'] | |
genre = answers['genres']['answer'] | |
if media_type == "show": | |
options = get_tv_recs(genre.lower()) | |
else: | |
options = get_movie_recs(genre.lower()) | |
recs = options.get('title_rec', None) | |
message = ( | |
f'Ok, you want to watch a {genre} {media_type}.' | |
f' Here are some options: {recs}.\n\n Do you want more info about any of these titles Y/N?' | |
) | |
return { | |
'actions': [ | |
{'say': message}, | |
{'listen': True}, | |
{"remember" : options } | |
] | |
} | |
@app.route('/collect-desc', methods=['POST']) | |
def collect_desc(): | |
memory = json.loads(request.form.get('Memory')) | |
answers = memory['twilio']['collected_data']['titledescription']['answers'] | |
title = answers['titles']['answer'] | |
description = get_description(title) | |
message = ( | |
f'{description}\n\n Is there anything else I can help with?' | |
) | |
return { | |
'actions': [ | |
{'say': message}, | |
{'listen': True} | |
] | |
} | |
@app.route('/get-movie-recs', methods=['GET']) | |
def get_movie_recs(genre): | |
memory = json.loads(request.form.get('Memory')) | |
genre = get_movie_genre_int(genre) | |
url = "https://api.themoviedb.org/3/discover/movie?api_key={api_key}&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1&with_genres={genre}".format(api_key=MOVIE_DB_API_KEY, genre=genre) | |
try: | |
r = requests.get(url) | |
json_response = r.json() | |
results_list = json_response['results'] | |
overviews = [res['overview'] for res in results_list][0:5] | |
titles = [res['title'] for res in results_list][0:5] | |
recommended_title = ', '.join(titles) | |
except: | |
print(r.error) | |
return { 'title_rec': recommended_title, | |
'overviews': overviews, | |
'titles': titles } | |
@app.route('/get-tv-recs', methods=['GET']) | |
def get_tv_recs(genre): | |
memory = json.loads(request.form.get('Memory')) | |
genre = get_tv_genre_int(genre) | |
url = "https://api.themoviedb.org/3/discover/tv?api_key={api_key}&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1&with_genres={genre}".format(api_key=MOVIE_DB_API_KEY, genre=genre) | |
try: | |
r = requests.get(url) | |
json_response = r.json() | |
results_list = json_response['results'] | |
overviews = [res['overview'] for res in results_list][0:5] | |
titles = [res['name'] for res in results_list][0:5] | |
recommended_title = ', '.join(titles) | |
except: | |
print(r.error) | |
return { 'title_rec': recommended_title, | |
'overviews': overviews, | |
'titles': titles } | |
@app.route('/get-description', methods=['GET']) | |
def get_description(title): | |
memory = json.loads(request.form.get('Memory')) | |
title_index = memory['titles'].index(title) | |
overview = memory['overviews'][title_index] | |
seen = is_unseen(title) | |
return overview + "\n\n" + seen | |
def get_movie_genre_int(genre): | |
movie_genres = { "action": 28, | |
"adventure": 12, | |
"animation": 16, | |
"comedy": 35, | |
"crime": 80, | |
"documentary": 99, | |
"drama": 18, | |
"family": 10751, | |
"fantasy": 14, | |
"history": 36, | |
"horror": 27, | |
"music": 10402, | |
"mystery": 9648, | |
"romance": 10749, | |
"sci-fi": 878, | |
"tv-movie": 10770, | |
"thriller": 53, | |
"war": 10752, | |
"western": 37 | |
} | |
genre_int = movie_genres.get(genre, None) | |
return genre_int | |
def get_tv_genre_int(genre): | |
tv_genres = { "action": 10759, | |
"animation": 16, | |
"comedy": 35, | |
"crime": 80, | |
"documentary": 99, | |
"drama": 18, | |
"family": 10751, | |
"kids": 10762, | |
"mystery": 9648, | |
"news": 10763, | |
"reality": 10764, | |
"sci-fi": 10765, | |
"soap": 10766, | |
"talk": 10767, | |
"war": 10768, | |
"western": 37 | |
} | |
genre_int = tv_genres.get(genre, None) | |
return genre_int | |
def is_unseen(media_title): | |
""" | |
Open CSV, check to see if not yet seen | |
if seen search again if not return the title | |
""" | |
df = pd.read_csv("app/test_history.csv", usecols=[0]) | |
new_df = df[df['Title'].str.contains(media_title, na=False)] | |
if new_df.empty: | |
return "You have not seen this yet!" | |
return "You already watched this on Netflix!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"actions": [ | |
{ | |
"collect": { | |
"name": "what_to_watch", | |
"questions": [ | |
{ | |
"question": "Hi there! Do you want to watch a show or movie?", | |
"name": "media_type", | |
"type": "media_type" | |
}, | |
{ | |
"question": "Which genre would you like to watch? You can pick things like action, comedy, drama, or sci-fi", | |
"name": "genres", | |
"type": "genres" | |
} | |
], | |
"on_complete": { | |
"redirect": "[your-ngrok-link]/collect" | |
} | |
} | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"actions": [ | |
{ | |
"collect": { | |
"name": "titledescription", | |
"questions": [ | |
{ | |
"question": "Which title would you like to hear more about?", | |
"name": "titles", | |
"type": "titles" | |
} | |
], | |
"on_complete": { | |
"redirect": "[your-ngrok-link]/collect-desc" | |
} | |
} | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from twilio.rest import Client | |
account_sid = os.environ.get('TWILIO_ACCOUNT_SID') | |
auth_token = os.environ.get('TWILIO_AUTH_TOKEN') | |
assistant_sid = os.environ.get('ASSISTANT_SID') | |
client = Client(account_sid, auth_token) | |
#Get the existing Greeting Task Record SID | |
tasks = client.autopilot.assistants(assistant_sid) \ | |
.tasks \ | |
.list(limit=5) | |
for record in tasks: | |
if record.unique_name == 'greeting': | |
greeting_task_sid = record.sid | |
#Update the Greeting Task | |
task = client.autopilot.assistants(assistant_sid) \ | |
.tasks(greeting_task_sid) \ | |
.update(friendly_name='show_greeting', | |
actions_url='[your-ngrok-link]/show-greeting') | |
print(task.friendly_name) | |
# Create the Genre Type | |
genre_field_type = client.autopilot \ | |
.assistants(assistant_sid) \ | |
.field_types \ | |
.create(unique_name='genres') | |
print(genre_field_type.sid) | |
# Create the Genres Values | |
genres_list = ["action", "adventure", "animation", "comedy", "crime", "documentary", "drama", | |
"family", "fantasy", "history", "horror", "music", "mystery", "romance", "sci-fi", | |
"tv-movie", "thriller", "war", "western", "kids", "news", "reality", "soap", "talk"] | |
for genre_value in genres_list: | |
field_value = client.autopilot \ | |
.assistants(assistant_sid) \ | |
.field_types(genre_field_type.sid) \ | |
.field_values \ | |
.create(language='en-US', value=genre_value) | |
print(field_value.sid) | |
# Create the Media Field Type | |
media_field_type = client.autopilot \ | |
.assistants(assistant_sid) \ | |
.field_types \ | |
.create(unique_name='media_type') | |
print(media_field_type.sid) | |
# Create the Media Type Values | |
media_types = ["show", "movie"] | |
for media in media_types: | |
media_field_value = client.autopilot \ | |
.assistants(assistant_sid) \ | |
.field_types(media_field_type.sid) \ | |
.field_values \ | |
.create(language='en-US', value=media) | |
print(media_field_value.sid) | |
# Create the Title Description Task | |
title_description_task = client.autopilot.assistants(assistant_sid) \ | |
.tasks \ | |
.create(unique_name='title_description', | |
actions_url='[your-ngrok-link]/title-description') | |
print(title_description_task.sid) | |
# Create the Title Description Task Samples | |
description_list = ['Yes', 'Y', 'yes please'] | |
for description in description_list: | |
description_sample = client.autopilot \ | |
.assistants(assistant_sid) \ | |
.tasks(title_description_task.sid) \ | |
.samples \ | |
.create(language='en-US', tagged_text=description) | |
print(description_sample.sid) | |
# Create the Titles Field Type | |
titles_field_type = client.autopilot \ | |
.assistants(assistant_sid) \ | |
.field_types \ | |
.create(unique_name='titles') | |
print(titles_field_type.sid) | |
# Create the Media Type Values | |
titles = ['SEAL Team', 'Fear the Walking Dead', 'Game of Thrones', 'The Boys', 'The Simpsons', | |
'Dragon Ball Z', 'Big Mouth', 'Uzaki-chan Wants to Hang Out!', 'Rick and Morty', 'Moonbase 8', | |
'House', 'Lucifer', '30 Coins', 'The Undoing', 'Chicago P.D.', 'FBI', 'Carmel: Who Killed Maria Marta?', | |
'Trial 4', 'Disney Gallery: The Mandalorian', 'Kingdom of Plants', 'The Playbook', 'Selena: The Series', | |
'The Good Doctor', "Grey's Anatomy", 'Malcolm in the Middle', 'Anne with an E', "Dexter's Laboratory", | |
'Riverdale', 'Supernatural', 'The Mandalorian', 'His Dark Materials', 'Attack on Titan', 'The Liberator', | |
'The Barrier', 'Homeland', 'Westworld', 'Yellowstone', 'The High Chaparral', 'Little House on the Prairie', | |
'Wynonna Earp', 'Miraculous: Tales of Ladybug & Cat Noir', 'Ben 10', 'Jurassic World: Camp Cretaceous', | |
'Gravity Falls', 'Victorious', 'The Colbert Report', 'The Daily Show with Trevor Noah', 'Saturday Night Live', | |
'The One Show', 'Meet the Press', 'Acapulco Shore', 'Running Man', 'Keeping Up with the Kardashians', | |
'Diomedes, el Cacique de La Junta', 'Until I Met You', 'The Lord of the Skies', 'Yo soy Betty, la fea', | |
'Rebelde', 'Teresa', 'Avenida Brasil', 'The Graham Norton Show', 'DAS!', 'Real Time with Bill Maher', | |
'The Dick Cavett Show', 'NDR Talk Show', 'Chick Fight', 'Mulan', 'The Croods: A New Age', | |
"Angela's Christmas Wish", 'The SpongeBob Movie: Sponge on the Run', 'Alien Xmas', 'Once Upon a Snowman', | |
'Godmothered', 'Historias lamentables', 'Christmas Crossfire', 'Folklore: The Long Pond Studio Sessions', | |
'After Porn Ends 2', 'Money Heist: The Phenomenon', '69: The Saga of Danny Hernandez', 'Baby God', | |
'Welcome to Sudden Death', 'Honest Thief', 'The Christmas Chronicles: Part Two', 'Just Another Christmas', | |
'Upside-Down Magic', 'Jiu Jitsu', 'Fatman', 'The Craft: Legacy', 'Arthur & Merlin: Knights of Camelot', | |
'Selena', 'Mank', 'Bone Tomahawk', "Roald Dahl's The Witches", 'Peninsula', 'Jingle Jangle: A Christmas Journey', | |
'Phineas and Ferb The Movie: Candace Against the Universe', 'Trolls World Tour', 'The Lion King', 'Enola Holmes', | |
'Come Play', 'Annabelle Comes Home', 'Happy Halloween, Scooby-Doo!', 'After We Collided', 'Happiest Season', | |
'Operation Christmas Drop', 'The Princess Switch: Switched Again', 'Battlefield 2025', 'Archive', 'The New Mutants', | |
'Midnight at the Magnolia', 'We Bare Bears: The Movie', 'No Good Deed', 'Ice Age: The Great Egg-Scapade', 'Tenet', | |
'Hard Kill', 'Wander', 'Rogue City', 'Greenland', 'Rogue Warfare: Death of a Nation', 'Greyhound', '1917', | |
'The Outpost', 'Rogue Warfare: The Hunt', 'The Dalton Gang', 'Badland', 'Hell on the Border', 'Eminence Hill'] | |
for title in titles: | |
title_field_value = client.autopilot \ | |
.assistants(assistant_sid) \ | |
.field_types(titles_field_type.sid) \ | |
.field_values \ | |
.create(language='en-US', value=title) | |
#Get the existing Goodbye Task Record SID | |
tasks = client.autopilot.assistants(assistant_sid) \ | |
.tasks \ | |
.list(limit=5) | |
for record in tasks: | |
if record.unique_name == 'goodbye': | |
goodbye_task_sid = record.sid | |
#Update the Goodbye Task Samples | |
goodbye_sample = client.autopilot \ | |
.assistants(assistant_sid) \ | |
.tasks(goodbye_task_sid) \ | |
.samples \ | |
.create(language='en-US', tagged_text="n") | |
print(goodbye_sample.sid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment