summaryrefslogtreecommitdiff
path: root/bin/rdf2hours
blob: 9a22aed62ae8e7a8b8d3818f0388880b806037c2 (plain)
  1. #!/usr/bin/perl
  2. #
  3. # Copyright © 2013 Jonas Smedegaard <dr@jones.dk>
  4. # Description: render opening hours webpage from RDF data
  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 3, or (at your option)
  9. # 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
  14. # General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. use 5.010;
  19. use strictures 1;
  20. use utf8::all;
  21. #use DDP alias => 'Dumper';
  22. use DateTimeX::Easy;
  23. use DateTime::Format::W3CDTF;
  24. use Carp;
  25. use Try::Tiny;
  26. use RDF::Query;
  27. use RDF::TrineX::Functions qw[curie model parse];
  28. use HTML::HTML5::Builder qw[:standard];
  29. use HTML::HTML5::Writer;
  30. # TODO: add options --verbose and --debug
  31. # TODO: add override options --theme and (multivalued) --category
  32. my @category = <<LIST =~ m/(\S.*\S)/g;
  33. full-meal food
  34. non-meal food
  35. <http://schema.org/GroceryStore>
  36. non-grocery store
  37. attraction
  38. transport
  39. public service
  40. LIST
  41. # TODO: add override options --from, --through and --theme
  42. #my $globalfrom = DateTimeX::Easy->new('now');
  43. # TODO: teach DTX::Easy / DT::F::Natural DT::F::Flexible words "current" and "present" (as alternatives to this)
  44. my $globalfrom = DateTimeX::Easy->new('first day of this month');
  45. # TODO: teach DTX::Easy / DT::F::Natural DT::F::Flexible these:
  46. # last day of month three months from now
  47. # last day of month in three months
  48. # last day of (the) month three months ahead
  49. # ... in the future
  50. # ... into the future
  51. # ... from now
  52. # ... later <- this last one for changing object, not create new
  53. #my $globalthrough = DateTimeX::Easy->new('last day of month in 3 months');
  54. my $globalthrough = DateTimeX::Easy->new($globalfrom);
  55. $globalthrough->add( months => 4 )->subtract( days => 1 );
  56. # TODO: add Getopt option --preset as shortcut to --category, --from and --through
  57. my $parser = RDF::Trine::Parser::Turtle->new;
  58. my $model = model();
  59. # TODO: throw sensible error if no (non-opt) args provided
  60. while (my $data = shift @ARGV) {
  61. try {
  62. parse($data, using => $parser, into => $model);
  63. } catch {
  64. say STDERR "Failed to parse file \"$data\": $_";
  65. die;
  66. }
  67. }
  68. # compose query for hourspec data at least partly within a "window" of time
  69. # (yes, comparing global dates against opposite spec dates is deliberate)
  70. my $w3c = DateTime::Format::W3CDTF->new;
  71. my $query = qurie( sprintf(<<'SPARQL', $w3c->format_datetime($globalthrough), $w3c->format_datetime($globalfrom) ));
  72. SELECT *
  73. WHERE {
  74. {
  75. ?location
  76. a schema:Place ;
  77. gr:category ?category ;
  78. gr:name ?name ;
  79. gr:hasOpeningHourSpecification ?spec .
  80. ?spec
  81. gr:opens ?opens ;
  82. gr:closes ?closes ;
  83. rdfs:label ?speclabel ;
  84. gr:validFrom ?specfrom ;
  85. gr:validThrough ?specthrough .
  86. OPTIONAL { ?spec rdfs:comment ?speccomment } .
  87. OPTIONAL { ?location gr:description ?locationcomment } .
  88. FILTER ( ?specfrom <= "%s"^^xsd:dateTime ) .
  89. FILTER ( ?specthrough > "%s"^^xsd:dateTime )
  90. } UNION {
  91. ?location
  92. gr:category ?category ;
  93. gr:name ?name ;
  94. gr:hasOpeningHourSpecification ?spec .
  95. ?spec
  96. gr:opens ?opens ;
  97. gr:closes ?closes .
  98. OPTIONAL {
  99. ?spec
  100. rdfs:label ?speclabel ;
  101. gr:validFrom ?specfrom ;
  102. gr:validThrough ?specthrough .
  103. } .
  104. OPTIONAL { ?spec rdfs:comment ?speccomment } .
  105. OPTIONAL { ?location gr:description ?locationcomment } .
  106. FILTER (!bound(?specfrom)) .
  107. FILTER (!bound(?specthrough))
  108. }
  109. }
  110. SPARQL
  111. # TODO: sort using SPARQL instead of perl
  112. # ORDER BY ?category ?name ?specfrom ?specthrough ?opens ?closes
  113. my %data;
  114. my $iterator = $query->execute($model);
  115. while ( my $row = $iterator->next ) {
  116. my $category = $row->{category}->as_string;
  117. my $name = $row->{name}->literal_value;
  118. my $locationcomment = $row->{locationcomment} ? $row->{locationcomment}->literal_value : '';
  119. my $speclabel = $row->{speclabel} ? $row->{speclabel}->literal_value : '';
  120. # Strip surrounding quotes
  121. $category =~ s/^"(.*)"$/$1/;
  122. # merge identically named locations, tying varying descriptions to spec instead
  123. # my $name_key = titledescribe( $name, $locationcomment );
  124. # my $specbundle_key = $speclabel;
  125. my $name_key = $name;
  126. my $specbundle_key = titledescribe( $speclabel, $locationcomment );
  127. my $specfrom = $row->{specfrom} ? $row->{specfrom}->datetime : '';
  128. my $specthrough = $row->{specthrough} ? $row->{specthrough}->datetime : '';
  129. my $speccomment = $row->{speccomment} ? $row->{speccomment}->literal_value : '';
  130. # TODO: use DateTime objects instead.
  131. my $opens = $row->{opens}->literal_value;
  132. $opens =~ s/:\d\d$//;
  133. my $closes = $row->{closes}->literal_value;
  134. $closes =~ s/:\d\d$//;
  135. $closes =~ s/^23:59/24/;
  136. my @weekdays;
  137. my $iterator = $model->get_statements($row->{spec}, curie('gr_hasOpeningHoursDayOfWeek'), undef);
  138. while (my $statement = $iterator->next) {
  139. my $label = $statement->object;
  140. given ($label->as_string) {
  141. when (/Monday/) { push @weekdays, 1 };
  142. when (/Tuesday/) { push @weekdays, 2 };
  143. when (/Wednesday/) { push @weekdays, 3 };
  144. when (/Thursday/) { push @weekdays, 4 };
  145. when (/Friday/) { push @weekdays, 5 };
  146. when (/Saturday/) { push @weekdays, 6 };
  147. when (/Sunday/) { push @weekdays, 7 };
  148. when (/PublicHolidays/) { push @weekdays, 8 };
  149. default { die "failed to parse weekday: ", $label->as_string };
  150. }
  151. }
  152. my $weekdays = join ', ', sort @weekdays;
  153. $weekdays = 0 unless ($weekdays);
  154. my $weekdays_key = titledescribe( $weekdays, $speccomment );
  155. $data{$category}{$name_key}{name} = $name;
  156. $data{$category}{$name_key}{specbundle}{$specbundle_key}{locationcomment} = $locationcomment;
  157. $data{$category}{$name_key}{specbundle}{$specbundle_key}{speclabel} = $speclabel;
  158. $data{$category}{$name_key}{specbundle}{$specbundle_key}{specfrom} = $specfrom;
  159. $data{$category}{$name_key}{specbundle}{$specbundle_key}{specthrough} = $specthrough;
  160. $data{$category}{$name_key}{specbundle}{$specbundle_key}{spec}{$weekdays_key}{weekdays} = $weekdays;
  161. # TODO: extend SPARQL to cover specs without opens/closes, or drop below check
  162. if ($opens and $closes) {
  163. my $hours_key = $opens;
  164. $hours_key =~ s/\b(\d)\b/0$1/g;
  165. my $hourrange = "$opens - $closes";
  166. $data{$category}{$name_key}{specbundle}{$specbundle_key}{spec}{$weekdays_key}{hours}{$hours_key} = $hourrange;
  167. $data{$category}{$name_key}{specbundle}{$specbundle_key}{spec}{$weekdays_key}{speccomment} = $speccomment;
  168. }
  169. }
  170. my $title = 'Åbningstider';
  171. my $intro = sprintf(
  172. 'Åbningstider på Orø indenfor udvalgte kategorier, dækkende perioden %s.',
  173. daterangedescribe($globalfrom, $globalthrough),
  174. );
  175. # TODO: make simplified html (preferred for Scribus) optional
  176. #push my @content, h1($title), "\n", p($intro);
  177. push my @content, h1($title), "\n", $intro;
  178. # TODO: optionally merge items appearing in multiple categories
  179. for my $category ( @category ) {
  180. push @content, "\n", h2( categorydescribe($category) ), "\n";
  181. for my $name ( sort keys %{ $data{$category} } ) {
  182. push @content, "\n", h3($name);
  183. my @bundle = keys %{ $data{$category}{$name}{specbundle} };
  184. # FIXME: respect locale when sorting (Galleri before Gaardstronomi)
  185. for my $specbundle ( sort {
  186. $data{$category}{$name}{specbundle}{$a}{specfrom}
  187. <=> $data{$category}{$name}{specbundle}{$b}{specfrom}
  188. || $a cmp $b
  189. } @bundle ) {
  190. # TODO: when global limits shown, show only speclabel plural spec bundles
  191. my $speclabel = speclabeldescribe(
  192. $data{$category}{$name}{specbundle}{$specbundle}{speclabel},
  193. $data{$category}{$name}{specbundle}{$specbundle}{locationcomment},
  194. $data{$category}{$name}{specbundle}{$specbundle}{specfrom},
  195. $data{$category}{$name}{specbundle}{$specbundle}{specthrough},
  196. scalar @bundle,
  197. );
  198. push @content, "\n", h4($speclabel) if ($speclabel);
  199. my @specbundle;
  200. for my $weekdays ( sort keys %{ $data{$category}{$name}{specbundle}{$specbundle}{spec} } ) {
  201. my $specdescription = specdescribe(
  202. $data{$category}{$name}{specbundle}{$specbundle}{spec}{$weekdays}{weekdays},
  203. $data{$category}{$name}{specbundle}{$specbundle}{spec}{$weekdays}{speccomment},
  204. );
  205. push @specbundle, br if (@specbundle);
  206. my @hours;
  207. for my $hours ( sort keys %{ $data{$category}{$name}{specbundle}{$specbundle}{spec}{$weekdays}{hours} } ) {
  208. # TODO: make simplified html (preferred for Scribus) optional
  209. # push @hours, br, "\n" if (@hours);
  210. push @hours, br if (@hours);
  211. push @hours, "\t", span(
  212. $data{$category}{$name}{specbundle}{$specbundle}{spec}{$weekdays}{hours}{$hours}
  213. );
  214. }
  215. push @specbundle, "\n", span(
  216. span($specdescription, ":"), @hours,
  217. );
  218. }
  219. # TODO: make simplified html (preferred for Scribus) optional
  220. # push @content, p("\n", @specbundle) if (@specbundle);
  221. push @content, @specbundle if (@specbundle);
  222. }
  223. }
  224. }
  225. #Dumper %data;
  226. #die;
  227. my $writer = HTML::HTML5::Writer->new;
  228. say $writer->document( html(
  229. -lang => 'da',
  230. head(
  231. title($title),
  232. Meta(-charset => 'utf-8'),
  233. ),
  234. body(
  235. "\n", @content
  236. ),
  237. ));
  238. # create query object from curied SPARQL string
  239. sub qurie {
  240. my $sparql = shift;
  241. $sparql =~ s/(?<=\s|\^)([a-z]+):([a-zA-Z]+)(?=\s)/curie("$1_$2")/eg;
  242. my $query = RDF::Query->new( $sparql );
  243. if (!$query) {
  244. say STDERR $sparql;
  245. say STDERR RDF::Query->error;
  246. croak "failed to prepare SPARQL query";
  247. }
  248. return $query;
  249. }
  250. sub daterangedescribe {
  251. my ($from, $through) = @_;
  252. return sprintf( '%s - %s',
  253. $from->format_cldr('d/M'),
  254. $through->format_cldr('d/M'),
  255. );
  256. }
  257. sub categorydescribe {
  258. my $cat = shift;
  259. $cat =~ s!full-meal food!spisesteder!;
  260. $cat =~ s!non-meal food!caféer, slikbutikker o.l.!;
  261. $cat =~ s!<http://schema.org/GroceryStore>!dagligvarebutikker!;
  262. $cat =~ s!non-grocery store!øvrige butikker!;
  263. $cat =~ s!attraction!attraktioner og museer!;
  264. $cat =~ s!public service!offentlige services!;
  265. return ucfirst($cat);
  266. }
  267. sub titledescribe {
  268. my ($title, $comment) = @_;
  269. return ($comment) ? "$title ($comment)" : $title;
  270. }
  271. sub speclabeldescribe {
  272. my ($label, $comment, $from, $through, $size) = @_;
  273. # TODO: support optionally enabling descriptive label
  274. my $compact = 1;
  275. return '' if ( ($compact) and ( $size == 1 ) );
  276. my $daterange = daterangedescribe($from, $through);
  277. if (($from) and ($through)) {
  278. return titledescribe( $daterange, $comment )
  279. if ($compact);
  280. return $label, ' (', $daterange, ' - ', $comment, ')'
  281. if ($comment);
  282. return $label, ' (', $daterange, ')';
  283. }
  284. warn "gr:validFrom and gr:validThrough missing for label \"$label\"";
  285. return ($compact) ? '' : titledescribe( $label, $comment );
  286. }
  287. sub specdescribe {
  288. my ($weekdays, $comment) = @_;
  289. # TODO: translate properly
  290. $weekdays =~ s/1, 2, 3, 4, 5, 6, 7/alle ugedage/;
  291. $weekdays =~ s/1, 2, 3, 4, 5/mandag-fredag/;
  292. $weekdays =~ s/1, 2, 3, 4/mandag-torsdag/;
  293. $weekdays =~ s/2, 3, 4, 5, 6, 7/tirsdag-søndag/;
  294. $weekdays =~ s/3, 4, 5/onsdag-fredag/;
  295. $weekdays =~ s/^2, 3, 4(|, [^5].*)$/tirsdag-torsdag$1/;
  296. $weekdays =~ s/^(|.*?[\d^5], |.*?(?!fredag, ))6, 7(.*)$/$1weekend$2/;
  297. $weekdays =~ s/1/mandag/;
  298. $weekdays =~ s/2/tirsdag/;
  299. $weekdays =~ s/3/onsdag/;
  300. $weekdays =~ s/4/torsdag/;
  301. $weekdays =~ s/5/fredag/;
  302. $weekdays =~ s/6/lørdag/;
  303. $weekdays =~ s/7/søndag/;
  304. $weekdays =~ s/8/helligdage/;
  305. if ( $weekdays eq "0" and ($comment) ) {
  306. return $comment;
  307. }
  308. return titledescribe( $weekdays, $comment );
  309. }
  310. 1;