Skip to content

Instantly share code, notes, and snippets.

@sycobuny
Created June 9, 2011 15:06
Show Gist options
  • Save sycobuny/1016910 to your computer and use it in GitHub Desktop.
Save sycobuny/1016910 to your computer and use it in GitHub Desktop.
Pull files off my iPhone
#!/usr/bin/perl
####
# this script is not safe.
# don't expect it to sanitize your input; it doesn't.
# so don't use it on a web server or anywhere else someone untrusted can access
# it.
# it was designed to work on Linux Mint.
# it requires sqlite3 to be installed, and is invoked from the command line.
# I don't have a license in this file, but you can read the postgres/MIT/BSD
# license and pretty much assume all of that applies: free to use, credit me,
# no warranty, blah blah.
# TODO items include:
# make it more interactive (select one or more files)
# make it prettier
# make it safer
# make it handle errors more gracefully and clearly to average users
# make the configuration easier to understand?
####
use warnings;
use strict;
####
# config section
# replace these with whatever settings you'd need
use constant USER => 'sycobuny'; # who you are
use constant PHONE => 'troubadour'; # what your phone is named
use constant HOMEDIR => "/home/@{[USER]}"; # where your homedir is
use constant DESTDIR => HOMEDIR . # where to dump files
'/Desktop/';
use constant FONEDIR => HOMEDIR . # where the phone gets mounted
'/.gvfs/' .
PHONE;
use constant LIBFILE => FONEDIR . # your sqlite iTunes Library DB
'/iTunes_Control/iTunes/iTunes Library.itlp/Library.itdb';
use constant LOCFILE => FONEDIR . # your sqlite file locations DB
'/iTunes_Control/iTunes/iTunes Library.itlp/Locations.itdb';
use constant SRCDIR => FONEDIR . # where the music files are located
'/iTunes_Control/Music/';
####
# verify files exist
unless (-e(LIBFILE) && -e(LOCFILE)) {
print "@{[LIBFILE]} does not exist\n" unless -e(LIBFILE);
print "@{[LOCFILE]} does not exist\n" unless -e(LOCFILE);
print "Required database(s) do not exist.\n";
print "(Perhaps you need to plug in your phone?)\n";
exit(1);
}
####
# set up vars
my ($artist, $title) = @ARGV;
my ($query, $ids);
# run sql to get item ids
$query = <<SQL;
SELECT pid,
artist,
title
FROM item
WHERE artist LIKE '\%$artist\%'
SQL
$query .= qq{AND title LIKE '\%$title\%'} if $title;
$ids = `sqlite3 "@{[LIBFILE]}" "$query"`;
unless ($ids) {
print "Could not find any results.\n";
exit(1);
}
foreach my $id (split("\n", $ids)) {
my ($item_id, $artist, $title) = split(/\|/, $id);
my ($query, $location, $extension, $source, $destination);
$query = <<SQL;
SELECT location
FROM location
WHERE item_pid = $item_id
SQL
$location = `sqlite3 "@{[LOCFILE]}" "$query"`;
chomp $location;
$extension = (split(/\./, $location))[1];
$source = "@{[SRCDIR]}$location";
$artist =~ s/\//_/g;
$title =~ s/\//_/g;
$destination = "@{[DESTDIR]}$artist - $title.$extension";
`cp \"$source\" \"$destination\"`;
last;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment