-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlterNet.js
182 lines (161 loc) · 5.8 KB
/
AlterNet.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
{
"translatorID":"ea531652-cdeb-4ec2-940e-627d4b107263",
"translatorType":4,
"label":"AlterNet",
"creator":"Jesse Johnson",
"target":"^http://(?:www\\.)alternet.org",
"minVersion":"1.0.0b4.r1",
"maxVersion":"",
"priority":100,
"inRepository":true,
"lastUpdated":"2008-07-10 06:15:00"
}
function detectWeb(doc, url) {
// identifies articles according to the presence of an article ID
// number in the URL
var index = url.toString().indexOf('.org/') + 5;
index += url.toString().substr(index).indexOf('/');
if (index != -1) {
// ordinary aritcle
var id = url.toString().substr(index + 1, 5);
Zotero.Utilities.cleanString(id);
if (Number(id)) {
return "magazineArticle";
}
//columnist or blog article
index += url.toString().substr(index + 1).indexOf('/');
id = url.toString().substr(index + 2, 5);
Zotero.Utilities.cleanString(id);
if (Number(id) && url.toString().search('blog') == -1) {
return "magazineArticle";
}
else if (Number(id)) {
return "blogPost";
}
}
return null;
}
function scrape(doc, url, title) {
var index = url.toString().indexOf('.org/') + 5;
index += url.toString().substr(index).indexOf('/');
if (index != -1) {
// ordinary aritcle
var id = url.toString().substr(index + 1, 5);
Zotero.Utilities.cleanString(id);
if (Number(id)) {
var newItem = new Zotero.Item("magazineArticle");
}
//columnist or blog article
index += url.toString().substr(index + 1).indexOf('/');
id = url.toString().substr(index + 2, 5);
Zotero.Utilities.cleanString(id);
if (Number(id) && url.toString().search('blog') == -1) {
var newItem = new Zotero.Item("magazineArticle");
}
else if (Number(id)) {
var newItem = new Zotero.Item("blogPost");
}
}
newItem.url = url;
newItem.title = title;
if (newItem.itemType == "magazineArticle") {
newItem.publicationTitle = "AlterNet";
newItem.repository = "alternet.org";
}
else if (newItem.itemType == "blogPost") {
newItem.websiteType = "AlterNet Blog";
}
// general scraping variables
var xpath;
// author
if (newItem.itemType == "magazineArticle") {
xpath = '//p[@class="storybyline"]//a[contains(@href,"author")]';
}
else if (newItem.itemType == "blogPost") {
xpath = '//p[@class="storybyline"]//a[contains(@href,"bloggers")]';
}
temp = doc.evaluate(xpath, doc, null, XPathResult.ANY_TYPE, null).iterateNext();
if (temp) {
var author = Zotero.Utilities.trimInternal(temp.textContent);
if(author.substr(0, 3).toLowerCase() == "by ") {
author = author.substr(3);
}
var authors = author.split(",");
for each (var author in authors) {
newItem.creators.push(Zotero.Utilities.cleanAuthor(author, "author"));
}
}
// date
if (newItem.itemType == "magazineArticle") {
xpath = '//p[@class="storybyline"]//a[contains(@href,"date")]';
temp = doc.evaluate(xpath, doc, null, XPathResult.ANY_TYPE, null).iterateNext();
var date = Zotero.Utilities.strToDate(temp.textContent);
}
else if (newItem.itemType == "blogPost") {
xpath = '//p[@class="storybyline"]/b';
temp = doc.evaluate(xpath, doc, null, XPathResult.ANY_TYPE, null).iterateNext();
var begin = temp.textContent.lastIndexOf(" on ");
temp = temp.textContent.substr(begin + 4);
var date = Zotero.Utilities.strToDate(temp.substr(0, temp.length - 1));
}
if (date != null) {
var strdate;
date.month = date.month + 1;
strdate = date.year + '-';
if (date.month < 10) {
strdate += '0' + date.month;
}
else {
strdate += date.month;
}
if (date.day > 10) {
strdate += '-' + date.day;
}
else {
strdate += '-0' + date.day;
}
newItem.date = strdate;
}
// abstract
xpath = '//div[@class="teaser"]//div[contains(@class,"teaser")]';
temp = doc.evaluate(xpath, doc, null, XPathResult.ANY_TYPE, null).iterateNext();
if (temp) {
newItem.abstractNote = Zotero.Utilities.trimInternal(temp.textContent);
}
// article snapshot
// grabs 5-digit article code from url and uses it to derive printable page url for use in article snapshot
var index = url.toString().indexOf('.org/') + 5;
index += url.toString().substr(index).indexOf('/');
if (index != -1) {
var printurl;
// ordinary article
var id = url.toString().substr(index + 1, 5);
if (Number(id)) {
printurl = "http://www.alternet.org/module/printversion/" + id;
newItem.attachments.push({url:printurl, title:"AlterNet Article Snapshot", mimeType:"text/html"});
}
// columnist article
else {
index += url.toString().substr(index + 1).indexOf('/');
id = url.toString().substr(index + 2, 5);
Zotero.Utilities.cleanString(id);
if (Number(id)) {
printurl = "http://www.alternet.org/module/printversion/" + id;
if (newItem.itemType == "blogPost") {
printurl += "/?type=blog";
}
newItem.attachments.push({url:printurl, title:"AlterNet Article Snapshot", mimeType:"text/html"});
}
}
}
newItem.complete();
}
function doWeb(doc, url) {
// ordinary and columnist articles
var xpath = '//p[@class="storyheadline"]';
var title;
if (title = doc.evaluate(xpath, doc, null, XPathResult.ANY_TYPE, null).iterateNext()) {
scrape(doc, url, title.textContent);
}
return null;
}