Tweet
- Here is the whole
build
script, that's building my website
file: build.pl
1: #!/usr/bin/perl -w
2: use 5.010;
3: use strict;
4: use warnings;
5: use utf8;
6:
7: use Blio;
8: use Blio::Node;
9: use DBI;
10: use FindBin;
11: use Path::Class;
12: use DateTime::Format::Strptime;
13: use Encode;
14: use Class::Load qw(try_load_class);
15: use Sys::Hostname qw(hostname);
16:
17: my $blio = Blio->new_with_options;
18:
19: $blio->collect;
20: handle_vinyl();
21: handle_microblog();
22: prepare_index();
23:
24: $blio->stash->{menu_order} = [ map {$_.'.html' } qw(
25: microblog perl bicycle reisen talks var mixtapes potd vinyl tags
26: )];
27:
28: $blio->write;
29:
30:
31: if (try_load_class('Net::Twitter::Lite::WithAPIv1_1') && hostname() eq 'bnr') {
32: potd_tweet();
33: }
34:
35: sub potd_tweet {
36: my %tweeted;
37: my $index_file = file($blio->output_dir->file('tweeted.index'));
38: if (-e $index_file) {
39: %tweeted = map { chomp; $_ =>1 } $index_file->slurp;
40: }
41: my @to_tweet;
42:
43: while (my ($url, $node) = each %{$blio->nodes_by_url}) {
44: next unless $url =~ /^potd\//;
45: next if $tweeted{$url};
46: if (my $tweet = $node->stash->{tweet}) {
47: push(@to_tweet, $node);
48: }
49: }
50:
51: if (@to_tweet == 1) {
52: my $node = $to_tweet[0];
53: my $status = $node->stash->{tweet};
54: my $length_check = $status;
55: $length_check=~s{https?://.* }{'x' x 24}ge; # twitter short urls are 23 characters + 1 trailing space
56: if (length($length_check) > 89) {
57: $status=substr($status, 0,87).'â¦';
58: }
59:
60: my $message = {
61: status=>'#potd '.encode_utf8($status).' '.$blio->site_url.'/'.$node->url,
62: media=>[ $blio->output_dir->file($node->primary_image->url)->stringify ]
63: };
64:
65: my $conf = do $blio->output_dir->parent->file('twitter.conf')->stringify;
66: my $nt = Net::Twitter::Lite::WithAPIv1_1->new(
67: clientname => 'Blio',
68: clientver => 1,
69: clienturl => 'http://domm.plix.at',
70: useragent => "Blio/1",
71: legacy_lists_api => 0,
72: ssl => 1,
73: apiurl => 'https://api.twitter.com/1.1',
74: %$conf
75: );
76:
77: eval {
78: my $rv = $nt->update_with_media($message);
79: if ($rv->{id_str}) {
80: say "successfully tweeted: ".$message->{status};
81: }
82: };
83: if ($@) {
84: say "error while tweeting: ".$@;
85: }
86:
87: my $fh = $index_file->opena;
88: say $fh $node->url;
89: close $fh;
90: }
91: elsif (@to_tweet > 1) {
92: say "more than one tweet, stay silent to be sure";
93: say join(', ',map {$_->url} @to_tweet);
94: }
95:
96: }
97:
98: sub prepare_index {
99: my $index = $blio->nodes_by_url->{'index.html'};
100: my @featured;
101: my $by_date = $blio->nodes_by_date;
102: my (@vinyl, @microblog, @nodes, @images, $potd);
103: my $full=0;
104: foreach my $feature (@$by_date) {
105: last if @microblog >= 2 && @vinyl >= 6 && @nodes >= 5 && @images >=5;
106: next unless $feature->parent;
107: my $url = $feature->url;
108: if ($url =~m{^vinyl/}) {
109: next if @vinyl >= 6;
110: push(@vinyl,$feature);
111: }
112: elsif ($url =~m{^microblog/}) {
113: next if @microblog >= 2;
114: push(@microblog,$feature);
115: }
116: elsif ($url =~m{^potd}) {
117: $potd = $feature unless $potd;
118: }
119: elsif ($url =~m{^tags/}) {
120: next;
121: }
122: else {
123: if (@nodes < 5) {
124: push(@nodes,$feature);
125: }
126: if (@images < 5) {
127: if ($feature->has_images) {
128: push(@images,$feature);
129: }
130: }
131: }
132: }
133:
134: if ($potd) {
135: splice(@nodes,1,0,$potd);
136: }
137:
138: $index->stash->{images}=\@images;
139: $index->stash->{nodes}=\@nodes;
140: $index->stash->{vinyl}=\@vinyl;
141: $index->stash->{microblog}=\@microblog;
142: $index->stash->{potd}=$potd;
143:
144: my @top_atom;
145: my $all_sorted = $blio->nodes_by_date;
146: foreach (@$all_sorted) {
147: next if $_->id =~m{vinyl/};
148: next if $_->id =~m{tags/};
149: next if $_->id =~m{microblog/};
150: next if $_->id =~m{potd/};
151: last if @top_atom >=15;
152: push(@top_atom,$_);
153: }
154: $index->children(\@top_atom);
155: }
156:
157: sub handle_vinyl {
158: my $vinyl = $blio->nodes_by_url->{'vinyl.html'};
159:
160: my $vinyldb = $FindBin::Bin.'/sqlite/vinyl.db';
161: my $DBH=DBI->connect("dbi:SQLite:dbname=".$vinyldb,{sqlite_unicode => 1});
162: my $sth=$DBH->prepare("select * from vinyl order by buy DESC,id DESC");
163: $sth->execute;
164:
165: my @medium=('unknown','Album','12"','10"','7"','CD','MP3');
166:
167: while (my $r=$sth->fetchrow_hashref) {
168: my $title = $r->{artist}.' - '.$r->{name};
169: my $id = $title;
170: $id =~s/[^\w\d-]+/_/g;
171: my $url = 'vinyl/'.$id.'.html';
172: my $buy = $r->{buy};
173: $buy .= '0101' if length($buy) == 4;
174: $buy .= '01' if length($buy) == 6;
175:
176: my $date=DateTime->new(
177: year=>substr($buy,0,4),
178: month=>substr($buy,4,2),
179: day=>substr($buy,6,2),
180: time_zone=>'local',
181: );
182: my @content;
183: push(@content,$r->{description}) if $r->{description};
184: push(@content,$medium[$r->{medium}].', '.$r->{pub});
185: push(@content,$r->{shop});
186: my $content = join("\n", map { "<p>$_</p>" } @content);
187:
188: my $node = Blio::Node->new(
189: base_dir => $blio->source_dir,
190: source_file => file($vinyldb),
191: id=>'vinyl/'.$id,
192: url=>$url,
193: title=>decode_utf8($title),
194: date=>$date,
195: content=>decode_utf8($content),
196: );
197: $blio->nodes_by_url->{$url}=$node;
198: $vinyl->add_child($node);
199: $node->parent($vinyl);
200: }
201: }
202:
203: sub handle_microblog {
204: my $mb = $blio->nodes_by_url->{'microblog.html'};
205: $mb->template('microblog_index.tt');
206:
207: my $db = $FindBin::Bin.'/sqlite/microblog.db';
208: my $DBH=DBI->connect("dbi:SQLite:dbname=".$db);
209: my $sth=$DBH->prepare("select * from microblog order by date DESC,id DESC");
210: $sth->execute;
211:
212: my $strp = DateTime::Format::Strptime->new(
213: pattern => '%FT%T',
214: );
215:
216: while (my $r=$sth->fetchrow_hashref) {
217: my $date = $strp->parse_datetime($r->{date});
218: my $id = 'microblog/'.$date->iso8601;
219: my $url = $id.'.html';
220:
221: my $node = Blio::Node->new(
222: base_dir => $blio->source_dir,
223: source_file => file($db),
224: id=>$id,
225: url=>$url,
226: title=>$r->{message},
227: date=>$date,
228: content=>$r->{message},
229: template=>'microblog.tt',
230: );
231: $blio->nodes_by_url->{$url}=$node;
232: $mb->add_child($node);
233: $node->parent($mb);
234: }
235: }
236: