-
Notifications
You must be signed in to change notification settings - Fork 4
/
knownLocations.pl
executable file
·124 lines (107 loc) · 3.88 KB
/
knownLocations.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/perl -w
# by Torben Menke https://entorb.net
# DESCRIPTION
# edit known locations stored as .txt
# sort and cleanup on write
# format "$lat $lon $loc\n", sorted by $loc name
# TODO
# IDEAS
# DONE
# Modules: My Default Set
use strict;
use warnings;
use 5.010; # say
use Data::Dumper;
use utf8; # this script is written in UTF-8
binmode STDOUT, ':utf8'; # default encoding for linux print STDOUT
# Modules: Perl Standard
use Encode qw(encode decode);
# use File::Path qw/remove_tree/;
# Modules: Web
use CGI;
my $cgi = CGI->new;
#use CGI ":all";
#use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
# Modules: My Strava Module Lib
use lib ('.');
use lib ('/var/www/virtual/entorb/perl5/lib/perl5');
use lib "C:\\Users\\menketrb\\Documents\\Hacken\\Perl\\Strava-Web"
; # just for making Visual Studio Code happy
use lib "d:\\files\\Hacken\\Perl\\Strava-Web";
use TMsStrava qw( %o %s)
; # at entorb.net some modules require use local::lib!!!
TMsStrava::htmlPrintHeader( $cgi, 'Known locations' );
TMsStrava::initSessionVariables( $cgi->param("session") );
TMsStrava::htmlPrintNavigation();
say
"<p>Privacy warning: This data is stored on the server until you replace it by an empty list.</p>";
my @knownLocations;
# submit: write new contents
# if contents is empty -> delete file
if ( $cgi->param('submit') ) {
my $s = $cgi->param('knownLocations');
if ( $s =~ m/^\s*$/s ) { # knownLocations is empty -> delete file
if ( -f $s{'fileKnownLocations'} ) {
unlink $s{'fileKnownLocations'}; #
TMsStrava::logIt("deleted known locations file");
say "<p>Deleted your known locations</p>";
}
} ## end if ( $s =~ m/^\s*$/s )
else { # write to file
my $s = decode( 'UTF-8', $cgi->param('knownLocations') );
my @formdata = split /(\r?\n|\r)/, $s;
my %h; # temp storage for sorting
foreach my $line (@formdata) {
# cleanup unwanted chars like ","
next
unless $line
=~ m/^[^\d\-]*([\d\-\.]+)[^\d\-]*([\d\-\.]+) (.*)\r?\n?$/;
my ( $lat, $lon, $loc ) = ( $1, $2, $3 );
$loc =~ s/(^\s*|\s*$)//g; # trim spaces from ends
$loc =~ s/\s+//; # whitespaces -> ""
$h{$loc} = [ $lat, $lon ];
} ## end foreach my $line (@formdata)
my @L = sort keys(%h);
foreach my $k (@L) {
push @knownLocations,
join( " ", @{ $h{$k} } )
. " $k"; # $lat $lon $loc, sorted by $loc name
}
open( my $fhOut, '>:encoding(UTF-8)', $s{'fileKnownLocations'} )
or die("ERROR: Can't write to file");
print $fhOut join( "\n", @knownLocations );
close $fhOut;
TMsStrava::logIt("wrote known locations to file");
} ## end else [ if ( $s =~ m/^\s*$/s )]
# remove excel export after modification of know places
foreach my $file (<$s{'tmpDownloadFolder'}/*.xlsx>) {
unlink $file;
TMsStrava::logIt(
"deleted Excel download files after modification of known locations");
}
} # if submit
# the list is displayed wether or not the form is submittet and wether or not a user's location file is present
# Read known locations from file $stravaUserID.txt
# format of file: "$lat $lon "descripting (without spaces)\n"
@knownLocations
= TMsStrava::readKnownLocationsFromFile( $s{'fileKnownLocations'} );
foreach my $item (@knownLocations) {
my @L = @{$item}; # each line is an $arrayref
$item = join " ", @L;
}
say '
<form action="knownLocations.pl?session=' . $s{'session'} . '" method="post">
<table>
<tr><th>Locations<br><small>example: 51.070298 13.760067 DD-Alaunpark</small></th></tr>
<tr><td>
<textarea name="knownLocations" cols="60" rows="20">
' . join( "\n", @knownLocations ) . '
</textarea>
</td></tr>
</table>
<input type="hidden" name="session" value="' . $s{'session'} . '">
<input type="submit" name="submit" value="Submit">
</form>
';
TMsStrava::htmlPrintFooter($cgi);