source: comics/PennyArcade/comic.js @ 1:4e64bf1747ce

Revision 1:4e64bf1747ce, 5.6 KB checked in by lukacu, 14 years ago (diff)

Core project and comics migration

Line 
1/*
2 * A template for JavaScript comic plugin for WebStrips
3 *
4 * License:
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 * Public License for more details.
15 *
16 * http://www.opensource.org/licenses/gpl-license.php
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation,
20 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24/*
25 * place any global variables and support functions here ...
26 */
27
28var NEWEST_RE = new RegExp("<input type=\"hidden\" name=\"Date\" value=\"(\\d{4})(\\d{2})(\\d{2})\"");
29
30var FIRST_RE = new RegExp("first\"><a href=\"/comic/(\\d{4})/(\\d{1,2})/(\\d{1,2})/\"");
31   
32var NEXT_RE = new RegExp("next\"><a href=\"/comic/(\\d{4})/(\\d{1,2})/(\\d{1,2})/\"");
33   
34var PREV_RE = new RegExp("back\"><a href=\"/comic/(\\d{4})/(\\d{1,2})/(\\d{1,2})/\"");
35
36var PATTERN_RE = new RegExp("<div class=\"body\">[\n\t ]+<img src\\=\"([^\"]*)\" +alt\\=\"([^\"]*)\"");
37
38var ARCHIVE_RE = new RegExp("option value=\"http\\://www\\.penny-arcade\\.com/comic/(\\d{4})/(\\d{2})/(\\d{2})\">[\n\t ]+[0-9\\/]+ ([^\n\t<]+)", "g");
39
40var DATE_RE = new RegExp("(\\d\\d\\d\\d)-(\\d{1,2})-(\\d{1,2})");
41
42var newestDate= null;
43
44function parseDate(str) {
45
46    var m = str.match(DATE_RE);
47
48    if (m) {
49        d = new Date(m[1] + "/" + m[2] + "/" + m[3] + " 00:00:00");
50        d.setMilliseconds(0);
51        return d;
52   
53    } else return new Date();
54   
55}
56
57/*
58 * Function title(id) should return a title for the comic strip with the given id.
59 */
60function title(id) {
61
62    c = parseDate(id);
63   
64    s = get(format("http://www.penny-arcade.com/comic/%1$tY/%1$tm/%1$td", c));
65   
66    p = PATTERN_RE.exec(s);
67   
68    if (p) {
69        return p[2];
70    }
71
72    return null;
73
74}
75
76/*
77 * Function image(id) should return a URL of an image for the comic strip with the given id.
78 */
79function image(id) {
80               
81    c = parseDate(id);
82   
83    s = get(format("http://www.penny-arcade.com/comic/%1$tY/%1$tm/%1$td", c));
84   
85    p = PATTERN_RE.exec(s);
86   
87    if (p) {
88        if (p[1].indexOf("http://") == 0)
89            return p[1];
90
91        // TODO: I do not know if that still works ... i hope that all urls are absolute now.
92        return "http://art.penny-arcade.com" + p[1];
93
94    }
95
96    return null;
97   
98}
99
100/*
101 * Function previous(id) should return the id of a comic strip that is prior to the comic strip
102 * with a given id or null if there is no such comic.
103 */
104function previous(id) {
105    c = parseDate(id);
106   
107    s = get(format("http://www.penny-arcade.com/comic/%1$tY/%1$tm/%1$td", c));
108   
109    p = PREV_RE.exec(s);
110   
111    if (p) {
112        return format("%1$tY-%1$tm-%1$td", parseDate(p[1] + "-" + p[2] + "-" + p[3]));
113    }
114    return null;
115
116}
117
118/*
119 * Function next(id) should return the id of a comic strip that is next to the comic strip
120 * with a given id or null if there is no such comic.
121 */
122function next(id) {
123
124    // The problem here is that PennyArcade gives an url also for comics that do not exist
125    // yet. And without this check that messes up the system.
126
127    if (newestDate == null)
128        newest();
129
130    c = parseDate(id);
131    if (c - newestDate > -43200000)
132        return null;
133
134    s = get(format("http://www.penny-arcade.com/comic/%1$tY/%1$tm/%1$td", c));
135
136    n = NEXT_RE.exec(s);
137
138    if (n) {
139        return format("%1$tY-%1$tm-%1$td", parseDate(n[1] + "-" + n[2] + "-" + n[3]));
140    }
141
142    return null;
143}
144
145/*
146 * Function first() should return the id of the first comic strip or null if that information
147 * cannot be obtained for this comic.
148 */
149function first() {
150    s = get("http://www.penny-arcade.com/comic");
151
152    m = FIRST_RE.exec(s);
153
154    if (m) {
155
156        return format("%1$tY-%1$tm-%1$td", parseDate(m[1] + "-" + m[2] + "-" + m[3]));
157    }
158   
159    return null;
160}
161
162/*
163 * Function newest() should return the id of the most recent comic strip or null if that information
164 * cannot be obtained for this comic.
165 */
166function newest() {
167   
168    s = get("http://www.penny-arcade.com/comic");
169   
170    m = NEWEST_RE.exec(s);
171   
172    if (m) {
173        newestDate = parseDate(m[1] + "-" + m[2] + "-" + m[3]);
174        return format("%1$tY-%1$tm-%1$td", newestDate);
175    }
176   
177    return null;
178
179}
180
181/*
182 * if the information is present you can also implement the following functionality currently
183 * supported by WebStrips (uncoment the functions)
184 */
185
186/*
187 * Function link(id) should return an url pointing to the page where the comic strip with a
188 * given id was retrieved from.
189 */
190
191function link(id) {
192    c = parseDate(id);
193   
194    return format("http://www.penny-arcade.com/comic/%1$tY/%1$tm/%1$td", c);
195}
196
197
198/*
199 * Function archive(id) should process the archive data from the comic strip with the given id on
200 * and return the possible new comics using archiveInsert(id, title) and archiveAppend(id, title)
201 * functions that will only be allowed to call if the archive function is invoked.
202 *
203 * archiveInsert(id, title) - inserts the archive entry at the beginning of the newly generated archive
204 * segment (The last call to this function will insert an entry to the start of the new segment).
205 *
206 * archiveAppend(id, title) - inserts the archive entry at the end of the newly generated archive
207 * segment (The last call to this function will insert an entry to the end of the new segment and thus
208 * to the end of archive).
209 */
210
211function archive(id) {
212
213    s = get("http://www.penny-arcade.com/archive");
214
215    ARCHIVE_RE.lastIndex = 0;
216
217    while (m = ARCHIVE_RE.exec(s)) {
218       
219        j = m[1] + m[2] + m[3];
220
221        archiveInsert(j, m[4]);
222
223        if (j == id)
224            break;
225    }
226
227}
228
Note: See TracBrowser for help on using the repository browser.