Electronic-Mango / simple-justwatch-python-api

A simple (and unofficial) JustWatch Python API.
https://electronic-mango.github.io/simple-justwatch-python-api/
GNU General Public License v3.0
29 stars 6 forks source link

[feature] search function by URL? #9

Open b-gonzalez opened 1 month ago

b-gonzalez commented 1 month ago

In my current process, I search for a movie on JustWatch by using the title. If there are multiple titles that match this name, multiple media entries are returned. To find the media entry that I'm looking for, I can use the URL to filter for the media entry that contains the matching URL. But I was wondering if the search functionality could be updated to search by URLs. This would remove my need to loop through media entries to find the matching one.

dseomn commented 1 month ago

Do you want to search URLs, or just get the info for what's at a specific URL? If the latter, here's some code that does it in case that helps: https://github.com/dseomn/rock-paper-sand/blob/942db488291dfec9f91c7f3365dd92d7d3fbbd9a/rock_paper_sand/justwatch.py#L239-L245

b-gonzalez commented 1 month ago

I wanted to be able to search by URLs. I was able to get the code that you linked to working. Although I had to implement all of the code in the query_document variable as opposed to just the snippet you linked to. The data in the JSON returned is data like this:

{
  "data": {
    "urlV2": {
      "node": {
        "__typename": "Movie",
        "id": "tm10",
        "offers": [
          {
            "monetizationType": "FLATRATE",
            "availableToTime": "2024-08-01T07:00:00Z",
            "availableFromTime": null,
            "package": {
              "clearName": "Netflix",
              "technicalName": "netflix"
            }
          },

But I'm trying to get data like this:

            "content": {
              "title": "The Matrix",
              "fullPath": "/us/movie/the-matrix",
              "originalReleaseYear": 1999,
              "originalReleaseDate": "1999-03-31",
              "runtime": 136,
              "shortDescription": "Set in the 22nd century, The Matrix tells the story of a computer hacker who joins a group of underground insurgents fighting the vast and powerful computers who now rule the earth.",
              "posterUrl": "/poster/140351543/s718/the-matrix.jpg"

And like this:

{
  "monetizationType": "RENT",
  "presentationType": "HD",
  "retailPrice": "$3.99",
  "retailPriceValue": 3.99,
  "currency": "USD",
  "type": "STANDARD",
  "package": {
    "clearName": "Apple TV",
    "technicalName": "itunes",
  }

using just the URL

dseomn commented 1 month ago

At a glance, that looks like it's all from the same thing being returned, you'd just have to request the fields you want.

b-gonzalez commented 1 month ago

Ah okay. Thanks for the heads up. I'm not super familiar with GraphQL. After some trial and error it looks like I was able to merge the code in both codebases to pull most of the data I want by the URL. I created a new variable like this:

  _GRAPHQL_SEARCH_URL_QUERY = """
  fragment Movie on MovieOrShow {
      __typename
      id
      content(country: $country, language: $language) {
        title
        fullPath
        originalReleaseYear
        originalReleaseDate
        runtime
        shortDescription
        genres {
          shortName
          __typename
        }
        externalIds {
          imdbId
          __typename
        }
        __typename
      }
      offers(country: $country, platform: WEB) {
        monetizationType
        presentationType
        retailPriceValue
        currency
        lastChangeRetailPriceValue
        type
        package {
          id
          packageId
          clearName
          technicalName
          __typename
        }
      }
  }

  fragment Node on Node {
      __typename
      id
      ...Movie
  }

  query GetNodeByUrlPath($urlPath: String!, $country: Country!, $language: Language!) {
      urlV2(fullPath: $urlPath) {
          node {
              ...Node
          }
      }
  }
  """

EDIT: It looks like I was able to get a bit farther. I'm now able to pull most of the movie data. I needed to update GetNodeByUrlPath to take a $language parameter. So I think after a few more updates I should be able to pull most of the data I need.