Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # fixdates.pl (pwtenny AT the place where google mail goes)
- # 2021-12-30
- # Extract time/date stamps from JSON files in Google Photos backups/exports and set them
- # for all the archived photos and videos on the local file system. Or something.
- # Where to start looking for photos. Everything in this directory is searched recursively.
- # $start shouldn't have ANY photos in it, if it does, they aren't going to get got.
- $start = "D:/Documents/Downloads/takeout-20211229T132532Z-001/Takeout/Google Photos/";
- opendir($dh, $start) || die "The \$start var isn't a valid directory. It's not my fault. IT'S NOT MY FAULT.\n";
- # Iterate through every file/folder in $start
- while(readdir $dh)
- {
- # Trying to open ".." will briefly read the parent directory of $start, and "." will make us read $start twice.
- next if($_ eq '..' || $_ eq '.');
- if(-d $_)
- {
- search_directory($start.$_);
- }
- }
- # Iterate through everything in a sub-directory of $start, ignoring .JSON files.
- sub search_directory
- {
- $where = shift;
- opendir($ldh, $where);
- # This is not a recursive search. AFAIK Google Photo exports everything in one-layer-deep directories
- # and that's it. No sub-directories in any of them. (Perhaps that's possible?)
- while(readdir $ldh)
- {
- next if($_ eq '..' || $_ eq '.');
- next if($_ =~ /\.json/); # Don't want these.
- print "$where: ".$_."\n"; # Delete this if you don't want a bunch of console spam. But you should. Spam is delicious.
- set_datetime($where.'/'.$_);
- }
- print "\n";
- }
- # Open a .JSON file and DIRTY parse out a timestamp, then set the file attributes accordingly.
- # I honestly don't know if every JSON file has both a photoTakenTime and a creationTime,
- # but I do know that we want photoTakenTime if it exists, since creationTime is *always* when the file
- # was uploaded to Google Photos.
- sub set_datetime
- {
- $file = shift;
- $json = $file.".json";
- $ts = "";
- open(fh,$json);
- $json_data = join("", <fh>);
- close fh;
- if($json_data =~ /photoTakenTime/)
- {
- ($photoTakenTime) = $json_data =~ /"photoTakenTime": \{(.*?)\}/gs;
- ($ts) = $photoTakenTime =~ /"timestamp": "(.*?)",/gs;
- }
- else
- {
- ($ts) = $json_data =~ /"timestamp": "(.*?)",/gs;
- }
- use Win32API::File::Time qw{:win};
- $truefalse = SetFileTime($file, $ts, $ts, $ts);
- # Notice how I didn't check the return code to see if it worked?
- }
- # Code Monkey get up get coffee
- # Code Monkey go to job
- # Code Monkey have boring meeting
- # With boring manager Rob
- # Rob say Code Monkey very dilligent
- # But his output stink
- # His code not “functional” or “elegant”
- # What do Code Monkey think?
- # Code Monkey think maybe manager want to write god damned login page himself
- # Code Monkey not say it out loud
- # Code Monkey not crazy, just proud
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement