mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Sep 2003
Posts: 42
Demarko Offline OP
Ameglian cow
OP Offline
Ameglian cow
Joined: Sep 2003
Posts: 42
Hi all,

I'm looking for a working TVMaze Lookup script. Yet, I couldn't find one, so I'm trying to build a small script snippet myself.
A small script that will allow me quickly check some basic data from a given name of a tv show that a) is using TV Maze API and b) does not require any external files.

Here is my code so far:
Code
; Usage: /tv <show_name>

alias tv {
  var %show_name = $replace($1-,$chr(32),+)
  var %api_url = https://api.tvmaze.com/singlesearch/shows?q= $+ %show_name
  var %response = $urlget(%api_url)

  if (%response) {
    var %show_name = $json(%response,name)
    var %premiered = $json(%response,premiered)
    var %status = $json(%response,status)
    var %summary = $replace($remove($json(%response,summary),<[^>]*>),$chr(10),$chr(32))
    var %message = Show: %show_name | Premiered: %premiered | Status: %status | Summary: %summary
    echo -a %message
    } else {
    echo -a Show not found.
  }
}
Example:
When I type /tv american dad it generates the following url https://api.tvmaze.com/singlesearch/shows?q=american+dad
However as far as I can tell my %response is always empty (Null) - and I dont know why and how to fix that.

Who can help please? Thanks in advance! smile

Joined: Jul 2006
Posts: 4,180
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,180
Quote
Who can help please?
ChatGPT clearly couldn't do it.

Far too many misunderstanding in this code, $urlget does not return data from the website, incorrect if/else syntax, incorrect space delimited pipe '|', and incorrect assumption that mIRC has a $json built-in function, which it doesn't.


Join the channel in my signature if you want real time help, but helping you with this here... nah.


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Sep 2003
Posts: 42
Demarko Offline OP
Ameglian cow
OP Offline
Ameglian cow
Joined: Sep 2003
Posts: 42
Originally Posted by Wims
Join the channel in my signature if you want real time help, but helping you with this here... nah.
I tried the chan you meantioned, but unfortunately I can not idle there or any other chan for hours until someone may answer, so I will post my questions in here. wink



First of all, thank you for the feedback Wims. It helped me to get the first part of the script to work. That is getting data from $urlget blush

Here is the updated script snippet:
Code
; Usage: /tv <show_name>

