]> git.sesse.net Git - skvidarsync/commitdiff
Small refactoring.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 27 Oct 2023 22:02:25 +0000 (00:02 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 27 Oct 2023 22:02:25 +0000 (00:02 +0200)
bin/sync.pl

index 02f32952edca57db2b6d0a53e5eb6db12b70ed66..3e6920c1051ae2d3b06eb7927620f2c979764c0f 100644 (file)
@@ -137,6 +137,26 @@ sub sheet_batch_update {
        die $response->decoded_content if !$response->is_success;
 }
 
+sub get_spreadsheet_with_title {
+       my ($ua, $token, $wanted_sheet_title) = @_;
+
+       # See if we have any spreadsheets that match this title.
+       my $response = $ua->get('https://sheets.googleapis.com/v4/spreadsheets/' . $config::sheet_id . '?key=' . $config::gsheets_api_key . '&fields=sheets/properties',
+               Authorization => 'Bearer ' . $token
+       );
+       my $sheets_json = JSON::XS::decode_json($response->decoded_content);
+       my ($tab_name, $tab_id);
+       for my $sheet (@{$sheets_json->{'sheets'}}) {
+               my $title = $sheet->{'properties'}{'title'};
+               my $sheet_id = $sheet->{'properties'}{'sheetId'};
+               if ($title =~ /\Q$wanted_sheet_title\E/) {
+                       # skv_log("Synkroniserer ($config::invitation_channel, $invitation_ts) mot arket “$title” (fane-ID $sheet_id).");
+                       return ($title, $sheet_id);
+               }
+       }
+       return (undef, undef);
+}
+
 skv_log("Siste sync startet: " . POSIX::ctime(time));
 
 # Initialize the handles we need for communication.
@@ -154,22 +174,7 @@ my $invitation_ts = $linkref->{'ts'};
 my $wanted_sheet_title = $linkref->{'sheet_title'};
 die "Could not get newest sheet title" if (!defined($wanted_sheet_title));
 
-# See if we have any spreadsheets that match this title.
-my $response = $ua->get('https://sheets.googleapis.com/v4/spreadsheets/' . $config::sheet_id . '?key=' . $config::gsheets_api_key . '&fields=sheets/properties',
-       Authorization => 'Bearer ' . $token
-);
-my $sheets_json = JSON::XS::decode_json($response->decoded_content);
-my ($tab_name, $tab_id);
-for my $sheet (@{$sheets_json->{'sheets'}}) {
-       my $title = $sheet->{'properties'}{'title'};
-       my $sheet_id = $sheet->{'properties'}{'sheetId'};
-       if ($title =~ /\Q$wanted_sheet_title\E/) {
-               # skv_log("Synkroniserer ($config::invitation_channel, $invitation_ts) mot arket “$title” (fane-ID $sheet_id).");
-               $tab_name = $title;
-               $tab_id = $sheet_id;
-               last;
-       }
-}
+my ($tab_name, $tab_id) = get_spreadsheet_with_title($ua, $token, $wanted_sheet_title);
 if (!defined($tab_name)) {
        skv_log("Fant ikke noen fane med “$wanted_sheet_title” i navnet; kan ikke synkronisere.\n");
        sheet_batch_update($ua, $token, [ serialize_skv_log_to_sheet() ]);
@@ -185,7 +190,7 @@ while (my $ref = $q->fetchrow_hashref) {
 }
 
 # Get the list of all people in the sheet (we're going to need them soon anyway).
-$response = $ua->get('https://sheets.googleapis.com/v4/spreadsheets/' . $config::sheet_id . '?key=' . $config::gsheets_api_key . '&ranges=' . $tab_name . '!A9:Z5000&fields=sheets/data/rowData/values/userEnteredValue',
+my $response = $ua->get('https://sheets.googleapis.com/v4/spreadsheets/' . $config::sheet_id . '?key=' . $config::gsheets_api_key . '&ranges=' . $tab_name . '!A9:Z5000&fields=sheets/data/rowData/values/userEnteredValue',
        Authorization => 'Bearer ' . $token
 );
 my $main_sheet_json = JSON::XS::decode_json($response->decoded_content);