SmartBlocks icon indicating copy to clipboard operation
SmartBlocks copied to clipboard

Pull data from IMDb.com into Roam 2.0 - Allowing for multiple title results

Open mlava opened this issue 4 years ago β€’ 28 comments

This is the most recent version: IMDb SmartBlocks.zip

βœ‚οΈ Copy of your #42SmartBlock from Roam

  • #42SmartBlock IMDB
    
    • <%NOBLOCKOUTPUT%><%JAVASCRIPTASYNC: ```javascript
      var apiKey = '<your API key here>';
      var search = document.title;
      var settings = {
        "url": "https://www.omdbapi.com/?apiKey="+apiKey+"&s="+search+"",
        "method": "GET",
        "async": false,
      };
      
      $.ajax(settings).done(function (response) {
        console.log(response);
        var jsonMovies = JSON.stringify(response);
        var movies = JSON.parse(jsonMovies);
        document.movieList = movies;
      });``` %>
      
    • Which movie do you mean? <%JAVASCRIPTASYNC: ```javascript
      var listToReturn = "{{"+document.movieList.Search[0].Title+" - "+document.movieList.Search[0].Year+":42SmartBlock:zz IMDb Selected:movieId="+document.movieList.Search[0].imdbID+"}}";
      if (typeof document.movieList.Search[1] != 'undefined') {
        listToReturn += " {{"+document.movieList.Search[1].Title+" - "+document.movieList.Search[1].Year+":42SmartBlock:zz IMDb Selected:movieId="+document.movieList.Search[1].imdbID+"}}";
      }
      if (typeof document.movieList.Search[2] != 'undefined') {
        listToReturn += " {{"+document.movieList.Search[2].Title+" - "+document.movieList.Search[2].Year+":42SmartBlock:zz IMDb Selected:movieId="+document.movieList.Search[2].imdbID+"}}";
      }
      if (typeof document.movieList.Search[3] != 'undefined') {
        listToReturn += " {{"+document.movieList.Search[3].Title+" - "+document.movieList.Search[3].Year+":42SmartBlock:zz IMDb Selected:movieId="+document.movieList.Search[3].imdbID+"}}";
      }
      if (typeof document.movieList.Search[4] != 'undefined') {
        listToReturn += " {{"+document.movieList.Search[4].Title+" - "+document.movieList.Search[4].Year+":42SmartBlock:zz IMDb Selected:movieId="+document.movieList.Search[4].imdbID+"}}";
      }
      return listToReturn;```  %>
      
  • #42SmartBlock zz IMDb Selected
    
    • <%J: document.activeElement.value = ""; %><%NOBLOCKOUTPUT%><%JAVASCRIPTASYNC: ```javascript
      let movieId = roam42.smartBlocks.activeWorkflow.vars["movieId"];
      var apiKey = '<your API key here>';
      var settings = {
        "url": "https://www.omdbapi.com/?apiKey="+apiKey+"&i="+movieId+"",
        "method": "GET",
        "async": false,
      };
      
      $.ajax(settings).done(function (response) {
        console.log(response);
        var jsonMovies = JSON.stringify(response);
        var movies = JSON.parse(jsonMovies);
        document.movies = movies;
        var directors = movies.Director;
        var directors1 = directors.replace(new RegExp(', ', 'g'), "]] [[");
        document.directors = directors1;
        var writers = movies.Writer;
        var res = writers.match(/screen|novel|story|characters|based/); // remove 'screen play' and 'novel'
        if (res !== null) {
          var writers = writers.replace(new RegExp('(additional story material)|(screen play)|(by)|(novel)|(characters)|(story by)|(screenplay by)|(based on characters created by)|(based upon the novel by)|(story)|(screenplay)|(co-head)', 'g'), "");
        	var writers = writers.replace(new RegExp('\[(.+)\]', 'g'), "");
        	var writers = writers.replace(new RegExp(' , ', 'g'), "]] [[");
        	var writers = writers.replace(new RegExp(', ', 'g'), "]] [[");
        } else {
        	var writers = writers.replace(new RegExp(', ', 'g'), "]] [[");
        }
        document.writers = writers;
        var actors = movies.Actors;
        var cast = actors.replace(new RegExp(', ', 'g'), "]] [[");
        document.cast = cast;
        var genres = movies.Genre;
        var genre = genres.replace(new RegExp(', ', 'g'), " #");
        document.genre = genre;
      });``` %>
      
    • <%IFTRUE:document.movies.Poster!=='N/A'%><%J:return'![]('+document.movies.Poster+')'%>
      
    • <%JAVASCRIPT: return'**Metadata:**'%>
      
      • <%JAVASCRIPT: return'**Director:** [['+document.directors+']]'%>
        <%JAVASCRIPT: return'**Writer:** [['+document.writers+']]'%>
        <%JAVASCRIPT: return'**Cast:** [['+document.cast+']]'%>
        <%JAVASCRIPT: return'**Year:** [['+document.movies.Year+']]'%>
        <%JAVASCRIPT: return'**Keywords:** #movies #'+document.genre+' '%><%CURSOR%>
        
    • <%JAVASCRIPT: return'**IMDb:** https://www.imdb.com/title/'+document.movies.imdbID+'/'%>
      
    • <%JAVASCRIPT: return'**Plot Summary:** '+document.movies.Plot+''%>
      
    • <%JAVASCRIPT: return'**Rating:**  {{[[slider]]}}'%>
      

