summaryrefslogtreecommitdiff
path: root/bin/rdf2hours
blob: b99b14ce02190568622f87a93c2fabd3ffeec80e (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. for my $category ( @category ) {
  179. push @content, "\n", h2( categorydescribe($category) ), "\n";
  180. for my $name ( sort keys %{ $data{$category} } ) {
  181. push @content, "\n", h3($name);
  182. my @bundle = keys %{ $data{$category}{$name}{specbundle} };
  183. # FIXME: respect locale when sorting (Galleri before Gaardstronomi)
  184. for my $specbundle ( sort {
  185. $data{$category}{$name}{specbundle}{$a}{specfrom}
  186. <=> $data{$category}{$name}{specbundle}{$b}{specfrom}
  187. || $a cmp $b
  188. } @bundle ) {
  189. # TODO: when global limits shown, show only speclabel plural spec bundles
  190. my $speclabel = speclabeldescribe(
  191. $data{$category}{$name}{specbundle}{$specbundle}{speclabel},
  192. $data{$category}{$name}{specbundle}{$specbundle}{locationcomment},
  193. $data{$category}{$name}{specbundle}{$specbundle}{specfrom},
  194. $data{$category}{$name}{specbundle}{$specbundle}{specthrough},
  195. scalar @bundle,
  196. );
  197. push @content, "\n", h4($speclabel) if ($speclabel);
  198. my @specbundle;
  199. for my $weekdays ( sort keys %{ $data{$category}{$name}{specbundle}{$specbundle}{spec} } ) {
  200. my $specdescription = specdescribe(
  201. $data{$category}{$name}{specbundle}{$specbundle}{spec}{$weekdays}{weekdays},
  202. $data{$category}{$name}{specbundle}{$specbundle}{spec}{$weekdays}{speccomment},
  203. );
  204. push @specbundle, br if (@specbundle);
  205. my @hours;
  206. for my $hours ( sort keys %{ $data{$category}{$name}{specbundle}{$specbundle}{spec}{$weekdays}{hours} } ) {
  207. # TODO: make simplified html (preferred for Scribus) optional
  208. # push @hours, br, "\n" if (@hours);
  209. push @hours, br if (@hours);
  210. push @hours, "\t", span(
  211. $data{$category}{$name}{specbundle}{$specbundle}{spec}{$weekdays}{hours}{$hours}
  212. );
  213. }
  214. push @specbundle, "\n", span(
  215. span($specdescription, ":"), @hours,
  216. );
  217. }
  218. # TODO: make simplified html (preferred for Scribus) optional
  219. # push @content, p("\n", @specbundle) if (@specbundle);
  220. push @content, @specbundle if (@specbundle);
  221. }
  222. }
  223. }
  224. #Dumper %data;
  225. #die;
  226. my $writer = HTML::HTML5::Writer->new;
  227. say $writer->document( html(
  228. -lang => 'da',
  229. head(
  230. title($title),
  231. Meta(-charset => 'utf-8'),
  232. ),
  233. body(
  234. "\n", @content
  235. ),
  236. ));
  237. # create query object from curied SPARQL string
  238. sub qurie {
  239. my $sparql = shift;
  240. $sparql =~ s/(?<=\s|\^)([a-z]+):([a-zA-Z]+)(?=\s)/curie("$1_$2")/eg;
  241. my $query = RDF::Query->new( $sparql );
  242. if (!$query) {
  243. say STDERR $sparql;
  244. say STDERR RDF::Query->error;
  245. croak "failed to prepare SPARQL query";
  246. }
  247. return $query;
  248. }
  249. sub daterangedescribe {
  250. my ($from, $through) = @_;
  251. return sprintf( '%s - %s',
  252. $from->format_cldr('d/M'),
  253. $through->format_cldr('d/M'),
  254. );
  255. }
  256. sub categorydescribe {
  257. my $cat = shift;
  258. $cat =~ s!full-meal food!spisesteder!;
  259. $cat =~ s!non-meal food!caféer, slikbutikker o.l.!;
  260. $cat =~ s!<http://schema.org/GroceryStore>!dagligvarebutikker!;
  261. $cat =~ s!non-grocery store!øvrige butikker!;
  262. $cat =~ s!attraction!attraktioner og museer!;
  263. $cat =~ s!public service!offentlige services!;
  264. return ucfirst($cat);
  265. }
  266. sub titledescribe {
  267. my ($title, $comment) = @_;
  268. return ($comment) ? "$title ($comment)" : $title;
  269. }
  270. sub speclabeldescribe {
  271. my ($label, $comment, $from, $through, $size) = @_;
  272. # TODO: support optionally enabling descriptive label
  273. my $compact = 1;
  274. return '' if ( ($compact) and ( $size == 1 ) );
  275. my $daterange = daterangedescribe($from, $through);
  276. if (($from) and ($through)) {
  277. return titledescribe( $daterange, $comment )
  278. if ($compact);
  279. return $label, ' (', $daterange, ' - ', $comment, ')'
  280. if ($comment);
  281. return $label, ' (', $daterange, ')';
  282. }
  283. warn "gr:validFrom and gr:validThrough missing for label \"$label\"";
  284. return ($compact) ? '' : titledescribe( $label, $comment );
  285. }
  286. sub specdescribe {
  287. my ($weekdays, $comment) = @_;
  288. # TODO: translate properly
  289. $weekdays =~ s/1, 2, 3, 4, 5, 6, 7/alle ugedage/;
  290. $weekdays =~ s/1, 2, 3, 4, 5/mandag-fredag/;
  291. $weekdays =~ s/1, 2, 3, 4/mandag-torsdag/;
  292. $weekdays =~ s/2, 3, 4, 5, 6, 7/tirsdag-søndag/;
  293. $weekdays =~ s/3, 4, 5/onsdag-fredag/;
  294. $weekdays =~ s/^2, 3, 4(|, [^5].*)$/tirsdag-torsdag$1/;
  295. $weekdays =~ s/^(|.*?[\d^5], |.*?(?!fredag, ))6, 7(.*)$/$1weekend$2/;
  296. $weekdays =~ s/1/mandag/;
  297. $weekdays =~ s/2/tirsdag/;
  298. $weekdays =~ s/3/onsdag/;
  299. $weekdays =~ s/4/torsdag/;
  300. $weekdays =~ s/5/fredag/;
  301. $weekdays =~ s/6/lørdag/;
  302. $weekdays =~ s/7/søndag/;
  303. $weekdays =~ s/8/helligdage/;
  304. if ( $weekdays eq "0" and ($comment) ) {
  305. return $comment;
  306. }
  307. return titledescribe( $weekdays, $comment );
  308. }
  309. 1;