Reviews query throwing up blank list

41 views
Skip to first unread message

Cravan

unread,
Jul 2, 2019, 10:33:02 AM7/2/19
to sqlalchemy

Hi all,

                One of the other tasks for my movie review assignment is that I have to create api access to my website, which should return a json response including a review count. However, I have confirmed that I have entered a review for a specific movie and it prompts out in my html, but apparently when I try to print out the json I get 0 reviews for my review count. Can someone help me troubleshoot? Sorry if it sounds unclear.

````

@app.route("/api/<imdb_id>")

def api(imdb_id):

    check_for_api_statement = sqlalchemy.text('SELECT * 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 = list(check_for_api_unsplitted[0])

    title_unsplitted = check_for_api[0]

    title = check_for_api[0]

    year = check_for_api[1]

    imdb_id = imdb_id

    imdbrating = check_for_api[4]

    director = omdb_data['Director']

    actors = omdb_data['Actors']

    check_for_reviews_statement = sqlalchemy.text('SELECT * FROM reviews WHERE movie = :movie_title')

    check_for_reviews_unsplitted = engine.execute(check_for_reviews_statement, movie_title=title).fetchall()

    review_count = len(check_for_reviews_unsplitted)

    print(check_for_reviews_unsplitted)    #this returns a blank list for some strange reason

    if len(check_for_reviews_unsplitted) != 0:

        check_for_reviews = check_for_reviews_unsplitted.split(',')

        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)

    print (apijson)

    if len(check_for_api) != 0:

        return render_template("api.html", apijson=apijson)

    else:

        return render_template("error2.html", message="No such movie.")

Simon King

unread,
Jul 2, 2019, 12:07:59 PM7/2/19
to sqlal...@googlegroups.com
You might want to try turning on debug logging for your SQL statements
by passing echo="debug" when creating your engine:

https://docs.sqlalchemy.org/en/13/core/engines.html#sqlalchemy.create_engine

Amongst other things, this will show the values of parameters that you
are passing to the database.

I'm not sure what your problem is, but I notice that you are using
"SELECT * FROM movies" to load your rows, and then you are assuming
that the columns you get back are in a certain order. (You assume that
the first column is title, the second is year, and the fifth is
imdbrating). It's possible that the columns are coming back in a
different order, and so the value you think is the title is actually
something else.

It would be better to explicitly list the columns you want, something like:

SELECT title, year, imdbrating
FROM movies
WHERE "imdbID" = :imdb_id

Alternatively, the rows that you get back from fetchall() actually
know which columns they came from, so you could do this:

# don't call list() here:
check_for_api = check_for_api_unsplitted[0]

# use dictionary-style access to get the individual values
title = check_for_api["title"]

Simon
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
> ---
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
> To post to this group, send email to sqlal...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/7DB000C8-1EBC-48AF-A24F-202196C743CF%40gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Cravan

unread,
Jul 3, 2019, 6:54:04 AM7/3/19
to sqlal...@googlegroups.com
Hi Simon,
I always turn on debug mode using export FLASK_DEBUG=1 in the shell, but no error pops out, so I have no idea whether that is similar. I tried echo="debug", but didn’t really understand the results, so I am confused as to how to edit my code. I have used both edits as suggested, and this is what was produced:
````
2019-07-03 18:49:43,100 INFO sqlalchemy.engine.base.Engine select version()
2019-07-03 18:49:43,100 INFO sqlalchemy.engine.base.Engine {}
2019-07-03 18:49:43,764 DEBUG sqlalchemy.engine.base.Engine Col ('version',)
2019-07-03 18:49:43,764 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-03 18:49:43,765 INFO sqlalchemy.engine.base.Engine select current_schema()
2019-07-03 18:49:43,765 INFO sqlalchemy.engine.base.Engine {}
2019-07-03 18:49:44,109 DEBUG sqlalchemy.engine.base.Engine Col ('current_schema',)
2019-07-03 18:49:44,110 DEBUG sqlalchemy.engine.base.Engine Row ('public',)
2019-07-03 18:49:44,534 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60))
AS anon_1
2019-07-03 18:49:44,534 INFO sqlalchemy.engine.base.Engine {}
2019-07-03 18:49:44,947 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60)
) AS anon_1
2019-07-03 18:49:44,947 INFO sqlalchemy.engine.base.Engine {}
2019-07-03 18:49:45,353 INFO sqlalchemy.engine.base.Engine show standard_conforming_strings
2019-07-03 18:49:45,353 INFO sqlalchemy.engine.base.Engine {}
2019-07-03 18:49:45,762 DEBUG sqlalchemy.engine.base.Engine Col ('standard_conforming_strings',)
2019-07-03 18:49:45,762 DEBUG sqlalchemy.engine.base.Engine Row ('on',)
2019-07-03 18:49:46,173 INFO sqlalchemy.engine.base.Engine SELECT title, year, "imdbRating" FROM movies WHER
E "imdbID" = %(imdb_id)s
2019-07-03 18:49:46,173 INFO sqlalchemy.engine.base.Engine {'imdb_id': 'tt1490017'}
2019-07-03 18:49:46,991 DEBUG sqlalchemy.engine.base.Engine Col ('title', 'year', 'imdbRating')
2019-07-03 18:49:46,991 DEBUG sqlalchemy.engine.base.Engine Row ('The Lego Movie', 2014, Decimal('7.8'))
('The Lego Movie', 2014, Decimal('7.8'))
2019-07-03 18:49:47,862 INFO sqlalchemy.engine.base.Engine SELECT * FROM reviews WHERE movie = %(movie_title
)s
2019-07-03 18:49:47,863 INFO sqlalchemy.engine.base.Engine {'movie_title': 'The Lego Movie'}
2019-07-03 18:49:48,699 DEBUG sqlalchemy.engine.base.Engine Col ('movie', 'rating', 'username', 'review')
[]
{"title": "The Lego Movie", "year": 2014, "imdb_id": "tt1490017", "director": "Phil Lord, Christopher Miller
", "actors": "Will Arnett, Elizabeth Banks, Craig Berry, Alison Brie", "imdb_rating": 7.8, "review_count": 0
, "average_score": "N.A"}
127.0.0.1 - - [03/Jul/2019 18:49:49] "GET /api/tt1490017 HTTP/1.1" 200 -
````
Also, I used three print statements,
````
print(check_for_api)
......#some code(same as before)
print(check_for_reviews_unsplitted)
......#some code again(same as before)
print (apijson)
````
Thanks,
Cravan
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CAFHwexfVMZTVX6hKDvxgRFG0Wrc%3DhSfwU05XQGm%2B7gsRTtR_Cw%40mail.gmail.com.

Simon King

unread,
Jul 3, 2019, 10:13:54 AM7/3/19
to sqlal...@googlegroups.com
On Wed, Jul 3, 2019 at 11:54 AM Cravan <crav...@gmail.com> wrote:
>
> Hi Simon,
> I always turn on debug mode using export FLASK_DEBUG=1 in the shell, but no error pops out, so I have no idea whether that is similar. I tried echo="debug", but didn’t really understand the results, so I am confused as to how to edit my code. I have used both edits as suggested, and this is what was produced:
> ````
> 2019-07-03 18:49:43,100 INFO sqlalchemy.engine.base.Engine select version()
> 2019-07-03 18:49:43,100 INFO sqlalchemy.engine.base.Engine {}
> 2019-07-03 18:49:43,764 DEBUG sqlalchemy.engine.base.Engine Col ('version',)
> 2019-07-03 18:49:43,764 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-03 18:49:43,765 INFO sqlalchemy.engine.base.Engine select current_schema()
> 2019-07-03 18:49:43,765 INFO sqlalchemy.engine.base.Engine {}
> 2019-07-03 18:49:44,109 DEBUG sqlalchemy.engine.base.Engine Col ('current_schema',)
> 2019-07-03 18:49:44,110 DEBUG sqlalchemy.engine.base.Engine Row ('public',)
> 2019-07-03 18:49:44,534 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60))
> AS anon_1
> 2019-07-03 18:49:44,534 INFO sqlalchemy.engine.base.Engine {}
> 2019-07-03 18:49:44,947 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60)
> ) AS anon_1
> 2019-07-03 18:49:44,947 INFO sqlalchemy.engine.base.Engine {}
> 2019-07-03 18:49:45,353 INFO sqlalchemy.engine.base.Engine show standard_conforming_strings
> 2019-07-03 18:49:45,353 INFO sqlalchemy.engine.base.Engine {}
> 2019-07-03 18:49:45,762 DEBUG sqlalchemy.engine.base.Engine Col ('standard_conforming_strings',)
> 2019-07-03 18:49:45,762 DEBUG sqlalchemy.engine.base.Engine Row ('on',)

Everything above this point is routine stuff that SQLAlchemy does to
figure out what verison of Postgresql it's talking to and what options
it is running with. You can safely ignore it.

The next line is the first query that you've run:
> 2019-07-03 18:49:46,173 INFO sqlalchemy.engine.base.Engine SELECT title, year, "imdbRating" FROM movies WHER
> E "imdbID" = %(imdb_id)s

These are the parameters that you passed to the query (in this case,
just the imdb_id):
> 2019-07-03 18:49:46,173 INFO sqlalchemy.engine.base.Engine {'imdb_id': 'tt1490017'}

The "Col" line tells you the names of the columns in the result set:
> 2019-07-03 18:49:46,991 DEBUG sqlalchemy.engine.base.Engine Col ('title', 'year', 'imdbRating')

...and then you will normally see a "Row" line for each row in the
result. In this case your query returned a single row. The elements of
the row tuple correspond to the columns from the Col line.
> 2019-07-03 18:49:46,991 DEBUG sqlalchemy.engine.base.Engine Row ('The Lego Movie', 2014, Decimal('7.8'))

I assume this is from your "print(check_for_api)" statement:
> ('The Lego Movie', 2014, Decimal('7.8'))

Here's your next query:
> 2019-07-03 18:49:47,862 INFO sqlalchemy.engine.base.Engine SELECT * FROM reviews WHERE movie = %(movie_title
> )s

...and the parameters for that query:
> 2019-07-03 18:49:47,863 INFO sqlalchemy.engine.base.Engine {'movie_title': 'The Lego Movie'}

The column names in the result set:
> 2019-07-03 18:49:48,699 DEBUG sqlalchemy.engine.base.Engine Col ('movie', 'rating', 'username', 'review')

...but then there are no Row lines, so the query didn't return any
rows. In other words, there are no rows in the "reviews" table where
the "movie" column equals "The Lego Movie".

You said that you've confirmed that you've entered the review. How
have you confirmed that?

Simon
> To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/FD18A0DF-70BD-4867-BA8C-A90DD76E24A3%40gmail.com.

Cravan

unread,
Jul 3, 2019, 10:16:36 AM7/3/19
to sqlal...@googlegroups.com
This is because I have a movie function that throws up any reviews; here it is:
````
@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()
if res.status_code == 200:
if len(check_for_review) != 0:
check_for_review_splitted = str(check_for_review).split(',')
return render_template("movie_individual.html", movie=lol, check_for_review=check_for_review_splitted, omdb_data=omdb_data, ratings_list=ratings_list)
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:
check_for_review_splitted = str(check_for_review).split(',')
return render_template("movie_noomdbdata.html", movie=lol, check_for_review=check_for_review_splitted)
else:
return render_template("movie_gotnothing.html", movie=lol, res=res.json())
````
And it prints out the review.Here's a pic attached to this email.
Cravan
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CAFHwexc6%2BT%2BzNK%3DR2WPefSgGVkYnC8CyjGkF_ydN3HEFp_ggmg%40mail.gmail.com.
Screen Shot 2019-07-03 at 10.15.55 PM.png

Simon King

unread,
Jul 3, 2019, 10:20:12 AM7/3/19
to sqlal...@googlegroups.com
Is that screenshot from a review for The Lego Movie? Can you show the
log output when you generate that page?
> To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/B6DBDE22-5A75-4DE6-B69C-346E5E597F9E%40gmail.com.

Cravan

unread,
Jul 3, 2019, 10:23:02 AM7/3/19
to sqlal...@googlegroups.com
Yes, it is.
````
2019-07-03 22:21:43,914 INFO sqlalchemy.engine.base.Engine select version()
2019-07-03 22:21:43,915 INFO sqlalchemy.engine.base.Engine {}
2019-07-03 22:21:44,608 DEBUG sqlalchemy.engine.base.Engine Col ('version',)
2019-07-03 22:21:44,608 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-03 22:21:44,609 INFO sqlalchemy.engine.base.Engine select current_schema()
2019-07-03 22:21:44,610 INFO sqlalchemy.engine.base.Engine {}
2019-07-03 22:21:45,020 DEBUG sqlalchemy.engine.base.Engine Col ('current_schema',)
2019-07-03 22:21:45,021 DEBUG sqlalchemy.engine.base.Engine Row ('public',)
2019-07-03 22:21:45,346 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60))
AS anon_1
2019-07-03 22:21:45,346 INFO sqlalchemy.engine.base.Engine {}
2019-07-03 22:21:45,670 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60)
) AS anon_1
2019-07-03 22:21:45,671 INFO sqlalchemy.engine.base.Engine {}
2019-07-03 22:21:46,000 INFO sqlalchemy.engine.base.Engine show standard_conforming_strings
2019-07-03 22:21:46,000 INFO sqlalchemy.engine.base.Engine {}
2019-07-03 22:21:46,348 DEBUG sqlalchemy.engine.base.Engine Col ('standard_conforming_strings',)
2019-07-03 22:21:46,348 DEBUG sqlalchemy.engine.base.Engine Row ('on',)
2019-07-03 22:21:46,676 INFO sqlalchemy.engine.base.Engine SELECT * FROM reviews WHERE movie = %(movie)s
2019-07-03 22:21:46,676 INFO sqlalchemy.engine.base.Engine {'movie': "('The Lego Movie'"}
2019-07-03 22:21:47,375 DEBUG sqlalchemy.engine.base.Engine Col ('movie', 'rating', 'username', 'review')
2019-07-03 22:21:47,376 DEBUG sqlalchemy.engine.base.Engine Row ("('The Lego Movie'", Decimal('1'), 'sms', '
a very cool movie')
127.0.0.1 - - [03/Jul/2019 22:21:47] "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 -
````
Cravan
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CAFHwexcoCcw1ggw3jcEiVomApD19BaLVKcVsNLU8DSOFa0%2BEzA%40mail.gmail.com.

Simon King

unread,
Jul 3, 2019, 10:43:43 AM7/3/19
to sqlal...@googlegroups.com
Look more closely here:

2019-07-03 22:21:46,676 INFO sqlalchemy.engine.base.Engine SELECT *
FROM reviews WHERE movie = %(movie)s
2019-07-03 22:21:46,676 INFO sqlalchemy.engine.base.Engine {'movie':
"('The Lego Movie'"}
2019-07-03 22:21:47,375 DEBUG sqlalchemy.engine.base.Engine Col
('movie', 'rating', 'username', 'review')
2019-07-03 22:21:47,376 DEBUG sqlalchemy.engine.base.Engine Row
("('The Lego Movie'", Decimal('1'), 'sms', '
a very cool movie')

The value in the "movie" column is literally:

('The Lego Movie'

ie. "open parenthesis, single quote, The Lego Movie, single quote"

Whatever you're doing to insert data into the database is broken.

Things like this also raise red flags:

check_for_review = engine.execute(check_for_review_statement,
movie=movie_title).fetchall()
check_for_review_splitted = str(check_for_review).split(',')

You're getting a list of rows, converting them to a string, and then
splitting that string on commas. This will break badly in anything but
the simplest of cases.

Simon
> To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/2F82992D-69A9-496E-A09D-881F3112EA83%40gmail.com.

Cravan

unread,
Jul 3, 2019, 10:48:19 AM7/3/19
to sqlal...@googlegroups.com
How do you suggest I change it then? I import my values are from a csv sheet as per the task requirements. As for the other problem, how should I change it? And may I know why will it not work/break badly?
Cravan
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CAFHwexe2LYymfKO%3DZS1uEFp_0ozJnscghjvfskWw7soah5gq1Q%40mail.gmail.com.
import.py

Simon King

unread,
Jul 4, 2019, 5:25:03 AM7/4/19
to sqlal...@googlegroups.com
The corrupted value is in the "reviews" table. Are you populating that
from a CSV file as well? If so, please show the code.

As for fixing the other problem, I don't really understand what the
code is trying to do, so I can't give you an exact solution. But let's
have a look in more detail:

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()
if res.status_code == 200:
if len(check_for_review) != 0:
check_for_review_splitted = str(check_for_review).split(',')

So "check_for_review" is a list of rows. Each row is an SQLAlchemy
RowProxy object that behaves a bit like a mix between a tuple and a
dictionary. These objects are properly structured, so that you can
access the rows and columns easily:

row = check_for_review[0]
rating = row["rating"]

But you're calling "str(check_for_review)". That turns the nice
structured set of objects into a single string, looking something like
this:

"[('Spider-Man', '10', 'peterp', 'Amazing, Incredible')]"

Then you're turning that string back into a list by splitting it on
commas. The individual items in that list will now be:

"[('Spider-Man'"
" '10'"
" 'peterp'"
" 'Amazing"
" Incredible')]"

As you can see, you've got unwanted brackets and quotes, and because
the review itself contained a comma, you've split that up as well.

Simon
> To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/C2E9AF04-9E54-4A08-871D-60B8AE4491C4%40gmail.com.

Cravan

unread,
Jul 4, 2019, 8:10:22 AM7/4/19
to sqlal...@googlegroups.com
Here's my entire code. Some parts I messed up the get and post methods, will work on that later on. Erm... how do I split the list without breaking up the review then? Or in the first place, is check_for_review a list of dictionaries?
Cravan
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CAFHwexe2BNUfjX%3D8e-jhxejMD2V%2BT_QmOtr%3DS9tpQ1wD4Y0XJA%40mail.gmail.com.
import.py
error3.html
index.html
layout.html
movie_gotnothing.html
movie_individual.html
movie_no_review.html
movie_noomdbdata.html
movie_specific.html
movies.html
search.html
main.py
movies.csv
movies.sql
users.sql
reviews.sql
api.html
error.html
error2.html

Cravan

unread,
Jul 4, 2019, 9:37:06 AM7/4/19
to sqlal...@googlegroups.com
Also, another problem is that multiple reviews do not print out.

On 4/7/19, 5:25 PM, "Simon King" <sqlal...@googlegroups.com on behalf of si...@simonking.org.uk> wrote:

The corrupted value is in the "reviews" table. Are you populating that
from a CSV file as well? If so, please show the code.

As for fixing the other problem, I don't really understand what the
code is trying to do, so I can't give you an exact solution. But let's
have a look in more detail:



This part is supposed to check for all reviews and print it out
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CAFHwexe2BNUfjX%3D8e-jhxejMD2V%2BT_QmOtr%3DS9tpQ1wD4Y0XJA%40mail.gmail.com.

Simon King

unread,
Jul 4, 2019, 1:08:30 PM7/4/19
to sqlal...@googlegroups.com
I'm afraid I can't really help you debug your whole application like
this - I can only try to answer specific questions.

If you haven't already done so, I think it would help you to find a
program to explore the data in your database. Either the psql command
line tool that comes with postgresql, pgcli (https://www.pgcli.com/),
or one of the GUIs listed on
https://wiki.postgresql.org/wiki/Community_Guide_to_PostgreSQL_GUI_Tools.
Then you can test out the queries that you're trying to run from
SQLAlchemy. It would hopefully be much more obvious to you when you've
inserted incorrect data.

Who has given you this assignment? Is there anyone else that can help you?

Simon
> To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CF1A5436-3672-4E9A-83BF-2506A19836CC%40gmail.com.

Cravan

unread,
Jul 5, 2019, 2:46:27 AM7/5/19
to sqlal...@googlegroups.com
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.

Simon King

unread,
Jul 5, 2019, 5:09:47 AM7/5/19
to sqlal...@googlegroups.com
I think you're trying to do too much in one go. You need to
concentrate on getting a single task at a time working correctly.

For example, in your api() function you've got this:

> title = check_for_api["title"]
> title_pattern = "(" + title

Why are you prefixing the title with "("? I assume it's because the
movie titles in your "reviews" table have been inserted incorrectly
(with extra brackets, quotes and so on). You need to fix the code that
inserts the reviews, not try to work around it by adding extra
characters when you query.

Here's another problem that shows up in the logs:

> 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 -

I don't know how you're constructing your URLs, but I think you're
expecting them to look something like "http://localhost/movies/The
Lego Movie", right? Whereas that log output was produced by accessing
this URL (once the url encoding has been removed):

http://localhost/movies/('The Lego Movie', 2014, 100, 'tt1490017',
Decimal('7.8'))

ie. you've constructed the URL using the whole row from the database,
rather than just the title.

Simon


Simon
> To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/48FD2A81-901B-4B0D-AB6F-8ABA794F9B82%40gmail.com.
Reply all
Reply to author
Forward
0 new messages