πŸ“‹ Describe the SmartBlock

This SmartBlock takes the title of the page as input to retrieve data from the IMDb via the OMDb API.

This version addresses the issue raised at https://github.com/roamhacker/SmartBlocks/issues/121 whereby there might be several movies with the same name. This version produces a series of buttons to select which movie is the best match, and then outputs the data from the OMDb API.

Note: poor coding in my first attempt lead to it breaking after a Roam42 update. I have updated code above as shown here: https://github.com/roamhacker/SmartBlocks/issues/122#issuecomment-748581003

βœ… Describe any prerequisites or dependencies that are required for this SmartBlock

You need to get a free API Key for OMDb at http://www.omdbapi.com/. The free account gives up to 1000 API calls daily, which should be more than enough for most of us!

πŸ“· Screenshot of your #42SmartBlock workflow/template from Roam

screenshot 1

Make sure to put the OMDb API key into the apiKey position in line 1 of the image above and line 2 of the image below.

screenshot 2 screenshot 3

The workflow/output can be seen in this loom video: https://www.loom.com/share/ea56d7429a6e430c89575d490ad01e4f

πŸ’‘ Additional Info

https://www.loom.com/share/ea56d7429a6e430c89575d490ad01e4f

Thanks to @roamhacker for suggested tweaks to make it all work nicely!

mlava avatar Dec 15 '20 21:12 mlava

Thanks for the updating, but this time smart block can't work properly under the new code

Daydog avatar Dec 16 '20 06:12 Daydog

Can you tell me what the actual issue is? At what point does it fail?

mlava avatar Dec 16 '20 07:12 mlava

Can you tell me what the actual issue is? At what point does it fail?

I copied all these codes and put the API key as your requirement, but nothing will happen when I key ";;IMDB'' and ";;IMDb Selected", does it work well on your roam research?

Daydog avatar Dec 16 '20 10:12 Daydog

Hi again, It's working fine for me in my Roam, as per the Loom video. Are you able to get any other SmartBlocks to launch? Is it copied and pasted exactly as in the images? Could you screenshot where you have it in Roam and post? Also, can you click F12 to open the console in your browser before you do the ;;IMDb trigger and see if it successfully logs an entry like this: image Clicking the little triangles should expand to show more information as below: screenshot 6

Cheers!

mlava avatar Dec 16 '20 10:12 mlava

image Hi there, I am sure that the code is exactly good, while finding something wrong here, need I get a new API?

Daydog avatar Dec 16 '20 11:12 Daydog

image Hi there, I am sure that the code is exactly good, while finding something wrong here, need I get a new API?

Thank you very much! I run the code successfully when I apply for a new API, BTW, this is an amazing smart block, I feel so happy when it works.

Daydog avatar Dec 16 '20 12:12 Daydog

Hi @Daydog. I am glad you got it working and glad you're finding it useful! Cheers πŸ˜ƒ

mlava avatar Dec 16 '20 20:12 mlava

Is there are reason you're using with bold **example:** instead of metadata tags like example:: ?

macklinhrw avatar Dec 16 '20 21:12 macklinhrw

Hi @macklinhrw

No real reason. I just don't really use Roam Research attributes as I've not found them useful. I wouldn't call them 'metadata tags' although I know a lot of people place attributes in their Roam Research metadata sections.

They serve no purpose than to future proof against some potential improvement in their function that no-one knows for sure will be developed.

Also, as a concept, do you think director needs to be an attribute? Are you likely to plot directors in a table, for example?

But the point of sharing the code if for people to use/modify as desired - happy for you to tweak the code for attributes if you prefer them. Cheers! πŸ˜ƒ

mlava avatar Dec 17 '20 01:12 mlava

The original code broke, and this SmartBlock stopped working. The error was in the second block of the IMDB SmartBlock, that begins with 'Which movie did you mean?'.

Please replace with:

Which movie do you mean? <%JAVASCRIPTASYNC: ```javascript
var listToReturn = "{{"+document.movieList.Search[0].Title+" - "+document.movieList.Search[0].Year+":42SmartBlock:zz IMDb Selected:movieId="+document.movieList.Search[0].imdbID+"}}";
if (typeof document.movieList.Search[1] != 'undefined') {
  listToReturn += " {{"+document.movieList.Search[1].Title+" - "+document.movieList.Search[1].Year+":42SmartBlock:zz IMDb Selected:movieId="+document.movieList.Search[1].imdbID+"}}";
}
if (typeof document.movieList.Search[2] != 'undefined') {
  listToReturn += " {{"+document.movieList.Search[2].Title+" - "+document.movieList.Search[2].Year+":42SmartBlock:zz IMDb Selected:movieId="+document.movieList.Search[2].imdbID+"}}";
}
if (typeof document.movieList.Search[3] != 'undefined') {
  listToReturn += " {{"+document.movieList.Search[3].Title+" - "+document.movieList.Search[3].Year+":42SmartBlock:zz IMDb Selected:movieId="+document.movieList.Search[3].imdbID+"}}";
}
if (typeof document.movieList.Search[4] != 'undefined') {
  listToReturn += " {{"+document.movieList.Search[4].Title+" - "+document.movieList.Search[4].Year+":42SmartBlock:zz IMDb Selected:movieId="+document.movieList.Search[4].imdbID+"}}";
}
return listToReturn;```  %>

mlava avatar Dec 20 '20 08:12 mlava

Mark, please try to edit your original SmartBlock here. If that doesn’t happen, maybe you can recreate it. That way we flush out the beta errors. Thanks for your amazing work.

TfTHacker avatar Dec 20 '20 10:12 TfTHacker

@roamhacker No worries, done.

mlava avatar Dec 20 '20 11:12 mlava

Hey there, this is a great SmartBlocks that used to work for me just fine. Unfortunately now it isn't anymore...I get the same error as Daydog above (the first line error 401), tried a new APIkey (and activated it on the website), copy and pasted the SmartBlocks in many times, turned off my CSS, re-installed Roam42, tried different browsers (Chrome and Safari)...all other SmartBlocks are working fine. and I'm running out of ideas what to do - Any ideas?

Also, I have replaced the second block with the code from the comment - is that still necessary?

Thanks for your help and this great SB!

kabo1234 avatar Jan 19 '21 04:01 kabo1234

@kabo1234

Please delete the SmartBlocks and import this version attached. Then, add your API key where it's needed (two places). Then try again. IMDb.zip

mlava avatar Jan 19 '21 06:01 mlava

thanks for the quick answer, but...still nothing. Any other ideas?

kabo1234 avatar Jan 19 '21 10:01 kabo1234

Have you left the < and > symbols in place in the var apiKey = '<your API key here>'; lines? It should just be an alphanumeric string between the quote marks, without < or >. Cheers.

mlava avatar Jan 19 '21 11:01 mlava

oh silly me - success!! thank you so much!

(ps I'll delete the video, forgot to blur the apikey)

kabo1234 avatar Jan 19 '21 11:01 kabo1234

Hi again,

I have run into a new problem where films with ":" in the title don't seem to render:

https://user-images.githubusercontent.com/74577463/105428091-be2cda80-5ca2-11eb-8750-99195fbccb79.mov

I've tried a couple fo different titles, all with colons, and they also fail to load. Any ideas?

Again, thanks for this great SB!

kabo1234 avatar Jan 22 '21 00:01 kabo1234

Hi @kabo1234

Sorry about that. This version should fix.

IMDB SB.zip

mlava avatar Jan 22 '21 01:01 mlava

Hi @kabo1234

Sorry about that. This version should fix.

IMDB SB.zip

Thanks this is working now!

One other thing...I am trying to also pull 'runtime' and added a line like this in zz IMDB selected:

document.runtime = runtime;

and then in the Metadata block:

<%JAVASCRIPT: return'Runtime:: [['+document.runtime+']]'%>

but it just returns 'undefined'

I had it working in the previous version, but unfortunately deleted it to import the new page...what am I missing?

kabo1234 avatar Jan 28 '21 04:01 kabo1234

Hi @kabo1234

in the zz IMDB selected SB, add:

document.runtime = movies.Runtime;

instead of

document.runtime = runtime;

That should work, but let me know either way!

mlava avatar Jan 28 '21 04:01 mlava

perfect thank you! I had tried a variety of combinations, but not the capitalisation of Runtime...

kabo1234 avatar Jan 28 '21 05:01 kabo1234

Hey, I'm back (sorry!)...it doesn't seem to find TV series (e.g. https://www.imdb.com/title/tt2741602/) - are they different, or would they require their own SB? Thanks for your help!

kabo1234 avatar Jan 29 '21 05:01 kabo1234

Hi again, In the last update, I added a filter for movies as I was getting game results and things that seemed to be clutter. If you need, I could make it work for movie or tv series only, but not games?

mlava avatar Jan 29 '21 06:01 mlava

If you change line 4 if the second code block in #42SmartBlock IMDB from:

if (document.movieList.Search[i].Type == "movie") {

to:

if (document.movieList.Search[i].Type == "movie" || document.movieList.Search[i].Type == "series") {

it should work as you want it to.

mlava avatar Jan 29 '21 06:01 mlava

Yes, it worked! fantastic! I can't tell you how helpful this is for my research...thank you again!

kabo1234 avatar Jan 29 '21 06:01 kabo1234

No worries!

On Fri, Jan 29, 2021 at 5:41 PM kabo1234 [email protected] wrote:

Yes, it worked! fantastic! I can't tell you how helpful this is for my research...thank you again!

β€” You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/roamhacker/SmartBlocks/issues/122#issuecomment-769612089, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABUKIPSA5NWZFPJ4667PSC3S4JKDJANCNFSM4U475REQ .

mlava avatar Jan 29 '21 09:01 mlava

New version in first post.

Updated to fix some issues and also to use Roam.alpha write api.

mlava avatar Apr 25 '21 10:04 mlava