Last active
December 23, 2015 15:39
-
-
Save jaguilar/6657055 to your computer and use it in GitHub Desktop.
Google docs script for scraping eve data from eve-central.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I hereby release this code to the public domain. | |
// Substitutes for placeholders in a string. | |
// Ex. sub("I am a {0} monkey.", "silly") -> "I am a silly monkey." | |
function sub(s) { | |
var args = arguments; | |
return s.replace(/{(\d+)}/g, function(match, number) { | |
var n = parseInt(number); | |
return typeof args[n+1] != 'undefined' | |
? args[n+1] | |
: match; | |
}); | |
}; | |
// obj must contain properties "type_id" and "system_id". Fills obj with | |
// various other properties from eve-central, and enters it into the | |
// cache. | |
// | |
// In: | |
// type_id: The Eve item's type id. | |
// system_id: The system you want to collect data about. | |
// | |
// Out: | |
// buy: Data about buy orders in the system. | |
// sell: Data about sell orders in the system. | |
function getItemData(obj) { | |
var obj = getOrCreate(obj); | |
if (obj.last_update != null && | |
(new Date().getTime() - obj.last_update) < 12 * 60 * 60 * 1000) { | |
return obj; | |
} | |
obj.last_update = new Date().getTime(); | |
try { | |
obj = eveCentralUpdate(obj); | |
save(obj); | |
} catch (e) { | |
Logger.log(e); | |
} | |
} | |
// Get the buy data from type and system as an array. You can use this as | |
// a custom function in a spreadsheet cell. | |
function buyData(type_id, system_id) { | |
return toArray(getItemData({type_id: type_id, system_id: system_id}).buy); | |
} | |
// The same, but for sell data. | |
function sellData(type_id, system_id) { | |
return toArray(getItemData({type_id: type_id, system_id: system_id}).sell); | |
} | |
// Reformat a buy or sell tuple into an array. | |
function toArray(m) { | |
return [m.volume, m.min, m.avg, m.max]; | |
} | |
function test() { | |
Logger.log(buyData(34, 30000142)); | |
} | |
// Ask eve-central.com about obj, using its type_id and system_id properties. | |
function eveCentralUpdate(obj) { | |
var resp = UrlFetchApp.fetch(sub("http://api.eve-central.com/api/marketstat?typeid={0}&systemlimit={1}", obj.type_id, obj.system_id)); | |
var orders = XmlService.parse(resp.getContentText()).getRootElement().getChild("marketstat").getChild("type"); | |
obj.buy = orderDict(orders.getChild("buy")); | |
obj.sell = orderDict(orders.getChild("sell")); | |
return obj; | |
} | |
function eveCentralTest() { | |
Logger.log(getItemData({type_id: 34, system_id: 30002659})); | |
} | |
// Return a dictionary of order data from order xml in the eve-central.com response. | |
function orderDict(order_xml) { | |
return {volume: fieldNum(order_xml, "volume"), | |
avg: fieldNum(order_xml, "avg"), | |
min: fieldNum(order_xml, "min"), | |
max: fieldNum(order_xml, "max"), | |
percentile: fieldNum(order_xml, "percentile")}; | |
} | |
// Get the number inside of a named field in order_xml. | |
function fieldNum(order_xml, field) { | |
return parseFloat(order_xml.getChild(field).getText()); | |
} | |
// Get or create an object in the script database (a Google Docs-specific | |
// service). The existing properties of query are considered the keys | |
// of the query. | |
function getOrCreate(query) { | |
var res = queryExpectingOne(query); | |
if (res == null) { | |
return query; | |
} | |
return res; | |
} | |
// Issue a query and return the first object in the cursor. | |
function queryExpectingOne(obj) { | |
var db = ScriptDb.getMyDb(); | |
var iter = db.query(obj); | |
if (iter.hasNext()) { | |
return iter.next(); | |
} else { | |
return null; | |
} | |
} | |
// Store obj in the ScriptDb. | |
function save(obj) { | |
var db = ScriptDb.getMyDb(); | |
db.save(obj); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment