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