Created
September 24, 2024 01:27
-
-
Save drushadrusha/de50a8b67b164326f381184a4000340c to your computer and use it in GitHub Desktop.
Add Last.fm scrobble history in Obsidian
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// phpcs:disable | |
// take dump here https://benjaminbenben.com/lastfm-to-csv/ | |
$csv = file_get_contents("anarki728.csv"); | |
$resultFolder = "result"; | |
$verbose = false; | |
$dayFormat = "d.m.Y"; | |
$arr = explode("\n", $csv); | |
unset($arr[0]); | |
foreach($arr as $line){ | |
$data = explode(",", $line); | |
if(count($data) != 4){ | |
continue; | |
} | |
$stamp = strtotime($data[3]); | |
if($verbose){ | |
echo date('H:i', $stamp)." ".$data[0]." - ".$data[2]."\n"; | |
echo "Trying to write the ".date($dayFormat, $stamp)." file\n"; | |
} | |
writeLineToFile(date($dayFormat, $stamp), date('H:i', $stamp)." ".$data[0]." - ".$data[2]); | |
} | |
function writeLineToFile($date, $line) { | |
global $verbose; | |
$fileName = $date . '.md'; | |
$rootDir = __DIR__; | |
$filePath = findFileInSubfolders($rootDir, $fileName); | |
if (!$filePath) { | |
$filePath = $rootDir . '/' . $fileName; | |
file_put_contents($filePath, ""); | |
} | |
if ($filePath && file_exists($filePath)) { | |
$fileContents = file_get_contents($filePath); | |
if (strpos($fileContents, $line) === false) { | |
echo "Added ".$line." to the ".$filePath."\n"; | |
file_put_contents($filePath, PHP_EOL . $line, FILE_APPEND); | |
} else { | |
if($verbose){ | |
echo "The line already exists in the file.\n"; | |
} | |
} | |
} | |
} | |
function findFileInSubfolders($dir, $fileName) { | |
$files = scandir($dir); | |
foreach ($files as $file) { | |
if ($file === '.' || $file === '..') continue; | |
$filePath = $dir . '/' . $file; | |
if (is_dir($filePath)) { | |
$result = findFileInSubfolders($filePath, $fileName); | |
if ($result) { | |
return $result; | |
} | |
} elseif ($file === $fileName) { | |
return $filePath; | |
} | |
} | |
return false; | |
} | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment