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