Skip to content

Instantly share code, notes, and snippets.

@oshliaer
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save oshliaer/553f5b6483d70fb192d4 to your computer and use it in GitHub Desktop.

Select an option

Save oshliaer/553f5b6483d70fb192d4 to your computer and use it in GitHub Desktop.
Using RegExp instead XmlServise.parse (). Without the structure of HTML #gas #xml #parse

Puma © 2015 MARVEL

Get google.com/mapmaker account info

var Bulk = function(template){
var b = template;
this.setProperty = function(prop, value){
if(!b.hasOwnProperty(prop)) return null;
switch(b[prop]['type']){
case 'float':
b[prop]['value'] = parseFloat(value);
break;
default:
b[prop]['value'] = '' + value;
break;
}
return this;
}
this.getProperty = function(prop){
if(b.hasOwnProperty(prop)) return b[prop]['value'];
return null;
}
this.getName = function(prop){
if(b.hasOwnProperty(prop)) return b[prop]['name'];
return null;
}
this.getRegExp = function(prop){
if(b.hasOwnProperty(prop)) return b[prop]['regExp'];
return null;
}
this.getOrder = function(name){
if(b.orders.hasOwnProperty(name)) return b.orders[name];
return null;
}
return this;
}
Bulk.prototype.fetch = function(){
var f = UrlFetchApp.fetch(this.getProperty('url'));
this.setProperty('fetch', f.getContentText());
return this;
}
Bulk.prototype.executor = function(prop, source, lastIndex, formatter){
var s = this.getProperty(source);
var re = new RegExp(this.getRegExp(prop));
var r = re.exec(s);
if(lastIndex) re.lastIndex = lastIndex;
if(r) {
if(formatter) r[1] = formatter(r[1]);
this.setProperty(prop, r[1]);
}
return this;
}
Bulk.prototype.getData = function(order){
var self = this;
if(!order) order = self.getOrder('deflt');
var data = order.map(function(el){
return self.getProperty(el);
});
return data;
}
Bulk.prototype.getHeaders = function(order){
var self = this;
if(!order) order = self.getOrder('deflt');
var data = order.map(function(el){
return self.getName(el);
});
return data;
}
function formatStats(str){
str = str.replace(/ /g, ' ').replace(/^\s*/g, '').replace(/\s*,\s*/g, ', ').replace(/\s*$/g, '');
return str;
}
function formatDetails(str){
str = str.replace(/<\/td><\/tr><tr><td>/g, ', ').replace(/<\/td><td>/g, ' ').replace(/<.*?>/g, '');
return str;
}
function template_() {
return {
url: {
name: 'URL',
type: 'string',
value: ''
},
user: {
name: 'USER',
value: '',
type: 'string',
regExp: /<div class="profile-name">(.*?)<\/div>/g
},
days: {
name: 'DAYS',
type: 'float',
value: 0,
regExp: /(\d+)\s*day/
},
total_edits: {
name: 'TOTAL EDITS',
type: 'float',
value: 0,
regExp: /(\d+)\s*total\s*edit/
},
approved: {
name: 'APPROVED',
type: 'float',
value: 0,
regExp: /(\d+)\s*approv/
},
reviews: {
name: 'REVIEWS',
type: 'float',
value: 0,
regExp: /(\d+)\s*review/
},
road_length: {
name: 'ROAD LENGTH (KM)',
type: 'float',
value: 0,
regExp: /Road\s*length\s*\(km\)\s*(\d+\.?\d*)/
},
points_interest: {
name: 'POINTS OF INTEREST',
type: 'float',
value: 0,
regExp: /interest\s*(\d+\.?\d*)/
},
business_listings: {
name: 'BUSINESS LISTINGS',
type: 'float',
value: 0,
regExp: /listings\s*(\d+\.?\d*)/
},
regions: {
name: 'REGIONS (KM2)',
type: 'float',
value: 0,
regExp: /egions\s*\(km2\)\s*(\d+\.?\d*)/
},
feature_edits: {
name: 'FEATURE_EDITS',
type: 'float',
value: 0,
regExp: /edits\s*(\d+\.?\d*)/
},
stats: {
name: 'STATS',
type: 'string',
value: '',
regExp: /<div class="time_period_large">(.*?)<\/div>/g
},
details: {
name: 'DETAILS',
type: 'string',
value: '',
regExp: /<div id="div\-mini\-dashboard\-expanded"><table>(.*?)<\/table>/g
},
fetch: {
value: ''
},
orders: {
deflt:['url',
'user','days',
'total_edits',
'approved',
'reviews',
'road_length',
'points_interest',
'business_listings',
'regions',
'feature_edits',
'stats',
'details']}
};
}
function testB(){
var b = new Bulk(template_());
b.setProperty('url', 'https://www.google.com/mapmaker?gw=66&uid=205621217155967615962')
.fetch()
.executor('user', 'fetch', 40000)
.executor('stats', 'fetch', null, formatStats)
.executor('details', 'fetch', null, formatDetails)
.executor('days', 'stats', 0)
.executor('total_edits', 'stats', 0)
.executor('approved', 'stats', 0)
.executor('reviews', 'stats', 0)
.executor('road_length', 'details', 0)
.executor('points_interest', 'details', 0)
.executor('business_listings', 'details', 0)
.executor('regions', 'details', 0)
.executor('feature_edits', 'details', 0);
Logger.log(b.getHeaders());
Logger.log(b.getData());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment