Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VENDOR REQUEST]: Digi-Key (US) #30

Open
PhillippOhlandt opened this issue Oct 2, 2022 · 10 comments
Open

[VENDOR REQUEST]: Digi-Key (US) #30

PhillippOhlandt opened this issue Oct 2, 2022 · 10 comments
Assignees
Labels
vendor-request Requesting a new vendor to be added

Comments

@PhillippOhlandt
Copy link
Member

Vendor Name

Digi-Key

Vendor Country

US

Vendor URL

https://www.digikey.com/

Product Links

https://rpilocator.com/?vendor=digikeyus

Additional information

https://github.com/nerves-metal-detector/nerves_metal_detector/blob/master/CONTRIBUTING.md#adding-new-vendors-and-products-from-rpilocator

There is also a German site which could be covered as an additional vendor as well.

@PhillippOhlandt PhillippOhlandt added the vendor-request Requesting a new vendor to be added label Oct 2, 2022
@PhillippOhlandt PhillippOhlandt added this to the RpiLocator Catalogue milestone Oct 2, 2022
@bflatau
Copy link
Contributor

bflatau commented Oct 17, 2022

@PhillippOhlandt I'll try this one next.

@PhillippOhlandt
Copy link
Member Author

@bflatau alright. Vendor name is DigiKeyUs and it uses application/ld+json. But it is also showing the count of available items which needs to be parsed from the HTML (not inside json). Unfortunately, there is no meta information in the HTML so you need to parse the XX In Stock text.

It's as simple as that (as long as the shop doesn't put anything in front of the number).

iex(1)> Integer.parse "72 In Stock"
{72, " In Stock"}

Please cast 0 to nil.

@bflatau
Copy link
Contributor

bflatau commented Oct 18, 2022

@PhillippOhlandt, have you come across this error?

[error] Product update job failed!

Job args:
%NervesMetalDetector.Vendors.DigikeyUs.ProductUpdate{url: "https://www.digikey.com/en/products/detail/raspberry-pi/SC0693/13530916", sku: "CM4101016"}

Error reason:
{:load_body, {:error, %HTTPoison.Error{reason: :timeout, id: nil}}}

If not, I'll try doing some research...

@PhillippOhlandt
Copy link
Member Author

@bflatau Does it happen every time?

@bflatau
Copy link
Contributor

bflatau commented Oct 18, 2022

@PhillippOhlandt yes every time :(

@PhillippOhlandt
Copy link
Member Author

@bflatau Ok, found it. I already suspected that and it took some testing. Basically, they want to be smart asses and prevent simple scrapers. Unfortunately (for them), all they really can check for are the request headers. Those are easy to "fake" tho (you can see that in the .livemd file for getting the items list from rpilocator).

What you can do now is have this list of headers defined before or after the options variable in the fetch_availability/1 function.

headers = [
  {"Accept",
   "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"},
  {"User-Agent",
   "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36"},
  {"Accept-Language", "en-US,en;q=0.9"},
  {"Accept-Encoding", ""}
]

Those 4 headers are actually required to get the request to work. Now you can pass these headers to the HTTPoison.get/3 function when loading the body, like so:

HTTPoison.get(url, headers, options)

@bflatau
Copy link
Contributor

bflatau commented Oct 19, 2022

Ah awesome! I'll check this out in the next few days. Yeah I was thinking, if I'm getting the products in the livebook, there's gotta be SOME way that this should be working...

Thanks for all the help, I'm learning a ton!

@PhillippOhlandt
Copy link
Member Author

@bflatau Hey, are you still planning on doing this one?

@bflatau
Copy link
Contributor

bflatau commented Mar 13, 2023

Hey @PhillippOhlandt!

Sorry I've been bogged down with some life stuff--I've actually been thinking about this project lately and wanting to get back into it. If you want to take care of it, you won't hurt my feelings, but I hope to make some progress this weekend. If I don't get anything done by this weekend, just assume that I'll contribute when I can, but don't factor me into any of your decisions.

Thanks for checking in

--Ben

@PhillippOhlandt
Copy link
Member Author

Hey @bflatau, sorry for the late reply. No worries, I will leave this for you for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
vendor-request Requesting a new vendor to be added
Projects
None yet
Development

No branches or pull requests

2 participants