-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathactivityModify.pl
More file actions
executable file
·220 lines (191 loc) · 6.82 KB
/
activityModify.pl
File metadata and controls
executable file
·220 lines (191 loc) · 6.82 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/perl -w
# by Torben Menke https://entorb.net
# DESCRIPTION
# TODO
# how to set description to ""?
# IDEAS
# DONE
# after successful modification, if present all jsons are deleted
# 181026: somce changes due to stravas api changes, trainer added
# 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
# 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, 'Bulk modify activities' );
TMsStrava::initSessionVariables( $cgi->param("session") );
TMsStrava::htmlPrintNavigation();
my ( @activityIDs, $commute, $trainer, $name, $description, $gear_id );
if ( $cgi->param('submitFromActivityList') ) { # from list_all_activities.pl
# say Dumper $cgi->param('activityID');
@activityIDs = ( $cgi->param('activityID') );
@activityIDs
= grep {/[\d+]/g} @activityIDs; # for security only number-only ids
} ## end if ( $cgi->param('submitFromActivityList'...))
elsif ( $cgi->param('submitFromModify') ) { # form below
# say Dumper $cgi->param; # debug
# cleaning for security
$_ = $cgi->param('activityIDs');
s/[^0-9]+/ /g; # for security ensure only numbers and spaces
s/(^ +| +$)//g; # spaces at the ends
@activityIDs = split / /, $_;
@activityIDs = grep { $_ > 100000 }
@activityIDs; # for only numbers > 100000 are making sense
# ensure @activityIDs has unique ids
my %h;
foreach my $key (@activityIDs) {
$h{$key}++;
}
@activityIDs = sort keys(%h);
undef %h;
die "ERROR: activity ID list empty" unless (@activityIDs);
$commute = $cgi->param('commute');
$trainer = $cgi->param('trainer'); # private was removed from stravas API
$name = $cgi->param('name');
$description = $cgi->param('description');
$gear_id = $cgi->param('gear_id');
TMsStrava::logIt(
"commute='$commute' trainer='$trainer' name='$name' description='$description' gear_id='$gear_id'"
);
%h = ();
if ( $commute eq "0" ) {
$h{'commute'} = 'false';
}
elsif ( $commute eq "1" ) {
$h{'commute'} = 'true';
}
if ( $trainer eq "0" ) {
$h{'trainer'} = 'false';
}
elsif ( $trainer eq "1" ) {
$h{'trainer'} = 'true';
}
$name =~ s/[^\w:_!\?\-\+\(\)\[\]\{\}]+//g
; # char whitelist TODO: Umlaute missing
if ( $name ne "" ) { $h{'name'} = '"' . $name . '"'; }
$description =~ s/[^\w:_!\?\-\+\(\)\[\]\{\}]+//g; # char whitelist
if ( $description ne "" ) { $h{'description'} = '"' . $description . '"'; }
$gear_id =~ s/[^\w]+//g; # char whitelist
if ( $gear_id ne "" ) { $h{'gear_id'} = '"' . $gear_id . '"'; }
die "ERROR: nothing to do" if ( not %h );
# generate json string
# goal: $json = '{ "commute": true, "private": false }';
my $json = "{"; #
foreach my $s (qw(commute trainer name description gear_id))
{ # private was removed by strava
$json .= "\"$s\":$h{$s}, " if $h{$s};
}
$json = substr( $json, 0, length($json) - 2 )
if ( length($json) > 2 ); # remove last ", "
$json .= "}";
# print resulting table, bad IDs result in empty row
say '<table>
<tr>
<th>ID</th>
<th>date</th>
<th>name</th>
<th>commute</th>
<th>training machine</th>
<th>gear</th>
</tr>';
foreach my $activityID (@activityIDs) {
my ( $htmlcode, $cont )
= TMsStrava::PostPutJsonToURL( 'PUT',
"https://www.strava.com/api/v3/activities/$activityID",
$s{'token'}, 1, $json ); # last parameter: 1=silent , 0=die on error
# print Dumper $cont;
my %h = TMsStrava::convertJSONcont2Hash($cont);
# print Dumper %h;
if ( not $h{'id'} ) {
say
"<tr><td colspan=6><font color=\"red\">$activityID</font></td></tr>";
}
else {
say '<tr>' . '<td>'
. $h{'id'} . '</td>' . '<td>'
. $h{'start_date_local'} . '</td>' . '<td>'
. $h{'name'} . '</td>'
. '<td align="center">'
. ( $h{'commute'} += 0 ) . '</td>'
. '<td align="center">'
. ( $h{'trainer'} += 0 ) . '</td>' . '<td>'
. $h{'gear'}{'name'} . '</td>' . '</tr>';
} ## end else [ if ( not $h{'id'} ) ]
} ## end foreach my $activityID (@activityIDs)
say '</table><hr>';
# after modification we need to cleanup the downloaded activity list jsons and stored dump files, since they are not up to date any more
TMsStrava::clearCache();
} ## end elsif ( $cgi->param('submitFromModify'...))
# display form always
say '
<form action="activityModify.pl?session=' . $s{'session'} . '" method="post">
<table>
<tr><th>Activity IDs</th><th>Settings</th></tr>
<tr><td>
<textarea name="activityIDs" cols="10" rows="20">'
. join( "\n", @activityIDs ) . '</textarea>
</td><td>
<table>
<tr align="center"><td> </td><td>1</td><td>0</td></tr>
<tr align="center"><td>commute</td>
<td><input type="radio" name="commute" value="1"></td>
<td><input type="radio" name="commute" value="0"></td>
</tr>
<tr align="center"><td>training machine</td>
<td><input type="radio" name="trainer" value="1"></td>
<td><input type="radio" name="trainer" value="0"></td>
</tr>
<tr align="center"><td>name</td>
<td colspan="2"><input type="text" name="name" value=""></td>
</tr>
<tr align="center"><td>description</td>
<td colspan="2"><input type="text" name="description" value=""></td>
</tr>
<tr align="center"><td>gear ID</td>
<td colspan="2"><input type="text" name="gear_id" value=""></td>
</tr>
</table>
<input type="hidden" name="session" value="' . $s{'session'} . '">
<input type="submit" name="submitFromModify" value="Submit">
</td></tr>
</table>
</form>
';
# TODO: this is just a copy from activityExcelImport
use Storable; # read and write variables to filesystem
my %gear;
if ( -f $s{'pathToGearHashDump'} ) {
%gear = %{ retrieve( $s{'pathToGearHashDump'} ) }
; # retrieve data from file (as ref)
}
if (%gear) { # gear hash is not empty
say '<h3>List of gear used in cached activities</h3>
<table>
<tr><th>Gear ID</th><th>Gear Name</th></tr>';
foreach my $gear_id ( sort keys %gear ) {
say "<tr><td>$gear_id</td><td>$gear{$gear_id}</td></tr>";
}
say '</table>';
} ## end if (%gear)
else {
say
"<p>Cache your activities first to display a list of your gear_id (shoes/bikes) here.</p>";
}
TMsStrava::htmlPrintFooter($cgi);