Skip to content

Instantly share code, notes, and snippets.

@iwanders
Created January 16, 2016 15:17
Show Gist options
  • Save iwanders/e2e5613d061c0445c086 to your computer and use it in GitHub Desktop.
Save iwanders/e2e5613d061c0445c086 to your computer and use it in GitHub Desktop.
android_pull_photos
#!/usr/bin/env python3
"""
Simple command line tool to pull photos from an Android phone using ADB.
Puts the files into monthly folders and deletes files from the phone that
are older than 30 days.
"""
import subprocess
import time
import datetime
import os
import sys
import hashlib
# returns list of files in a directory on the phone
def get_file_list(phone_dir):
a = subprocess.check_output(['adb', 'shell', 'ls', phone_dir])
a = a.split(b'\r\n')
return a
# copies file from phone to pc.
def copy_file(phone_path, dest_folder):
b = subprocess.check_output(['adb', 'pull', phone_path, dest_folder])
return b
# gets md5sum for a file, calculated on the phone
def get_md5(phone_path):
hash = subprocess.check_output(['adb', 'shell', 'md5sum', phone_path])
hash = str(hash, 'ascii')
hash = hash[:hash.find(" ")]
return hash
# removes file on the phone
def phone_del(phone_path):
res = subprocess.check_output(['adb', 'shell', 'rm', phone_path])
return res
# calculates local md5 sum.
def local_md5(filepath):
with open(filepath, 'rb') as file_to_check:
data = file_to_check.read()
md5_returned = hashlib.md5(data).hexdigest()
return md5_returned
# grab the photos that are yet to be grabbed.
def grab_ungrabbed_photos(file_list, local_dir, phone_dir):
current = datetime.datetime.now()
for fname in file_list:
if (len(fname) == 0):
continue
# fname = fname
f = str(fname, 'ascii')
date_str = f[f.find(r"_")+1:-4]
date = time.strptime(date_str, '%Y%m%d_%H%M%S')
thisdate = datetime.datetime.strptime(date_str, '%Y%m%d_%H%M%S')
formatted_str = time.strftime("%Y %b", date)
file_dest_folder = os.path.join(local_dir, formatted_str)
if (not os.path.exists(file_dest_folder)):
os.mkdir(file_dest_folder)
local_path = os.path.join(file_dest_folder, f)
phone_location = os.path.join(phone_dir, f)
if (os.path.isfile(os.path.join(file_dest_folder, f))):
pass
else:
print("Downloading file {:}:".format(str(fname, 'ascii')))
b = copy_file(phone_location, file_dest_folder)
if ((current - thisdate).days >= args.delete):
print("file is old... verifying and deleting")
phone_hash = get_md5(phone_location)
local_hash = local_md5(local_path)
if (phone_hash == local_hash):
print("Hashes match, deleting from phone.")
res = phone_del(phone_location)
# print('"'+ get_md5("%s/%s" % (phone_dir, f)) + '"')
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--delete",
help="Delete files from phone older than n days old.",
type=int,
default=30)
parser.add_argument("-p", "--phone-dir", help="Path on telephone",
default="/sdcard/DCIM/Camera/")
parser.add_argument("-l", "--local-dir", help="Path on computer",
default="./data/")
args = parser.parse_args()
filelist = get_file_list(phone_dir=args.phone_dir)
# print(filelist)
grab_ungrabbed_photos(filelist,
local_dir=args.local_dir,
phone_dir=args.phone_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment