Skip to content

Instantly share code, notes, and snippets.

@glombard
Last active April 16, 2016 00:02
Show Gist options
  • Save glombard/acb96e63d16668f7a32e7c0f9b632a59 to your computer and use it in GitHub Desktop.
Save glombard/acb96e63d16668f7a32e7c0f9b632a59 to your computer and use it in GitHub Desktop.
Copy photos from my Android phone's SD card. Skip files that already exist locally.
"""Copy photos from my Android phone's SD card. Skip files that already exist locally.
"""
from __future__ import print_function
import subprocess
import csv
import os
photos_dirs = ['/storage/external_SD/DCIM/Camera', '/sdcard/DCIM/Camera']
directory_to_scan = os.path.expanduser('~/home/Photos')
target_directory = os.path.join(directory_to_scan, '2016-04-15 Photos from Gert''s Nexus 6P Phone')
def list_files(dir):
output = subprocess.check_output(['adb', 'shell', 'ls -la ' + dir + '/*'])
reader = csv.DictReader(output.splitlines(),
delimiter=' ',
skipinitialspace=True,
fieldnames=['permissions', 'owner', 'group', 'size', 'date', 'time', 'name'])
for row in reader:
name = row['name']
yield name
def list_local_files(dir):
result = set()
for dir, subdirs, files in os.walk(dir):
for file in files:
full_name = os.path.join(dir, file)
if os.path.getsize(full_name) > 10:
result.add(file)
return result
def adb_copy(src, dest):
subprocess.call(['adb', 'pull', '-a', src, dest])
existing_local_files = list_local_files(directory_to_scan)
try:
os.makedirs(target_directory)
except:
pass
def get_existing_android_dir(photos_dirs):
for dir in photos_dirs:
print('trying ' + dir)
output = subprocess.check_output(['adb', 'shell', 'test -d ' + dir + ' && echo $?'])
if output.startswith('0'):
return dir
raise Exception("Can't find directory")
photos_dir = get_existing_android_dir(photos_dirs)
for name in sorted(list_files(photos_dir)):
if name in existing_local_files:
print('{} - skipped'.format(name))
else:
print(name)
src = os.path.join(photos_dir, name)
dest = os.path.join(target_directory, name)
adb_copy(src, dest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment