GraphQL Handler Class Finished (Recap)
A quick recap of what my project structure will be like, and what GraphQL does, since I don’t think I stated that too clearly…
How This Should Theoretically Work:
This is a web scraper that gets movie showtimes over different days from https://amctheatres.com/ and renders them as a calendar. But before you can even get the showtimes, you need the theater to get showtimes from (duh). Thankfully, I don’t have to rebuild this from scratch since I found AMC’s graphQL endpoint and their query for fetching theater locations by search term. (If I didn’t find this I would have likely given up on the project.)
Since the query is already pretty much built for me, I can recreate a theater lookup modal, similar to AMC’s, so the user can choose a nearby theater. Using the theater’s backend information returned in the query,
I can then construct a URL for that theater’s showtimes page, then scrape it.
Currently I’m just making the backend, I’ll move to the UI after I’m finished with making the backend components.
My Query Handler
So I decided to make the query handling process object-oriented because I didn’t want a bunch of variables holding long strings laying around. Before I explain how the query handler works, I need to explain how the query itself works (I’ll make this quick I promise).
Query Structure
It basically takes a search query (search term) and looks up matching locations. It returns a list of locations (which can be cities or theaters), the length of which is indicated by a resultCount parameter, with each location also returning a list of theaters around it. The search query and result count are given in a separate variables json. The query structure basically looks like this (simplified version):
query QueryName($query: String, $resultCount: Int = 10) {
locations(query: $query, first: $resultCount) {
[here you put in attributes you want returned. ex:]
title
latitude
longitude
theatres { [this is you requesting each location's theatres and their info]
[here is where you put the THEATRES' attributes you want. ex:]
name
theatreId
city
attributes(type: FILTERS, first: 4) { [and this is if you want the list of theatres' features, like available formats, type of seating, etc.]
name [just asking for the attributes' names]
}
}
}
}
The Query Handler
As you can see, all of this is incredibly painful. So I made a class that will take lists of attributes you want (location_fields, theatre_fields, attributes_fields) and construct a query, also with a method to send a request with a search term. All of this is shown in the video.
Where to next?
Now it’s time to build the url constructor and web scraper 😩 wish me luck guys
Comments 0
No comments yet. Be the first!
Sign in to join the conversation.