alias tv {
  var %show_name = $replace($1-,$chr(32),+)
  noop $urlget(https://api.tvmaze.com/singlesearch/shows?q= $+ %show_name, gb, &tvdata, tvout)
}

alias tvout {
  echo -ag $bvar(&tvdata,1-).text

  ;
  ; TODO Parse and show selected informations only
  ;

}

Now I get the result of the query. The result is a JSON formatted text string and it looks something like:
{"id":215,"url":"https://www.tvmaze.com/shows/215/american-dad","name":"American Dad!","type":"Animation","language":"English","genres":["Comedy","Family"],"status":"Running","runtime":30,"averageRuntime":30,"premiered":"2005-02-06","ended":null,"officialSite":"http://www.tbs.com/shows/american-dad.html","schedule":{"time":"22:00","days":["Monday"]},"rating":{"average":7.4},"weight":98,"network":{"id":32,"name":"TBS","country":{"name":"United States","code":"US","timezone":"America/New_York"},"officialSite":"https://www.tbs.com/"},"webChannel":null,"dvdCountry":{"name":"United States","code":"US","timezone":"America/New_York"},"externals":{"tvrage":2594,"thetvdb":73141,"imdb":"tt0397306"},"image":{"medium":"https://static.tvmaze.com/uploads/images/medium_portrait/476/1191222.jpg","original":"https://static.tvmaze.com/uploads/images/original_untouched/476/1191222.jpg"},"summary":"<p>In <b>American Dad!</b>, Stan Smith leads the all-American family in this animated sitcom filled with wild and crazy extremes. Everyday life is taken to the limit as Stan applies the same drastic measures used in his job at the CIA to his home life. Driven by machismo and the American dream, he often is blind to how horribly he fails at his attempts. This father might not know best, but he never stops trying.</p>","updated":1705592851,"_links":{"self":{"href":"https://api.tvmaze.com/shows/215"},"previousepisode":{"href":"https://api.tvmaze.com/episodes/2674312"}}}

So the question that remains is how to parse selected data/nodes from a JSON formatted string in mirc please?
The goal would be to build an output string that might look something like the following:
Code
  ; Desired output example: 
  ; echo -ag Name: %json_name $chr(124) Type: %json_type $chr(124) Language: %json_language $chr(124) Genres: %json_genres $chr(124) Status: %json_status $chr(124) URL: %json_url 
  ; echo -ag Network: %json_network_name $chr(124) Country: %json_network_country_code %json_network_country_name

If someone could please tell me how to access the "network.name" and/or "network.country.code" information from there for example, I think I would be able to access the other nodes myself grin

Thanks smile

Joined: Jul 2006
Posts: 4,180
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,180
Nice.

Now you need to either use a json parser, or parse in a crude way manually with either $mid/$gettok stuff or with regex

You can find a json parser for mIRC here https://github.com/SReject/JSON-For-Mirc but you need to be familiar with the json format to use it, this will guarantee correct data all the time.
As far as making assumptions, regex can be terribly good when you know them:

//var %string {"id":215,"url":"https://www.tvmaze.com/shows/215/american-dad","name":"American Dad!","type":"Animation","language":"English","genres":["Comedy","Family"],"status":"Running","runtime":30,"averageRuntime":30,"premiered":"2005-02-06","ended":null,"officialSite":"http://www.tbs.com/shows/american-dad.html","schedule":{"time":"22:00","days":["Monday"]},"rating":{"average":7.4},"weight":98,"network":{"id":32,"name":"TBS","country":{"name":"United States","code":"US","timezone":"America/New_York"},"officialSite":"https://www.tbs.com/"},"webChannel":null,"dvdCountry":{"name":"United States","code":"US","timezone":"America/New_York"},"externals":{"tvrage":2594,"thetvdb":73141,"imdb":"tt0397306"},"image":{"medium":"https://static.tvmaze.com/uploads/images/medium_portrait/476/1191222.jpg","original":"https://static.tvmaze.com/uploads/images/original_untouched/476/1191222.jpg"},"summary":"<p>In <b>American Dad!</b>, Stan Smith leads the all-American family in this animated sitcom filled with wild and crazy extremes. Everyday life is taken to the limit as Stan applies the same drastic measures used in his job at the CIA to his home life. Driven by machismo and the American dream, he often is blind to how horribly he fails at his attempts. This father might not know best, but he never stops trying.</p>","updated":1705592851,"_links":{"self":{"href":"https://api.tvmaze.com/shows/215"},"previousepisode":{"href":"https://api.tvmaze.com/episodes/2674312"}}},%p {"id":(\d+),"url":"([^"]+)","name":"([^"]+)","type":"([^"]+)","language":"([^"]+)","genres":\[([^\]]+)\],"status":"([^"]+)","runtime":(\d+),"averageRuntime":(\d+),"premiered":"([^"]+)","ended"([^,]+),"officialSite":"([^"]+)","schedule":{([^}]+)},"rating":{([^}]+)},"weight":(\d+),"network":{"id":(\d+),"name":"([^"]+)","country":{"name":"([^"]+)","code":"([^"]+)" | noop $regex(%string,%p) | echo -sg Name: $regml(3) $(|) Type: $regml(4) $(|) Language: $regml(5) $(|) Genres: $regml(6) $(|) Status: $regml(7) $(|) Url: $regml(2) | echo -sg Network: $regml(17) $(|) Country: $regml(18) $regml(19)You can execute that in mIRC's status window But note that if the json changes, the regex could fail as it matches quite precisely.


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Sep 2003
Posts: 42
Demarko Offline OP
Ameglian cow
OP Offline
Ameglian cow
Joined: Sep 2003
Posts: 42
Originally Posted by Wims
Nice.

Now you need to either use a json parser, or parse in a crude way manually with either $mid/$gettok stuff or with regex

You can find a json parser for mIRC here https://github.com/SReject/JSON-For-Mirc but you need to be familiar with the json format to use it, this will guarantee correct data all the time.
As far as making assumptions, regex can be terribly good when you know them:

//var %string {"id":215,"url":"https://www.tvmaze.com/shows/215/american-dad","name":"American Dad!","type":"Animation","language":"English","genres":["Comedy","Family"],"status":"Running","runtime":30,"averageRuntime":30,"premiered":"2005-02-06","ended":null,"officialSite":"http://www.tbs.com/shows/american-dad.html","schedule":{"time":"22:00","days":["Monday"]},"rating":{"average":7.4},"weight":98,"network":{"id":32,"name":"TBS","country":{"name":"United States","code":"US","timezone":"America/New_York"},"officialSite":"https://www.tbs.com/"},"webChannel":null,"dvdCountry":{"name":"United States","code":"US","timezone":"America/New_York"},"externals":{"tvrage":2594,"thetvdb":73141,"imdb":"tt0397306"},"image":{"medium":"https://static.tvmaze.com/uploads/images/medium_portrait/476/1191222.jpg","original":"https://static.tvmaze.com/uploads/images/original_untouched/476/1191222.jpg"},"summary":"<p>In <b>American Dad!</b>, Stan Smith leads the all-American family in this animated sitcom filled with wild and crazy extremes. Everyday life is taken to the limit as Stan applies the same drastic measures used in his job at the CIA to his home life. Driven by machismo and the American dream, he often is blind to how horribly he fails at his attempts. This father might not know best, but he never stops trying.</p>","updated":1705592851,"_links":{"self":{"href":"https://api.tvmaze.com/shows/215"},"previousepisode":{"href":"https://api.tvmaze.com/episodes/2674312"}}},%p {"id":(\d+),"url":"([^"]+)","name":"([^"]+)","type":"([^"]+)","language":"([^"]+)","genres":\[([^\]]+)\],"status":"([^"]+)","runtime":(\d+),"averageRuntime":(\d+),"premiered":"([^"]+)","ended"([^,]+),"officialSite":"([^"]+)","schedule":{([^}]+)},"rating":{([^}]+)},"weight":(\d+),"network":{"id":(\d+),"name":"([^"]+)","country":{"name":"([^"]+)","code":"([^"]+)" | noop $regex(%string,%p) | echo -sg Name: $regml(3) $(|) Type: $regml(4) $(|) Language: $regml(5) $(|) Genres: $regml(6) $(|) Status: $regml(7) $(|) Url: $regml(2) | echo -sg Network: $regml(17) $(|) Country: $regml(18) $regml(19)You can execute that in mIRC's status window But note that if the json changes, the regex could fail as it matches quite precisely.
Thanks! That looks good. smile

However there is a small problem with the given regex and that one is that it wont work for example for shows where network is NULL and webChannel is used instead. It wont show any data in such a case. Is there an easy way to bypass this problem please?
Example:
{"id":52341,"url":"https://www.tvmaze.com/shows/52341/star-wars-andor","name":"Star Wars: Andor","type":"Scripted","language":"English","genres":["Science-Fiction","Thriller","Espionage"],"status":"Running","runtime":null,"averageRuntime":46,"premiered":"2022-09-21","ended":null,"officialSite":"https://www.disneyplus.com/series/andor/3xsQKWG00GL5","schedule":{"time":"","days":["Wednesday"]},"rating":{"average":7.8},"weight":99,"network":null,"webChannel":{"id":287,"name":"Disney+","country":null,"officialSite":"https://www.disneyplus.com/"},"dvdCountry":null,"externals":{"tvrage":null,"thetvdb":393189,"imdb":"tt9253284"},"image":{"medium":"https://static.tvmaze.com/uploads/images/medium_portrait/487/1217811.jpg","original":"https://static.tvmaze.com/uploads/images/original_untouched/487/1217811.jpg"},"summary":"<p><b>Star Wars: Andor</b> explores a new perspective from the Star Wars galaxy, focusing on Cassian Andor's journey to discover the difference he can make. The series brings forward the tale of the burgeoning rebellion against the Empire and how people and planets became involved. It's an era filled with danger, deception and intrigue where Cassian will embark on the path that is destined to turn him into a rebel hero.</p>","updated":1700163464,"_links":{"self":{"href":"https://api.tvmaze.com/shows/52341"},"previousepisode":{"href":"https://api.tvmaze.com/episodes/2369091"}}}

Joined: Sep 2003
Posts: 42
Demarko Offline OP
Ameglian cow
OP Offline
Ameglian cow
Joined: Sep 2003
Posts: 42
Originally Posted by Demarko
However there is a small problem with the given regex and that one is that it wont work for example for shows where network is NULL and webChannel is used instead. It wont show any data in such a case. Is there an easy way to bypass this problem please?
As for now, I solved my problem using two separate regular expressions. One for the network value and another one for webchannel value.
So if the 1st value returns nothing, I simply try the 2nd one. That seems to do a good job so far cool


Link Copied to Clipboard