I'm sorry if I have caused any inconvenience, but this project is an assignment for my school's computing course, and I checked with my teacher but she couldn't find the root of the problem. I have edited my code based on your suggestions, but am still facing the same problem.
Here is the code that is supposed to produce an individualised webpage for each movie, with any reviews to be on it(if there are)
````
@app.route("/movies/<movie>")
def movie(movie):
lol = movie.split(',')
movie_title = lol[0]
res = requests.get("
http://www.omdbapi.com/", params={"apikey": "c2c76d64", "t": movie_title, "plot": "full"})
omdb_data = res.json()
ratings_list = omdb_data['Ratings']
check_for_review_statement = sqlalchemy.text('SELECT * FROM reviews WHERE movie = :movie')
check_for_review = engine.execute(check_for_review_statement, movie=movie_title).fetchall()
print(check_for_review)
if res.status_code == 200:
if len(check_for_review) != 0:
return render_template("movie_individual.html", movie=lol, omdb_data=omdb_data, ratings_list=ratings_list, check_for_review=check_for_review)
else:
return render_template("movie_no_review.html", movie=lol, omdb_data=omdb_data, ratings_list=ratings_list)
else:
if len(check_for_review) != 0:
return render_template("movie_noomdbdata.html", movie=lol, check_for_review=check_for_review)
else:
return render_template("movie_gotnothing.html", movie=lol, res=res.json())
````
Below is now the new api code:
````
@app.route("/api/<imdb_id>")
def api(imdb_id):
check_for_api_statement = sqlalchemy.text('SELECT title, year, "imdbRating" FROM movies WHERE "imdbID" = :imdb_id')
check_for_api_unsplitted = engine.execute(check_for_api_statement, imdb_id=imdb_id).fetchall()
res = requests.get("
http://www.omdbapi.com/", params={"apikey": "c2c76d64", "i": imdb_id, "plot": "full"})
omdb_data = res.json()
check_for_api = check_for_api_unsplitted[0]
title = check_for_api["title"]
title_pattern = "(" + title
year = check_for_api[1]
imdb_id = imdb_id
imdbrating = check_for_api[2]
director = omdb_data['Director']
actors = omdb_data['Actors']
check_for_reviews_statement = sqlalchemy.text('SELECT * FROM reviews WHERE movie = :movie_title')
check_for_reviews = engine.execute(check_for_reviews_statement, movie_title=title_pattern).fetchall()
review_count = len(check_for_reviews)
print(check_for_reviews)
if len(check_for_reviews) != 0:
score = float(check_for_reviews[4])
average_score = score/review_count
else:
average_score = "N.A"
a = {"title":title,"year":year,"imdb_id":imdb_id,"director":director,"actors":actors,"imdb_rating":imdbrating,"review_count":review_count,"average_score":average_score}
apijson = json.dumps(a, cls=CustomJsonEncoder)
if len(check_for_api) != 0:
return render_template("api.html", apijson=apijson)
else:
return render_template("error2.html", message="No such movie.")
````
Here are the logs for the api:
````
127.0.0.1 - - [05/Jul/2019 14:42:12] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [05/Jul/2019 14:42:14] "GET /favicon.ico HTTP/1.1" 404 -
2019-07-05 14:42:25,098 INFO sqlalchemy.engine.base.Engine select version()
2019-07-05 14:42:25,098 INFO sqlalchemy.engine.base.Engine {}
2019-07-05 14:42:26,020 DEBUG sqlalchemy.engine.base.Engine Col ('version',)
2019-07-05 14:42:26,020 DEBUG sqlalchemy.engine.base.Engine Row ('PostgreSQL 11.4 (Ubuntu 11.4-1.pgdg16.04+1
) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609, 64-bit',)
2019-07-05 14:42:26,021 INFO sqlalchemy.engine.base.Engine select current_schema()
2019-07-05 14:42:26,021 INFO sqlalchemy.engine.base.Engine {}
2019-07-05 14:42:26,363 DEBUG sqlalchemy.engine.base.Engine Col ('current_schema',)
2019-07-05 14:42:26,363 DEBUG sqlalchemy.engine.base.Engine Row ('public',)
2019-07-05 14:42:26,738 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60))
AS anon_1
2019-07-05 14:42:26,738 INFO sqlalchemy.engine.base.Engine {}
2019-07-05 14:42:27,146 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60)
) AS anon_1
2019-07-05 14:42:27,146 INFO sqlalchemy.engine.base.Engine {}
2019-07-05 14:42:27,548 INFO sqlalchemy.engine.base.Engine show standard_conforming_strings
2019-07-05 14:42:27,548 INFO sqlalchemy.engine.base.Engine {}
2019-07-05 14:42:28,295 DEBUG sqlalchemy.engine.base.Engine Col ('standard_conforming_strings',)
2019-07-05 14:42:28,296 DEBUG sqlalchemy.engine.base.Engine Row ('on',)
2019-07-05 14:42:29,400 INFO sqlalchemy.engine.base.Engine SELECT title, year, "imdbRating" FROM movies WHER
E "imdbID" = %(imdb_id)s
2019-07-05 14:42:29,400 INFO sqlalchemy.engine.base.Engine {'imdb_id': 'tt1490017'}
2019-07-05 14:42:30,683 DEBUG sqlalchemy.engine.base.Engine Col ('title', 'year', 'imdbRating')
2019-07-05 14:42:30,683 DEBUG sqlalchemy.engine.base.Engine Row ('The Lego Movie', 2014, Decimal('7.8'))
2019-07-05 14:42:31,248 INFO sqlalchemy.engine.base.Engine SELECT * FROM reviews WHERE movie = %(movie_title
)s
2019-07-05 14:42:31,248 INFO sqlalchemy.engine.base.Engine {'movie_title': '(The Lego Movie'}
2019-07-05 14:42:32,164 DEBUG sqlalchemy.engine.base.Engine Col ('movie', 'rating', 'username', 'review')
[]
127.0.0.1 - - [05/Jul/2019 14:42:32] "GET /api/tt1490017 HTTP/1.1" 200 -
````
And here are the logs for the individualised movie:
2019-07-05 14:44:51,837 INFO sqlalchemy.engine.base.Engine select version()
2019-07-05 14:44:51,837 INFO sqlalchemy.engine.base.Engine {}
2019-07-05 14:44:52,656 DEBUG sqlalchemy.engine.base.Engine Col ('version',)
2019-07-05 14:44:52,656 DEBUG sqlalchemy.engine.base.Engine Row ('PostgreSQL 11.4 (Ubuntu 11.4-1.pgdg16.04+1
) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609, 64-bit',)
2019-07-05 14:44:52,657 INFO sqlalchemy.engine.base.Engine select current_schema()
2019-07-05 14:44:52,658 INFO sqlalchemy.engine.base.Engine {}
2019-07-05 14:44:53,069 DEBUG sqlalchemy.engine.base.Engine Col ('current_schema',)
2019-07-05 14:44:53,069 DEBUG sqlalchemy.engine.base.Engine Row ('public',)
2019-07-05 14:44:53,405 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60))
AS anon_1
2019-07-05 14:44:53,405 INFO sqlalchemy.engine.base.Engine {}
2019-07-05 14:44:53,748 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60)
) AS anon_1
2019-07-05 14:44:53,748 INFO sqlalchemy.engine.base.Engine {}
2019-07-05 14:44:54,089 INFO sqlalchemy.engine.base.Engine show standard_conforming_strings
2019-07-05 14:44:54,090 INFO sqlalchemy.engine.base.Engine {}
2019-07-05 14:44:54,422 DEBUG sqlalchemy.engine.base.Engine Col ('standard_conforming_strings',)
2019-07-05 14:44:54,422 DEBUG sqlalchemy.engine.base.Engine Row ('on',)
2019-07-05 14:44:54,807 INFO sqlalchemy.engine.base.Engine SELECT * FROM reviews WHERE movie = %(movie)s
2019-07-05 14:44:54,807 INFO sqlalchemy.engine.base.Engine {'movie': "('The Lego Movie'"}
2019-07-05 14:44:55,626 DEBUG sqlalchemy.engine.base.Engine Col ('movie', 'rating', 'username', 'review')
2019-07-05 14:44:55,626 DEBUG sqlalchemy.engine.base.Engine Row ("('The Lego Movie'", Decimal('1'), 'sms', '
a very cool movie')
2019-07-05 14:44:55,626 DEBUG sqlalchemy.engine.base.Engine Row ("('The Lego Movie'", Decimal('8'), 'was', '
cool and good')
[("('The Lego Movie'", Decimal('1'), 'sms', 'a very cool movie'), ("('The Lego Movie'", Decimal('8'), 'was',
'cool and good')]
127.0.0.1 - - [05/Jul/2019 14:44:55] "GET /movies/%28%27The%20Lego%20Movie%27%2C%202014%2C%20100%2C%20%27tt1
490017%27%2C%20Decimal%28%277.8%27%29%29 HTTP/1.1" 200 -
````
Thanks,
Cravan
To view this discussion on the web visit
https://groups.google.com/d/msgid/sqlalchemy/CAFHwexeD9g7PdJU37G%3DewnFbYOFY35LkkRd2i2dZU_b2F81O%2BA%40mail.gmail.com.