summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoe Rubinstein <nrubinstein@avencall.com>2012-09-17 16:36:19 +0200
committerNoe Rubinstein <nrubinstein@avencall.com>2012-09-17 16:36:19 +0200
commit3abd35fb0b13c7dacb6d99361943a8c302382c9e (patch)
treed7ea630e376d575cab6f517aba3436f8a1a6db4a
parentfbcd4630be8a03d26836bea8fb43a998a73410f5 (diff)
Add a webserver for searching digikey
-rw-r--r--digikey.coffee37
-rwxr-xr-xdigikey.js19
2 files changed, 37 insertions, 19 deletions
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()
- }
- }
-});