Here it is:

ActionGetBookInfoWidget.prototype.invokeAction = 
function(triggeringWidget,event) {
  function dateNow() {
    var set = new Date();
    var getDate = set.getDate().toString();
    if (getDate.length == 1){ //example if 1 change to 01
     getDate = "0"+getDate;
    }
    var getMonth = (set.getMonth()+1).toString();
    if (getMonth.length == 1){
     getMonth = "0"+getMonth;
    }
    var getYear = set.getFullYear().toString();
    var dateNow = getYear +"-"+ getMonth +"-"+ getDate; //today
    return dateNow;
  }

  function createBook(title, authors, publisher, isbn, cover) {
    var author_line = "* \'\'Authors:\'\' ".concat(authors);
    var publisher_line = "* \'\'Publisher:\'\' ".concat(publisher);
    var isbn_line = "* \'\'ISBN:\'\' ".concat(isbn);
    var date_line = "* \'\'Date Added:\'\' ".concat(dateNow());
    var cover_line = "[img[".concat(cover,"]]\n");
    var notes_line = "* \'\'Notes:\'\'";
    var text = [author_line, publisher_line, isbn_line, date_line, cover_line, 
notes_line].join("\n");
    var fields = { tags: "Books", text: text }
    console.log(fields);

    $tw.wiki.addTiddler(new $tw.Tiddler($tw.wiki.getCreationFields(), fields, 
{title: title}, $tw.wiki.getModificationFields()));
  }

  // Create URL
  var base = "https://openlibrary.org/api/books?bibkeys=ISBN:";;
  var isbn = this.isbn;
  var url = base.concat(isbn,"&format=json&jscmd=data");
  console.log(url);

  // Do HTTP request
  window.fetch(url)
    .then(
      function(response) {
        if (response.status !== 200) {
          console.log('Looks like there was a problem. Status Code: ' +
            response.status);
          return;
        }

        response.json().then(function(data) {
          console.log(JSON.stringify(data));
          var key = "ISBN:".concat(isbn);
          var title = data[key]["title"];
          var publisher = data[key]["publishers"][0]["name"];
          var authors = data[key]["authors"].map(a => a["name"]).join("|");
          var cover = data[key]["cover"]["medium"];
          createBook(title, authors, publisher, isbn, cover);
          alert("Created page for: " + title);
        });
      }
    )
    .catch(function(err) {
    });

  return true; // Action was invoked
};

There's also the input tiddler that just invokes this action:

New book ISBN: <$edit-text tiddler="$:/state/isbn" tag="input" default=""/>


<$button>
<$action-getbookinfo $isbn={{$:/state/isbn}}/>
Add New Book
</$button>


On Thursday, May 21, 2020 at 1:04:49 AM UTC-7, Mat wrote:
>
> Flan, please share your solution if you can.
>
> Even if you have a special solution there can be bits of knowledge that 
> are of use (for example, I'm curious how GETs are used as I hope for this 
> <https://stackoverflow.com/questions/45671606/google-sheets-api-read-single-cell-value-from-public-sheet>
>  one 
> day, i.e to fetch cell data from Google Sheets into TW.)
>
> Thanks
>
> <:-)
>

-- 
You received this message because you are subscribed to the Google Groups 
"TiddlyWikiDev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to tiddlywikidev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tiddlywikidev/b7a33bcf-c4fe-4352-a72d-cd0a5c0f18a0%40googlegroups.com.

Reply via email to