-
Notifications
You must be signed in to change notification settings - Fork 1
/
coho-proxy.pl
executable file
·606 lines (400 loc) · 17.6 KB
/
coho-proxy.pl
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#!/usr/bin/env perl
=head1 The Tyee Public API Overview
The Tyee Public API currently enables you to programatically access stories published on L<http://thetyee.ca/> going back to roughly 2003. Each story object includes a number of properties, including: titles, teasers, bylines, and meta data like the article's section or keywords (see the JSON example linked below for a relatively complete reference). As the API improves, it will provide additional meta data, like name entities, e.g.: people, places, and so on.
This is a very early release of this public API. Things might change, so be sure to join the mailing list for updates: link TK.
Currently, the API is limited to just stories. Eventually, we hope endpoints for C<contributors>, C<commenters>, C<comments>, and C<series>. If you have suggestions, please post a note in the mailing list.
This API is the same API that we use to provide data to The Tyee's HTML5 mobile app (L<http://thetyee.ca/News/2011/04/11/MobileApp/>). As we build out more serices on top of this API, we hope to expand the scope of what it can do.
--The Tyee
=head2 Copyright notice
=item * Stories
Except where otherwise noted, content made available via this API is copyright The Tyee and the attributed authors.
=item * Photographs & illustrations
Except where otherwise noted, images on this site are copyright the attributed photographer/illustrator or representative agency.
=item * Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License. You are free to copy, distribute and transmit the work provided you attribute the work, do not use the work for commercial purposes, not alter, transform, or build upon this work.
=item * Common sense note
Obviously, the API is being made public so that people can specifically build, experiement, and innovate upon it (the API). At the same time, we ask that you respect the original form of the content -- the stories, the photos, etc. -- by not altering them from the original.
=head2 Terms of Use
=over
=item * API authentication and rate limiting
At the moment, we are not requiring authentication to the API, nor are we limiting the number of times that you can make a request to the API. The extent to which we do, or do not, implement these rests with those of you who use the API. If resources are used wisely and respectfully, there may not be a need to implement either.
=item * Commerical use
How do we feel about people using the API to create a commercial product, i.e.: an iOS app that they charge for? Not necessarily a bad thing, as long as they don't imply any endorsement by The Tyee.
=item * Be a good Internet citizen
Instead of getting the laywers involved, we would like to simply ask that you be a good Internet citizen: use the API as you would if you were the one providing it. Be respectful and be courteous. Join the mailing list and let us know what you're working on. Give us a heads-up and we'll do the same for API changes, sevice outages, and so on. Most importantly, engage with the spirit of making the Ineternet better for everyone.
=back
=head2 Basic API information
B<URL:> L<http://api.thetyee.ca/v1>
B<Formats:> json, jsonp
B<HTTP Method:> GET
B<Requires Authentication:> Not currently. Might in the future.
B<API rate limit:> No limits currently. Rate limits will apply in future versions.
B<Callback:> Available on all endpoints. If supplied, the response will use the JSONP format with a callback of the given name.
Example JSON representation of a story: https://gist.github.com/965479
=cut
use local::lib '/var/home/tyeeapi/perl5';
use Mojolicious::Lite;
use Mojo::Util qw(b64_encode url_escape url_unescape encode decode);
use JSON;
use LWP::UserAgent;
use HTTP::Request::Common;
use Date::Format;
get '/' => sub {
my $self = shift;
$self->render_text(
'Welcome to The Tyee Public API. You probably want to start <a href="/">here</a>.'
);
};
=head1 Stories, latest, grouped by section
Returns a list of stories, ordered reverse chronologically by cover date, grouped by major site section, e.g.:
Today's Features, News, Opinion, etc.
Returns four stories per group.
B<URL:> L<http://api.thetyee.ca/v1/latest/grouped>
B<Formats:> json, jsonp
B<HTTP Method:> GET
B<Requires Authentication:> Not currently. Might in the future.
B<API rate limit:> No limits currently. Rate limits will apply in future versions.
B<Parameters:> None.
=cut
get '/latest/grouped' => sub {
my $m = shift;
my @time = gmtime( time );
my $now = strftime( "%Y-%m-%dT%TZ", @time );
my $fetch_subset = sub {
my $query = shift;
my $tag = shift;
my $ua = LWP::UserAgent->new;
my $r = $ua->post( "http://localhost:9200/tyee/story/_search",
Content => encode_json( $query ) );
my $j = decode_json( $r->content );
foreach my $node ( @{ $j->{'hits'}->{'hits'} } ) {
$node->{'_source'}->{'group'} = $tag;
}
return $j;
};
# today's features
my $elastic = {
from => 0,
size => 4,
"sort" => [ { "storyDate" => { "reverse" => 1 } } ],
query => {
field => { topics => "Opinion News Mediacheck Culture Solutions National" }
}
};
my $structure = &$fetch_subset( $elastic, "Today's Features" );
# news sections
my %titles = ( Arts => "Arts and Culture" );
$elastic->{'size'} = 6;
foreach my $section ( qw/News National Opinion Mediacheck Culture Solutions/ ) {
$elastic->{'query'} = { field => { topics => $section } };
my $j = &$fetch_subset( $elastic, $titles{$section} || $section );
push @{ $structure->{'hits'}->{'hits'} }, @{ $j->{'hits'}->{'hits'} };
}
# package up and render
my $json = nice_encode_json( $structure );
proxy_render( $m, $json );
};
=head1 Stories, latest
Returns stories publish on The Tyee, sorted in reverse chonological order by cover date.
B<URL:> L<http://api.thetyee.ca/v1/latest/>[number]
B<Formats:> json, jsonp
B<HTTP Method:> GET
B<Requires Authentication:> Not currently. Might in the future.
B<API rate limit:> No limits currently. Rate limits will apply in future versions.
B<Parameters:> Number of stories to return (maximum 50). Optional.
=cut
get '/latest/:count' => [ count => qr/\d+/ ] => { count => 20 } => sub {
my $m = shift;
my $count = $m->param( "count" );
$count = 50 if $count > 50;
my @time = gmtime( time );
my $now = strftime( "%Y-%m-%dT%TZ", @time );
my $ua = LWP::UserAgent->new;
my $r = $ua->post( "http://localhost:9200/tyee/story/_search",
Content => '{ "from": 0, "size": '
. $count
. ', "sort" : [ { "storyDate" : { "reverse" : true } } ], "query" : { "range" : { "storyDate": { "to" : "'
. $now
. '"} } } }' );
proxy_render( $m, json_to_json( $r->content ) );
};
=head1 Stories, latest, teasers only
Returns a list of the latest stories published on The Tyee, sorted in reverse chonological order by cover date, but only provides the following properties: title, teaser, _type, and _id.
B<URL:> L<http://api.thetyee.ca/v1/latest/short/>[number]
B<Formats:> json, jsonp
B<HTTP Method:> GET
B<Requires Authentication:> Not currently. Might in the future.
B<API rate limit:> No limits currently. Rate limits will apply in future versions.
B<Parameters:> Number of stories to return (maximum 50). Optional.
=cut
get '/latest/short/:count' => [ count => qr/\d+/ ] => { count => 20 } => sub {
my $m = shift;
my $count = $m->param( "count" );
$count = 50 if $count > 50;
my @time = gmtime( time );
my $now = strftime( "%Y-%m-%dT%TZ", @time );
my $ua = LWP::UserAgent->new;
my $r = $ua->post( "http://localhost:9200/tyee/story/_search",
Content =>
'{ "script_fields": {"title": {"script":"_source.title"}, "teaser": {"script":"_source.teaser"}}, "from": 0, "size": '
. $count
. ', "sort" : [ { "storyDate" : { "reverse" : true } } ], "query" : { "range" : { "storyDate": { "to" : "'
. $now
. '"} } } }' );
proxy_render( $m, json_to_json( $r->content ) );
};
=head1 Story
Returns a single story object. Example of a response: https://gist.github.com/965479
B<URL:> L<http://api.thetyee.ca/v1/story/>[uuid]
B<Formats:> json, jsonp
B<HTTP Method:> GET
B<Requires Authentication:> Not currently. Might in the future.
B<API rate limit:> No limits currently. Rate limits will apply in future versions.
B<Parameters:> The UUID of the story to return. Required.
=cut
get '/story/:uuid' => sub {
my $m = shift;
my $ua = LWP::UserAgent->new;
my $r = $ua->post( "http://localhost:9200/tyee/story/_search",
Content => '{ "query": {"term": { "_id": "'
. $m->param( "uuid" )
. '"} } }' );
proxy_render( $m, json_to_json( $r->content ) );
};
get '/search/path/(*query)' => sub {
my $m = shift;
my $ua = LWP::UserAgent->new;
my $elastic = {
size => 25,
query => { field => { path => $m->param( "query" ) } }
};
my $r = $ua->post(
"http://localhost:9200/tyee/story/_search",
Content => encode_json( $elastic )
);
proxy_render( $m, json_to_json( $r->content ) );
};
get '/search/(*query)' => sub {
my $m = shift;
my $ua = LWP::UserAgent->new;
my $elastic = {
size => 25,
query => { field => { title => $m->param( "query" ) } }
};
my $r = $ua->post(
"http://localhost:9200/tyee/story/_search",
Content => encode_json( $elastic )
);
proxy_render( $m, json_to_json( $r->content ) );
};
=head1 Search for tags
Returns stories that contain keyword. Use quotes to denote full term or will search for both terms, i.e. api.thetyee.ca/v1/keywordsearch/%22Christy%20Clark%22 returns correctly where http://preview.api.thetyee.ca/v1/keywordsearch/Christy%20Clark will return Christy Or Clark stories
B<URL:> L<http://api.thetyee.ca/v1/story/>[uuid]
B<Formats:> json, jsonp
B<HTTP Method:> GET
B<Requires Authentication:> Not currently. Might in the future.
B<API rate limit:> No limits currently. Rate limits will apply in future versions.
B<Parameters:> The UUID of the story to return. Required.
=cut
get '/keywordsearch/(*query)' => sub {
my $m = shift;
my $ua = LWP::UserAgent->new;
my $elastic = {
"sort" => [ { "storyDate" => { "reverse" => 1 } } ],
size => 25,
query => { field => { keywords => $m->param( "query" ) } }
};
my $r = $ua->post(
"http://localhost:9200/tyee/story/_search",
Content => encode_json( $elastic )
);
proxy_render( $m, json_to_json( $r->content ) );
};
=head1 Search for tags
Returns stories that contain keyword. Use quotes to denote full term or will search for both terms, i.e. api.thetyee.ca/v1/keywordsearch/%22Christy%20Clark%22 returns correctly where http://preview.api.thetyee.ca/v1/keywordsearch/Christy%20Clark will return Christy Or Clark stories
B<URL:> L<http://api.thetyee.ca/v1/story/>[uuid]
B<Formats:> json, jsonp
B<HTTP Method:> GET
B<Requires Authentication:> Not currently. Might in the future.
B<API rate limit:> No limits currently. Rate limits will apply in future versions.
B<Parameters:> The UUID of the story to return. Required.
=cut
get '/author/:author/(:size)/(:from)' => sub {
my $m = shift;
my $author = $m->param( "author" );
my $from = $m->param( "from" );
my $size = $m->param( "size" );
my $ua = LWP::UserAgent->new;
use URI::Escape;
$author = uri_unescape($author);
# topic title remapping
my %remap = ( Arts => "Arts and Culture",
"Arts & Culture" => "Arts and Culture",
);
my $elastic = {
"from" => $from,
"size" => $size,
"sort" => [ { "storyDate" => { "reverse" => 1 } } ],
query => { field => { byline => '"' . ($author) . '"' } }
};
my $r = $ua->post(
"http://localhost:9200/tyee/story/_search",
Content => encode_json( $elastic )
);
proxy_render( $m, json_to_json( $r->content ) );
};
=head1 Stories by topic
Returns a list of stories (maximum 25) by section or topic.
B<URL:> L<http://api.thetyee.ca/v1/topic/>[topic]
B<Formats:> json, jsonp
B<HTTP Method:> GET
B<Requires Authentication:> Not currently. Might in the future.
B<API rate limit:> No limits currently. Rate limits will apply in future versions.
B<Parameters:> Topic. Required. (Valid parameters are: News, Opinion, Mediacheck, Arts and Culture, Books, Life, 2010 Olympics, Education, Energy, Environment, Federal Election 2011, Film, Food, Food + Farming, Gender + Sexuality, Health, Housing, Labour, Music, Photo Essays, Podcasts, Politics, Rights + Justice, Science + Tech, Transportation, Travel, Tyee News, Urban Design + Architecture, Video.)
=cut
get '/topic/:topic/(:size)/(:from)' => sub {
my $m = shift;
my $topic = $m->param( "topic" );
my $from = $m->param( "from" );
my $size = $m->param( "size" );
my $ua = LWP::UserAgent->new;
use URI::Escape;
$topic = uri_unescape($topic);
# topic title remapping
my %remap = ( Arts => "Arts and Culture",
"Arts & Culture" => "Arts and Culture",
);
my $elastic = {
"from" => $from,
"size" => $size,
"sort" => [ { "storyDate" => { "reverse" => 1 } } ],
query => { field => { topics => '"' . ($remap{$topic} || $topic) . '"' } }
};
my $r = $ua->post(
"http://localhost:9200/tyee/story/_search",
Content => encode_json( $elastic )
);
proxy_render( $m, json_to_json( $r->content ) );
};
get '/keyword/:keyword/(:size)/(:from)' => sub {
my $m = shift;
my $keyword = $m->param( "keyword" );
my $from = $m->param( "from" );
my $size = $m->param( "size" );
my $ua = LWP::UserAgent->new;
my $elastic = {
"from" => $from,
"size" => $size,
"sort" => [ { "storyDate" => { "reverse" => 1 } } ],
query => { query_string => {
query => $m->param( "keyword" ),
fields => "keywords"
} }
};
my $r = $ua->post(
"http://localhost:9200/tyee/story/_search",
Content => encode_json( $elastic )
);
proxy_render( $m, json_to_json( $r->content ) );
use Data::Dumper;
# print "hello";
# print Dumper($r);
#$m->render(
# text => $r,
# format => ( "html" )
# );
};
get '/searchme/:terms/(:size)/(:from)' => sub {
my $m = shift;
my $terms = $m->param( "terms" );
my $from = $m->param( "from" );
my $size = $m->param( "size" );
my $ua = LWP::UserAgent->new;
my $elastic = {
"from" => $from,
"size" => $size,
"sort" => [ { "storyDate" => { "reverse" => 1 } } ],
query => { query_string => {
query => $terms,
default_operator => "AND",
# query => "six ways to end homelessness"
# fields => "textWithHtml,title,byline,teaser",
# default_field => "_all"
} }
};
my $r = $ua->post(
"http://localhost:9200/tyee/story/_search",
Content => encode_json( $elastic )
);
my $content = json_to_json( $r->content);
proxy_render( $m, $content );
#use Data::Dumper;
# print "hello";
# print Dumper($r);
#$m->render(
# text => $r,
# format => ( "html" )
# );
};
get '/latest/blogs' => sub {
my $m = shift;
my $ua = LWP::UserAgent->new;
my $elastic = {
"size" => 25,
"sort" => [ { "storyDate" => { "reverse" => 1 } } ],
query => { field => { story_type => "blog_entry" } }
};
my $r = $ua->post(
"http://localhost:9200/tyee/story/_search",
Content => encode_json( $elastic )
);
proxy_render( $m, json_to_json( $r->content ) );
};
=head1 Story by path
Returns a story by path.
B<URL:> L<http://api.thetyee.ca/v1/path/>[path]
B<Formats:> json, jsonp
B<HTTP Method:> GET
B<Requires Authentication:> Not currently. Might in the future.
B<API rate limit:> No limits currently. Rate limits will apply in future versions.
B<Parameters:> Path. Required.
=cut
get '/path/:path' => sub {
my $m = shift;
my $path = $m->param( "path" );
my $ua = LWP::UserAgent->new;
my $elastic = {
query => { field => { path => "' . $path . '" } }
};
my $r = $ua->post(
"http://localhost:9200/tyee/story/_search",
Content => encode_json( $elastic )
);
proxy_render( $m, json_to_json( $r->content ) );
};
app->types->type( js => 'application/x-javascript; charset=utf-8' );
app->types->type( json => 'application/json; charset=utf-8' );
app->start;
### helper functions ###
# Takes a JSON string and outputs a nicely encoded JSON string
sub json_to_json {
my $json = shift;
return nice_encode_json( decode_json( $json ) );
}
# Nicely encodes an object to a JSON string. Unicode characters too!
sub nice_encode_json {
my $obj = shift;
return JSON->new->ascii( 1 )->pretty( 1 )->encode( $obj );
}
# Tells Mojo to render some JSON.
# Handles the JSON/JSONP formatting too.
sub proxy_render {
my $m = shift;
my $json = shift;
$json = $m->param( "callback" ) . "(" . $json . ");"
if $m->param( "callback" );
$m->render(
text => $json,
format => ( $m->param( "callback" ) ? "js" : "json" )
);
}