From 3abd35fb0b13c7dacb6d99361943a8c302382c9e Mon Sep 17 00:00:00 2001 From: Noe Rubinstein Date: Mon, 17 Sep 2012 16:36:19 +0200 Subject: Add a webserver for searching digikey --- digikey.coffee | 37 +++++++++++++++++++++++++++++++++++++ digikey.js | 19 ------------------- 2 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 digikey.coffee delete mode 100755 digikey.js diff --git a/digikey.coffee b/digikey.coffee new file mode 100644 index 0000000..b372ca7 --- /dev/null +++ b/digikey.coffee @@ -0,0 +1,37 @@ +#!/home/xilun/phantomjs-1.6.1-linux-x86_64-dynamic/bin/phantomjs + +webpage = require('webpage') +server = require('webserver').create() +query_parse = /search\/([^&]+)/ + +url = 'http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=' + +service = server.listen 8080, (request, response) -> + page = webpage.create() + + search = query_parse.exec request.url + + unless search? and search[1]? + console.log "Invalid URL: "+request.url + response.statusCode = 404 + response.write "try search/componentname" + response.close() + return + + console.log search[1] + + thisUrl = url + search[1] + tries = 0 + page.open thisUrl, (status) -> + if status != "success" + response.statusCode = 503 + response.write "MORT ET DESOLATION" + response.close() + else + if page.evaluate(-> typeof test == "function") + page.evaluate -> test() + console.log "TRYING AGAIN (try " + (tries += 1) + ")" + else + response.statusCode = 200 + response.write page.content + response.close() diff --git a/digikey.js b/digikey.js deleted file mode 100755 index ec452a3..0000000 --- a/digikey.js +++ /dev/null @@ -1,19 +0,0 @@ -#!/home/xilun/phantomjs-1.6.1-linux-x86_64-dynamic/bin/phantomjs - -var page = require('webpage').create(); - -var url = 'http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=IRF7455PBFTR-ND'; - -page.open(url, function (status) { - if(status !== "success") { - console.log("MORT ET DESOLATION") - } else { - if(page.evaluate(function () { return (typeof test === "function"); })) { - page.evaluate(function(){test();}) - console.log("TRY AGAIN") - } else { - console.log(page.content) - phantom.exit() - } - } -}); -- cgit v1.2.3