Skip to content
Browse files

first commit

  • Loading branch information...
0 parents commit 723b907daf33be0576eab298981faeac61fdd695 @freeborn freeborn committed Jul 28, 2011
Showing with 147 additions and 0 deletions.
  1. +23 −0 README
  2. +96 −0 bitcoin_converter.js
  3. +3 −0 bitcoin_rate.js
  4. +23 −0 cadjsonparse.py
  5. +2 −0 mytest.htm
23 README
@@ -0,0 +1,23 @@
+yoinked from here
+
+http://sanescreen.org/converter/
+
+modified bitcoin_rate.js to use CAD instead of USD.
+
+have not chenaged bitcoin_converter.js to represent CAD instead of USD
+
+
+
+USAGE:
+edit cadjsonparse.py to create bitcoin_rate.js under accessable web dir
+
+add a cron job for
+python cadjsonparse.py
+
+
+place bitcoin_converter.js in accessable webdir
+
+call bitcoin_converter.js from html
+
+<script type="text/javascript" src="http://meme.hackspace.ca/bitcoin_converter.js"></script>
+Membership costs <span class="usd_to_btc">50</span> BTC per month
96 bitcoin_converter.js
@@ -0,0 +1,96 @@
+/*
+Author : ThomasV
+Licence : GPL v3
+Support : 12oabCifvHuxzXtYVGhkxVfWZDvKcU743s
+*/
+
+function getElementsByClassName(oElm, strTagName, strClassName) {
+ var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
+ var arrReturnElements = new Array();
+ strClassName = strClassName.replace(/\-/g, "\\-");
+ var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
+ var oElement;
+ for( var i=0; i< arrElements.length; i++ ) {
+ oElement = arrElements[i];
+ if(oRegExp.test(oElement.className)){
+ arrReturnElements.push(oElement);
+ }
+ }
+ return (arrReturnElements);
+}
+
+
+/* A timestamp is added to the url, to prevent cache effects */
+function add_xc_script(){
+ var timestamp = new Date().getTime();
+ var request_url = "http://meme.hackspace.ca/bitcoin_rate.js?h="+timestamp;
+ var scriptObj = document.createElement("script");
+ scriptObj.setAttribute("type", "text/javascript");
+ scriptObj.setAttribute("src", request_url);
+ scriptObj.setAttribute("id", "xc");
+ var headLoc = document.getElementsByTagName("head").item(0);
+ headLoc.appendChild(scriptObj);
+}
+
+
+function bitcoin_converter_callback( ) {
+
+ var c = getElementsByClassName(document,"span","usd_to_btc");
+ for(var i=0; i<c.length; i++) {
+ address = c[i].title;
+ var pstr = c[i].innerHTML.split(" ");
+ var usd_price = parseFloat(pstr[0]);
+ var add_unit = false;
+ if(pstr.length>1) {
+ if( pstr[1]=="USD") {
+ add_unit = true;
+ } else {
+ alert("error");
+ return;
+ }
+ }
+ var price = Math.round(100*usd_price/self.btc_usd)/100.;
+ var price_u = "" + price;
+ if(add_unit) price_u = price_u + " BTC";
+ if(address) {
+ price_u = "<a href='javascript:show_bitcoin_address("+price+",\""+address+"\");'>" + price_u + "</a>";
+ }
+ c[i].innerHTML = price_u ;
+ c[i].title = usd_price + " USD";
+ }
+
+ var c = getElementsByClassName(document,"span","btc_to_usd");
+ for(var i=0; i<c.length; i++) {
+ address = c[i].title;
+ var pstr = c[i].innerHTML.split(" ");
+ var btc_price = parseFloat(pstr[0]);
+ var add_unit = false;
+ if(pstr.length>1) {
+ if( pstr[1]=="BTC") {
+ add_unit = true;
+ } else {
+ alert("error");
+ return;
+ }
+ }
+ var price = btc_price*self.btc_usd;
+ if(price < 1) precision = 1000.; else precision = 100.;
+ price = Math.round(precision*price)/precision;
+ var price_u = "" + price;
+ if(add_unit) price_u = price_u + " USD";
+ if(address) {
+ price_u = "<a href='javascript:show_bitcoin_address("+price+",\""+address+"\");'>" + price_u + "</a>";
+ }
+ c[i].innerHTML = price_u ;
+ c[i].title = btc_price + " BTC";
+ }
+
+
+}
+
+function show_bitcoin_address(amount, address){
+ alert("This item can be purchased with Bitcoins.\nPrice: "+amount+" Bitcoins\nAddress: "+address+"\n\nVisit http://www.bitcoin.org for more information.");
+}
+
+/* change this if you use addLoadEvent */
+window.onload = add_xc_script;
3 bitcoin_rate.js
@@ -0,0 +1,3 @@
+var btc_usd=13.0603;
+var btc_timestamp=1311817403.57;
+bitcoin_converter_callback( );
23 cadjsonparse.py
@@ -0,0 +1,23 @@
+import json
+import urllib2
+import time
+import sys
+
+try:
+ data = urllib2.urlopen("http://bitcoincharts.com/t/weighted_prices.json")
+except IOError, (errno):
+ print "%s" % (errno)
+ sys.exit()
+
+j = json.load(data)
+CADdata = j["CAD"]["24h"]
+
+timestamp = str(time.time())
+
+text_file = open("bitcoin_rate.js", "w")
+
+text_file.write("var btc_usd=" + CADdata + ";\n")
+text_file.write("var btc_timestamp=" + timestamp + ";\n")
+text_file.write("bitcoin_converter_callback( );\n")
+
+text_file.close()
2 mytest.htm
@@ -0,0 +1,2 @@
+<script type="text/javascript" src="http://meme.hackspace.ca/bitcoin_converter.js"></script>
+Membership costs <span class="usd_to_btc">50</span> BTC per month

0 comments on commit 723b907

Please sign in to comment.
Something went wrong with that request. Please try again.