-
Notifications
You must be signed in to change notification settings - Fork 0
/
descriptors.js
190 lines (155 loc) · 5.98 KB
/
descriptors.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
var descriptors = {
allPubs: [],
showTextarea: function($elem, $descriptor, val) {
if($elem.hasClass('selected')) {
$descriptor.find('.url').show();
$descriptor.find('textarea').hide();
$elem.removeClass('selected');
} else {
$descriptor.find('.url').hide();
$descriptor.find('textarea').show().val(val);
$elem.addClass('selected');
}
},
assignEvents: function($descriptor) {
var obj = $.data($descriptor, "json");
var json = JSON.stringify(obj, null, 2);
$descriptor.find('.parse a').click(function() {
var $this = $(this);
$this.text('Parsing...')
mypdf.load('pdfs/' + obj.pdf, function(text) {
console.log(text);
// woo extract regex!
var space1 = ' ';
var space2 = ' ';
var arr = text.match(/[\d]+\.([\d]+)?(\.[\d]+)?[ ]+[^\(\)\d\.]+/g);
//var arr = text.match(/[\d]+\.([\d]+)?(\.[\d]+)?[ ]+[A-Z][a-z ]+/g);
//var arr = text.match(/\d[.\d]+\s+[A-Z][^A-Z]+(?!\S)/g);
str = 'couldn\'t match regex';
if(arr) {
str = arr.map(function(d) {
var s = $.trim(d).replace(/ +(?= )/g, '');
var digits = s.match(/[\d]+/g);
var spaces = 0;
if(digits) {
spaces = digits.length - 1;
}
for(var i=0; i<spaces; i++) {
s = ' ' + s;
}
return s;
}).join('\n');
}
descriptors.showTextarea($this, $descriptor, str);
$this.text('Parse')
})
});
$descriptor.find('.json').click(function() {
descriptors.showTextarea($(this), $descriptor, json);
});
},
loadPublication: function(publication) {
var $descriptor = $('.descriptor').clone();
$descriptor.removeClass('descriptor');
$descriptor.show();
$descriptor.find('img').attr('src', 'thumbs/'+ publication.image);
$descriptor.find('[name=item1]').attr('href', publication.url).text(publication.title || publication.subtitle);
$descriptor.find('.url').attr('href', publication.url);
if(publication.pdf) {
$pdf = $descriptor.find('.parse');
$pdf.show();
}
//$descriptor.find('.price').text(publication.publication_year || publication.pages);
$.data($descriptor, 'json', publication);
$('.product-list').append($descriptor);
this.assignEvents($descriptor);
},
getPublicaton: function(url, $html) {
var cont = $html.find('.user-bioversitypublications-pi1');
var desc = cont.find('.descTD');
var publication = {
url: url,
title: $.trim(cont.find('h2:first').text()),
subtitle: $.trim(cont.find('span:first').text()),
image: base + cont.find('#tablePub tr:first td:first img').attr('src')
};
var $pdescs = desc.children('p');
var firstP = $.trim($pdescs.eq(0).text());
// description
if(firstP) {
publication.description = $.trim($pdescs.eq(2).text());
} else {
// description it's at index 1
publication.description = $.trim($pdescs.eq(1).text());
}
// PDF
var $child = desc.children();
if($child.eq(0).is('a')) {
// there's a PDF
publication.pdf = base + $child.eq(0).attr('href');
}
// bottom properties
// get all bolds in desc TD
var $bolds = $('.descTD b, .descTD p[style="font-weight:bold;"]');
$bolds.each(function(idx, elem) {
var $elem = $(elem);
var text = $elem.text();
text = text.toLowerCase();
text = text.replace(':', '');
text = text.replace(/ /g, '_');
text = $.trim(text);
if(text) {
// check whether it's empty or not
var $next = $($elem.get(0).nextSibling);
var value = $.trim($next.text());
if(value) {
publication[text] = value;
}
}
});
// RAW HTML
publication.descriptionHTML = $.trim($('.descTD').html());
this.allPubs.push(publication);
$dump = $('textarea.dump');
$dump.show();
$dump.val(JSON.stringify(this.allPubs, null, 2));
},
getUrls: function($html) {
var urls = $html.find('.publicationBox h4.title a');
urls.each(function(idx, elem) {
var $url = $(elem);
var url = base + $url.attr('href');
// load this url
scraper.get(url, function($html) {
descriptors.getPublicaton(url, $html);
});
});
},
load: function(searchTerm) {
$('.product-list').html('');
for(var i=0; i<pdfs.length; i++) {
var pub = pdfs[i];
if(searchTerm) {
var jsonPub = JSON.stringify(pub);
if(jsonPub.search(new RegExp(searchTerm, "i")) !== -1) { // found it!
descriptors.loadPublication(pub);
}
} else {
descriptors.loadPublication(pub);
}
}
},
getAll: function() {
function descLoad(counter) {
scraper.get(all + '&page=' + counter, function($html) {
descriptors.getUrls($html);
var found = $html.html().match('No publications found')
// get second page only if this page doesn't show
if(!found) { //continue to next page
descLoad(counter + 1);
}
});
}
descLoad(1);
}
};