mistval / unofficial-jisho-api

Encapsulates the official Jisho.org API and also provides kanji, example, and stroke diagram search.
MIT License
140 stars 15 forks source link

CORS #6

Closed jeffbernst closed 6 years ago

jeffbernst commented 6 years ago

Hi, first of all thanks for making this great tool! This is exactly what I was hoping to find.

I'm getting the cross origin error when I try to use this though (and I saw it was discussed in the thread on jisho). Is there anyway to send the requests through a proxy?

mistval commented 6 years ago

Hi, thanks for the issue. I'll see about adding an argument so that you can specify request parameters such as proxy.

In the meantime, looking at request's documentation it sounds like you can set an HTTP_PROXY environment variable and it should use that. Not the greatest solution but should work in a pinch.

jeffbernst commented 6 years ago

Ah ok cool, thanks!

mistval commented 6 years ago

I added a way to configure request options in the newly available version 1.1.2.

Check out the updated README for examples of how to configure a proxy.

Feel free to reopen the issue if there's any problem :)

jeffbernst commented 6 years ago

That was so fast -- thank you!

I'm trying it out now and getting an error (see image). cleanshot 2018-10-22 at 22 51 01

This is the configuration I used:

const jishoApi = require('unofficial-jisho-api');
const jisho = new jishoApi({ proxy: 'https://cors-anywhere.herokuapp.com/' });

jisho.searchForPhrase('日').then(result => {
  console.log(result);
});

If I use that proxy and make a fetch request directly to the Jisho API it works fine with something like this:

const proxy = 'https://cors-anywhere.herokuapp.com/'
const word = '余計';
const url = proxy + 'http://jisho.org/api/v1/search/words?keyword=' + word;

const response = await fetch(url);
const json = await response.json();
console.log(json)
mistval commented 6 years ago

Ah that proxy is a little bit weird, I don't think you can make it work by specifying options to the request module. How about this then, I'll expose the parsing functions and then you can get the HTML body of the page however works for you, pass it into the parsing functions, and get JSON out.

mistval commented 6 years ago

So for phrase search (official API) you would do

  const proxy = 'https://cors-anywhere.herokuapp.com/'
  const word = '余計';
  const url = proxy + jisho.getUriForPhraseSearch(word);

  const response = await fetch(url);
  const json = await response.json();
  console.log(json);

And for kanji search:

  const proxy = 'https://cors-anywhere.herokuapp.com/'
  const kanji = '余';
  const url = proxy + jisho.getUriForKanjiSearch(kanji);

  const response = await fetch(url);
  const json = jisho.parseKanjiPageHtml(await response.body()); // Or however you access the body of the response using whatever library you're using for HTTP requests
  console.log(json);

Would that work?

jeffbernst commented 6 years ago

Yeah for sure! That works for me. And one of the main things I'd want to get would be the sentences.

Let me know if I can help with any of this too! I appreciate everything you've already put together. Also, there's a chance I'll end up creating my own database with the dictionary/sentence files eventually so I don't want to feel like I've requested stuff from you that I didn't use enough.

mistval commented 6 years ago

Please try out the new v1.1.3 and let me know if it gets the job done :) Examples for fetching the pages yourself and having the module parse them are near the bottom of the updated README

For the sentences, the furigana has mistakes 5-10% of the time. At some point I'd like to see about improving that. If you notice sentences where the API returns different furigana compared to the Jisho.org website, feel free to comment on the issue I have open for that and include the search term and the incorrect sentence.

By the way, if you don't need the furigana, you might consider downloading Tatoeba, that's where (all of?) Jisho's example sentences come from and you can download their entire database as a big TSV file.

jeffbernst commented 6 years ago

Hey sorry for the late response -- had a busy week! I'm about to run back out but should be able to try out the new features within the next couple of days. Thanks again for adding this stuff! :)

And I appreciate the link to Tatoeba! I've been thinking about just downloading the files like you said and setting up my own server.

jeffbernst commented 6 years ago

Just tried it out and it works great. Thanks again! 😀

gfreecs0510 commented 3 years ago

hi @mistval ohayo gozaimasu, is there really no other way than to make a proxy server to resolve the CORS issue? thank you in advance

mistval commented 3 years ago

Hi @gfreecs0510 If you want to use this package (or the Jisho API in general) in a website, you'll have to use a proxy (or maybe try asking nicely on Jisho's forum for them to allow CORS).

gfreecs0510 commented 3 years ago

@mistval thank you very much, cheers :)