Skip to content

Commit

Permalink
feat: support csv file download from google sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
kalong committed Sep 24, 2023
1 parent 1f70917 commit 1f0be33
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 26 deletions.
Binary file modified .DS_Store
Binary file not shown.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ it will not work when run ./base64-convert-csv

it will also not work when run in visual studio code

the input.csv is the sample csv file
the raw.csv is the sample csv file

the program will read input.csv, so make sure the is name as "input.csv"
the program will read raw.csv, so make sure the is name as "raw.csv"

each row is one base64 data, the csv file do not have header
please download the file from the google sheet (sheet3) as csv, then rename to "raw.csv"

test the github app of commit and release
since each cell will have maximum length, cannot store all of the base64 string, therefore, the base64 string will split into several row.

the rust will combine the each base64 into one row, then convert it to wav

the csv file do not have header, and only have one column
35 changes: 33 additions & 2 deletions input.csv

Large diffs are not rendered by default.

62 changes: 42 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,65 @@
use std::env;
use std::fs::File;
use std::io::Write;
use std::io::{BufRead, BufReader, Write};
use std::path::PathBuf;
use base64::decode;
use rand::Rng;
use chrono::{Local, DateTime, Utc};
use csv::ReaderBuilder;

fn main() {
// Get the current working directory
let executable_path = env::args()
.next()
.map(PathBuf::from)
.expect("Failed to get executable path.");
//println!("executable_path");
//println!("{:?}", executable_path);

let executable_dir = executable_path
.parent()
.expect("Failed to get executable directory.");
//println!("executable_dir");
//println!("{:?}", executable_dir);

// raw csv
let raw_file_name = "raw.csv";
let raw_file_path = executable_dir.join(raw_file_name);
let raw_file = File::open(raw_file_path).expect("Failed to open CSV file.");
let raw_reader = BufReader::new(raw_file);
let mut combined_rows = Vec::new();
let mut current_row = String::new();

for line in raw_reader.lines() {
let line = line.expect("Failed to read line from raw CSV file.");
if line.starts_with("\"{") {
// Start of a new person's data
if !current_row.is_empty() {
combined_rows.push(current_row.clone());
current_row.clear();
}
current_row.push_str(&line[1..]);
} else {
current_row.push_str(&line);
}
}
combined_rows.push(current_row);

// Specify the CSV file name
let file_name = "input.csv"; // Replace with the name of your CSV file
//println!("{:?}", file_name);
//let file_name = "input.csv"; // Replace with the name of your CSV file

// Construct the file path
let file_path = executable_dir.join(file_name);
//println!("{:?}", file_path);
let file = File::open(file_path).expect("Failed to open CSV file.");
let mut csv_reader = ReaderBuilder::new()
.delimiter(b'\t') // Replace with the delimiter used in your CSV file
.has_headers(false)
.from_reader(file);
//let file_path = executable_dir.join(file_name);

//let file = File::open(file_path).expect("Failed to open CSV file.");
//let mut csv_reader = ReaderBuilder::new()
// .delimiter(b'\t') // Replace with the delimiter used in your CSV file
// .has_headers(false)
// .from_reader(file);

// Iterate over each row in the CSV file
for result in csv_reader.records() {
//for result in csv_reader.records() {
for result in &combined_rows {
// Read the Base64 string from the CSV row
let record = result.expect("Failed to read CSV record.");
let base64_string = record.get(0).unwrap_or_else(|| {
panic!("Invalid Base64 string in CSV file.");
});
//let record = result.expect("Failed to read CSV record.");
let base64_string_replace = result.replace("\"", "");
let base64_string = &base64_string_replace[9..&base64_string_replace.len()-1];
//println!("{:?}", base64_string);

// Skip empty cells
if base64_string.trim().is_empty() {
Expand All @@ -53,6 +74,7 @@ fn main() {
continue;
}
};
println!("{:?}", base64_data);

// Decode the Base64 string
let decoded_data = match decode(base64_data) {
Expand Down

0 comments on commit 1f0be33

Please sign in to comment.