/
index.js
executable file
·315 lines (283 loc) · 8.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//required modules: node-expat, node-inflow, find the require of them and fix the paths
require.paths.push(__dirname+'/../'); // here is my mouls arae located, /root_folder/deps/modulename
var mongodb=require('./mongodb');
var expat=require('node-expat');
var merger=require('nodejs-clone-extend/merger');
var httprequest = require('node-utils/request/lib/main');
var self=this;
self.collection=null; // set it after initalization
function getspecs(headers,callback)
{
self.find(headers,
function(specs)
{
var screensize={};
//console.log(require('sys').inspect(specs.uaprof.RDF.Profile.component.HardwarePlatform,true,2));
if(specs.uaprof)
{
var size=specs.uaprof.RDF[0].component.HardwarePlatform.ScreenSize._value || "340x620";
//var size=specs.uaprof.RDF.Profile.component.HardwarePlatform.ScreenSize._value || "340x620";
size=size.split("x");
screensize.width=size[0];
screensize.height=size[1];
}
else if(specs.wurfl)
{
screensize.width=specs.wurfl.group.display.resolution_width;
screensize.height=specs.wurfl.group.display.resolution_height;
}
screensize.width=screensize.width||340;
screensize.height=screensize.height||620;
specs.screensize=screensize;
//console.log(require('sys').inspect(screensize,true,2));
callback(specs);
});
}this.getspecs=getspecs;
function find(headers,callback)
{
var agent=headers['x-device-user-agent']||headers['user-agent']||"";
var uaprofile=headers['x-wap-profile']||headers['profile']||"";
if(headers['opt'])
{
var match=/ns=\d+/.exec(headers['opt']);
if(match!==null)
{
uaprofile= headers[match[0].substring(3,match[0].length)+'-profile'] || uaprofile;
}
}
if(uaprofile.charAt(0)=='"'||uaprofile.charAt(0)=='\'')
{
uaprofile=uaprofile.substring(1,uaprofile.length-1);
}
var xhtmldevice=false;
if (headers['accept'])
{
xhtmldevice=(headers['accept'].indexOf("application/vnd.wap.xhtml+xml")!=-1 ||
headers['accept'].indexOf("application/xhtml+xml")!=-1 ||
headers['accept'].indexOf("application/text+html")!=-1);
}
if(uaprofile!="")
{
get_profile(uaprofile,function(data){
callback({'uaprof':data,xhtml:xhtmldevice});
});
}
else
{
self.findagent(agent,function(data){
callback({'wurfl':data,xhtml:xhtmldevice});
});
}
};this.find=find;
function get_profile(profileurl,callback)
{
download_profile(profileurl,callback);
}this.get_profile=get_profile;
function download_profile(profileurl,callback)
{
httprequest({
method:'GET',
uri:profileurl
},
function complete(error,response,data)
{
var parser=new expat.parser(); // new instance of parser
//console.log(profileurl);
//console.log(data);
parser.parse(data);
var specs=parser.root;
if(specs.RDF)
{
// array to object implementation:
if(!(specs.RDF instanceof Array)&&specs.RDF.Description)
{
specs.RDF=[specs.RDF.Description];
}
for(var n=0,j=specs.RDF.length;n<j;n++)
{
if(specs.RDF[n].component)
{
// array to object implementation:
if(!(specs.RDF[n].component instanceof Array)&&specs.RDF[n].component.Description)
specs.RDF[n].component=[specs.RDF[n].component.Description];
var arr=specs.RDF[n].component;
var obj={};
for(var i=0,l=arr.length;i<l;i++)
{
obj[specs.RDF[n].component[i].Description.ID]=specs.RDF[n].component[i].Description;
}
specs.RDF[n].component=obj;
}
}
var arr=specs.RDF;
var obj={0:specs.RDF[0]};
for(var i=0,l=arr.length;i<l;i++)
{
obj[specs.RDF[i].ID]=specs.RDF[i];
}
specs.RDF=obj;
}
delete parser;
if(error)
callback(false);
else
callback(specs);
});
}this.download_profile=download_profile;
function save_profile(callback)
{
}
function findagent(agent,callback)
{
//test://var agent="Mozilla/5.0 (Linux; U; Android 2.2; en-us; PC36100 Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
mongodb.findagent(self.collection,agent,function(device)
{
if(device===false) callback(false);
else if(device.fall_back===undefined) callback(device);
else
{
var devices=[];
find_whileloop(devices,device.fall_back,function ()
{
//console.log('len='+devices.length);
var resultdevice=devices[devices.length-1];
for(var i=devices.length-2;i>-1;i--)
{
merger.extend(resultdevice,devices[i]); // maybe to limit it to two first levels: the groups and the capabilities
}
callback(resultdevice);
});
}
});
};this.findagent=findagent;
function find_whileloop(devices,id,callback)
{
mongodb.findid(self.collection,id,function(device)
{
if(device!==false)devices.push(device);
if(device.fall_back!==undefined)
{
find_whileloop(devices,device.fall_back,callback);
}
else
{
callback();
}
});
}
//todo: develop option to search backwards from capability to device
//this.findagent=mongodb.findagent; //mongodb.findid(collection,id,callback)
//this.findid=mongodb.findid;
function loadxml(callback)
{
require('fs').readFile(__dirname+'/wurfl-latest.xml', 'utf-8',function (err, data) {
if (err) throw err;
var xmlparser=new expat.parser();
xmlparser.parse(data);
self.data=xmlparser.root;
callback();
});
}this.loadxml=loadxml;
function download(callback) // not sure if it works but it is an idea
{
var sys = require('sys'),
exec = require('child_process').exec,
child;
child = exec(__dirname+'/download.sh',
function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
}
callback();
});
}this.download=download;
function savetodb(callback)
{
mongodb.connect("t999_phonespecs",function (collection,db){
//expat.inspect(self.data.wurfl.devices);
var inflow= require('../node-inflow');
var devices=self.data.wurfl.devices.device;
inflow.each({},devices,function(device,key)
{
next=this;
//change the structure from arrays to objects
if(device.group===undefined)
{
// fallback devices do not contain group
//console.log(require('sys').inspect(device,true,3));
}
else
{
for(var n=0,l=device.group.length;n<l;n++)
{
//console.log(n);
device.group[n].capability=expat.array_to_object(device.group[n].capability,'name');
}
device.group=expat.array_to_object(device.group,'id');
}
//console.log(require('sys').inspect(device,true,3));
//db.close();
//process.exit();
// save it
mongodb.savereplace(collection,{id:device.id},device,function ()
{
console.log(key);
next();
});
},function(){
console.log('mongodb save done');
db.close();
callback();
});
//var persist=require('./persist');
//persist.save(self.data,__dirname+"/wurfl-latest.js",function (){
//console.log('wurfl-latest.js saved');
//});
});
};this.savetodb=savetodb;
function update(callback)
{
console.log("downloading");
download(function (){
console.log('loading wurfl-latest.xml');
loadxml(function(){
console.log('saving to database');
savetodb(function(){
console.log("update done");
if(callback) callback();
});
});
});
}this.update=update;
//update(); // uncomment this and execute: node index.js
function test()
{
mongodb.connect("t999_phonespecs",function (collection,db)
{
self.collection=collection;
mongodb.index(self.collection,function (){
var headers={};
//findagent("Mozilla/5.0 (Linux; U; Android 2.2; en-us; PC36100 Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
find({'x-wap-profile':"http://nds1.nds.nokia.com/uaprof/N6230ir200.xml"},
function(specs){
var screensize={};
//console.log(require('sys').inspect(specs.uaprof.RDF.Profile.component.HardwarePlatform,true,2));
if(specs.uaprof)
{
var size=specs.uaprof.RDF.Profile.component.HardwarePlatform.ScreenSize._value || "340x620";
size=size.split("x");
screensize.width=size[0];
screensize.height=size[1];
}
else
{
screensize.width=specs.wurfl.group.display.resolution_width;
screensize.height=specs.wurfl.group.display.resolution_height;
}
console.log(require('sys').inspect(screensize,true,2));
db.close();
});
});
});
};this.test=test;
//test();