omardelarosa/pitchfork-npm

API will not connect to Pitchfork, along with other errors

Closed this issue · 3 comments

So I was playing around with your API today and noticed a few oddities. First off, running one of your examples in the test/using-promises.js led me to the following error:

> node using-promises.js

events.js:160
throw er; // Unhandled 'error' event
^

Error: Failed to connect to Pitchfork!
at Object. (/home/bruno207/development/pitchfork-npm/search.js:12:22)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object. (/home/bruno207/development/pitchfork-npm/index.js:2:15)
at Module._compile (module.js:570:32)

In fact, any time I try to interact with the API, whether through the command line or through node I hit that same error:

> pitchfork wilco 'yankee hotel foxtrot'

events.js:160
throw er; // Unhandled 'error' event
^

Error: Failed to connect to Pitchfork!
at Object. (/home/bruno207/development/lang-tutorials/node-testbench/node_modules/pitchfork/search.js:12:22)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object. (/home/bruno207/development/lang-tutorials/node-testbench/node_modules/pitchfork/index.js:2:15)
at Module._compile (module.js:570:32)

I figured that I should take a look at search.js to deduce what's going on here. After a bit of testing there's a few things I've found:

  1. The Pitchfork search url (BASE_URL) is no longer https://github.com/omardelarosa/pitchfork-npm/blob/master/search.js#L11 but has changed slightly to: http://pitchfork.com/search/?query="
  2. They seem to be blocking your user-agent. When I commented out https://github.com/omardelarosa/pitchfork-npm/blob/master/search.js#L77 I was able to successfully connect to Pitchfork. I also tried uncommenting that line and modifying https://github.com/omardelarosa/pitchfork-npm/blob/master/search.js#L10 to 'nottheuseragent' and I was again able to connect to Pitchfork.
  3. After modifying both those variables I hit then another error:
    > node using-promises.js

/home/bruno207/development/pitchfork-npm/search.js:21
responseBody.forEach(function(obj){
^

TypeError: responseBody.forEach is not a function
at get_review_objects (/home/bruno207/development/pitchfork-npm/search.js:21:16)
at /home/bruno207/development/pitchfork-npm/search.js:88:23
at Request.callback (/home/bruno207/development/pitchfork-npm/node_modules/superagent/lib/node/index.js:731:3)
at Request. (/home/bruno207/development/pitchfork-npm/node_modules/superagent/lib/node/index.js:135:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at Stream. (/home/bruno207/development/pitchfork-npm/node_modules/superagent/lib/node/index.js:906:12)
at emitNone (events.js:91:20)
at Stream.emit (events.js:185:7)
at Unzip. (/home/bruno207/development/pitchfork-npm/node_modules/superagent/lib/node/utils.js:124:12)

after a bit of printing it seems that responseBody is an object and you can't forEach with an object. In fact, responseBody is just res.body which in itself is an empty object. You can print res or res.body to check out what's being returned here: https://github.com/omardelarosa/pitchfork-npm/blob/master/search.js#L84

Not sure where to go from here but I just wanted to let you know that the API at the moment doesn't really work as intended. Let me know if you'd like any additional details.

node -v

v6.9.2

Many thanks for identifying these issues! I haven't updated this CLI since the (perhaps no longer so) recent Pitchfork re-design. Re: the User-Agent, I might just make it dynamic or user-configurable.

If you feel up for it, you're welcome to send a pull request to match the updated query. Otherwise, I will try to put together an updated version of this soon.

@Bruno207 I just pushed up on a new patch version 0.0.11 addressing the issues you identified. Try the examples and CLI again and they should work. I tested with node 4.0 and node 6.5.0 and it worked.

Can confirm, thanks for the fixes!