<commit_before>package Radio;

use strict;
use warnings;

use Dancer ':syntax';
use Dancer::Plugin::MPD;

use IO::File;
use POSIX qw(ceil);
use Time::Piece;
use Radio::Station;

our $VERSION = '0.1';
our $station = Radio::Station->promote(mpd); 

sub flash { 
  my $type = @_ > 1 ? shift : '';
  my $message = shift;
  
  session->{flash}{$message} = $type;
}

sub require_login {
  return 1 if session('user');
  
  flash warning => 'You must log in to view this page.';
  session(requested_page => request->path_info);
  
  redirect '/login';

  return 0;
}

sub authenticate {
  my ($name, $pass) = @_;
  
  return $pass eq setting('<PASSWORD>');
}

sub ago {
  my $timestamp = shift;

  return 'Never' unless $timestamp;

  my $now = time;
  my $minutes = int(($now - $timestamp) / 60);

  my $count = 0;
  my $unit = 'minute';

  if ($minutes < 1) {
    return 'Just now';
  } elsif ($minutes < 55) {
    $count = ceil($minutes / 5) * 5;
  } elsif ($minutes < 23 * 60) {
    $count = ceil($minutes / 60);
    $unit = 'hour';
  } elsif ($minutes < 24 * 60 * 6) {
    $count = ceil($minutes / 60 / 24);
    $unit = 'day';
  } elsif ($minutes < 24 * 60 * 7 * 4) {
    $count = ceil($minutes / 60 / 24 / 7);
    $unit = 'week';
  } elsif ($minutes < 24 * 60 * 30 * 18) {
    $count = ceil($minutes / 60 / 24 / 30);
    $unit = 'month';
  } else {
    $count = ceil($minutes / 60 / 24 / 365);
    $unit = 'year';
  }

  return sprintf('%u %s%s ago', $count, $unit, $count == 1 ? '' : 's');
}

before_template sub {
  my $tokens = shift;
  
  $tokens->{current} = $station->current; 
  $tokens->{ago} = \&ago;
  $tokens->{stream_uri} = setting('stream_uri');
};

get '/' => sub { 
  template 'news', { posts => [] };
};

get '/index.html' => sub {
  template 'news', { posts => [] };
};

post '/' => sub {
  require_login or return;

  my $id = localtime->strftime('%Y%m%d.%H%M%S');

  my $path = setting('path_news') . "/$id.txt";
  my $file = IO::File->new($path, 'w');

  if ($file) {
    $file->print(params->{post});
    $file->close();
    flash 'News posted';
  } else {
    flash error => "Unable to open news file $path: $!";
  }
<commit_msg>Fix news listing<commit_after>package Radio;

use strict;
use warnings;

use Dancer ':syntax';
use Dancer::Plugin::MPD;

use IO::File;
use POSIX qw(ceil);
use Time::Piece;
use Radio::Station;

our $VERSION = '0.1';
our $station = Radio::Station->promote(mpd); 

sub flash { 
  my $type = @_ > 1 ? shift : '';
  my $message = shift;
  
  session->{flash}{$message} = $type;
}

sub require_login {
  return 1 if session('user');
  
  flash warning => 'You must log in to view this page.';
  session(requested_page => request->path_info);
  
  redirect '/login';

  return 0;
}

sub authenticate {
  my ($name, $pass) = @_;
  
  return $pass eq setting('admin_password');
}

sub ago {
  my $timestamp = shift;

  return 'Never' unless $timestamp;

  my $now = time;
  my $minutes = int(($now - $timestamp) / 60);

  my $count = 0;
  my $unit = 'minute';

  if ($minutes < 1) {
    return 'Just now';
  } elsif ($minutes < 55) {
    $count = ceil($minutes / 5) * 5;
  } elsif ($minutes < 23 * 60) {
    $count = ceil($minutes / 60);
    $unit = 'hour';
  } elsif ($minutes < 24 * 60 * 6) {
    $count = ceil($minutes / 60 / 24);
    $unit = 'day';
  } elsif ($minutes < 24 * 60 * 7 * 4) {
    $count = ceil($minutes / 60 / 24 / 7);
    $unit = 'week';
  } elsif ($minutes < 24 * 60 * 30 * 18) {
    $count = ceil($minutes / 60 / 24 / 30);
    $unit = 'month';
  } else {
    $count = ceil($minutes / 60 / 24 / 365);
    $unit = 'year';
  }

  return sprintf('%u %s%s ago', $count, $unit, $count == 1 ? '' : 's');
}

before_template sub {
  my $tokens = shift;
  
  $tokens->{current} = $station->current; 
  $tokens->{ago} = \&ago;
  $tokens->{stream_uri} = setting('stream_uri');
};

get '/' => sub { 
  my @news = ();

  my $path = setting('news_path');
  for (<$path/*.txt>) {
    if (my $file = IO::File->new($_, 'r')) {
      push @news, {
        path => $_,
        posted => ($file->stat)[9],
        body => join('', $file->getlines),
      };
    } else {
      warning "Couldn't open $_ for reading: $!";
    }
  }

  @news = sort {$b->{posted} <=> $a->{posted}} @news;
  my $count = setting('news_max') || 10;
  @news = grep {$_} @news[0 .. ($count - 1)];

  template 'news', { posts => \@news };
};

post '/' => sub {
  require_login or return;

  my $id = localtime->strftime('%Y%m%d.%H%M%S');

  my $path = setting('news_path') . "/$id.txt";
  my $file = IO::File->new($path, 'w');

  if ($file) {
    $file->print(params->{post});
    $file->close();
    flash 'News posted';
  } else {
    flash error => "Unable to open news file $path: $!";
  }

{'fraction_non_alphanumeric': 0.20976645435244162, 'fraction_numerical': 0.026963906581740978, 'mean_word_length': 2.753784860557769, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3248899', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1755, 'n_words': 479}