-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
142 lines (118 loc) · 5.42 KB
/
main.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# -*- coding: utf-8 -*-
""" Created on Sat Sep 24 19:43:39 2022, by Phil bradbury """
import streamlit as st
import requests
import json
# Add Bootstrap
st.markdown('<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">', unsafe_allow_html=True)
# Import CSS file to style output page
with open("styles.css") as css:
st.markdown(f'<style>{css.read()}</style>', unsafe_allow_html = True)
#Data dictionaries
services_of_interest = {
"netflix": "Netflix",
"prime": "Amazon Prime",
"disney": "Disney+",
"apple": "Apple TV",
"now": "Now TV",
"iplayer": "BBC iPlayer"
}
# Start building the page output
st.title('Stream Check')
st.subheader('Select the subscription service(s) of interest to you, and then enter keyword(s) below to search for films streaming in the UK.')
filmtitle = st.text_input("Enter a film title (or part of)")
# Parameters
url = "https://streaming-availability.p.rapidapi.com/v2/search/title"
querystring = {"title": filmtitle,"country":"gb","type":"movie","output_language":"en"}
headers = {
"X-RapidAPI-Key": "c5e3589ba0mshb0e69d4356a72a3p12b8d4jsn44fbfcc97ec3",
"X-RapidAPI-Host": "streaming-availability.p.rapidapi.com"
}
# Functions
@st.cache_data
def get_data(theurl, theheaders, thequerystring):
response = requests.request("GET", theurl, headers=theheaders, params=thequerystring)
return response
def build_service_indicator(service_name, text_to_display, class_name, chosen):
service_class_name = ""
if chosen:
service_class_name = " " + class_name
return "<span class='btn btn-sm btn-secondary" + service_class_name + "'>" + text_to_display + "</span>"
# Sidebar - show all services for user to select from
show_service = {}
with st.sidebar:
st.header("Choose your service(s)")
for service in services_of_interest:
show_service[service] = st.checkbox(f'{services_of_interest[service]}')
# The data to use
if filmtitle:
data = get_data(url, headers, querystring)
result = json.loads(data.content)
st.markdown("<hr /><h3>UK results...</h3>", unsafe_allow_html=True)
non_uk_data = ""
not_on_selected_platforms_data = ""
for obj in result["result"]:
year = "Unknown"
title = obj["title"]
if("year" in obj) : year = obj["year"]
synopsis = obj["overview"] or "Synopsis unavailable"
actor_output = " "
if obj["cast"]:
for actor in obj["cast"]:
actor_output += "<div class='actor'>" + actor + "</div>"
if "original" in obj["posterURLs"]:
image_url = obj["posterURLs"]["original"]
canaccess = False
has_subscription = False
if "gb" in obj["streamingInfo"]:
canaccess = True
services = obj["streamingInfo"]["gb"]
services_data = ""
for s in services:
service_name = services_of_interest.get(s, s) # Check if service is in those of interest.
services_data += build_service_indicator(s, service_name, s, show_service.get(s, False))
if show_service.get(s, False):
has_subscription = True
# See if we have matched at least one service of interest
if not has_subscription:
not_on_selected_platforms_data += "<div class='card-header'><h4>" + title + "</h4><span>" + services_data + "</span></div>"
else:
non_uk_data += "<div class='card-header'><h4>{0}</h4><span class='btn btn-sm btn-primary'>{1}</span></div>".format(title, year)
if canaccess & has_subscription:
st.markdown(f"""
<div class="card">
<div class="card-header">
<h4>{title}</h4>
<span class="badge badge-primary">{year}</span>
</div>
<div class="card-body">
<div class="card-leftcol">
<img class="poster" src={image_url}></td>
</div>
<div class="card-rightcol">
<div class="synopsis">
{synopsis}
</div>
<div class="cast">
{actor_output}
</div>
</div>
</div>
<div class="card-footer">
{services_data}
</div>
</div>
""", unsafe_allow_html=True)
# Out of the loop over the data object
if not_on_selected_platforms_data:
st.markdown(f"""
###
<h3>Not on a chosen service...</h3>
{not_on_selected_platforms_data}
""", unsafe_allow_html=True)
#if non_uk_data:
# st.markdown(f"""
# ###
# <h3>Non-UK streaming services...</h3>
# {non_uk_data}
# """, unsafe_allow_html=True)