Skip to content

Instantly share code, notes, and snippets.

@mrbidon
Created February 18, 2024 11:23
Show Gist options
  • Select an option

  • Save mrbidon/7c5107db924b7394f11454c7f6d52927 to your computer and use it in GitHub Desktop.

Select an option

Save mrbidon/7c5107db924b7394f11454c7f6d52927 to your computer and use it in GitHub Desktop.

Revisions

  1. mrbidon created this gist Feb 18, 2024.
    108 changes: 108 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,108 @@
    #!/usr/bin/python3
    #
    # apt-get install libimobiledevice-utils ifuse libheif-examples
    #
    # fill these variable in main function to make it to works
    # raw_phone_directory = "/my/destination/backup/"
    # converted_phone_directory = "/my/directory/where/jpg/are/located/"
    # phone_mount_dir = "/directory/where/directory/is/monted"


    import subprocess
    import os


    class IPhoneSyncException(Exception):
    def __init__(self, cmd, p):
    self.cmd = cmd
    self.p = p

    def __str__(self):
    return repr(self.value)


    def run(cmd, raise_exception=True):
    p = subprocess.run(
    cmd,
    shell=True, capture_output=True)
    if raise_exception:
    if p.returncode != 0:
    raise IPhoneSyncException(cmd, p)

    return p


    def err(cmd, p):
    print(f"Can't execute '{cmd}'")
    print(p.returncode)
    print(p.stderr)
    print(p.stdout)


    class PairingException(Exception):
    def __init__(self, value):
    self.value = value

    def __str__(self):
    return repr(self.value)


    def pairing():
    finished = False
    while not finished:
    cmd = "idevicepair pair"
    p = run(cmd, False)
    if p.returncode != 0:
    msg = p.stdout.decode("utf8")
    if "Please enter the passcode on the device" in msg or "Please accept the trust dialog" in msg:
    print(p.stdout)
    text = input("enter 'n' to skip iphone sync or any key to retry")
    if text == "n":
    return False
    else:
    err(cmd, p)
    raise PairingException("")
    else:
    return True





    def convert(raw_phone_directory, converted_phone_directory):
    for (dirpath, dirnames, filenames) in os.walk(raw_phone_directory):
    if len(dirnames) > 0:
    print("There a sub dir in phone raw directory, not implemented")
    print(dirnames) # liste des sous répertoires de dirpath
    nb_converted = 0
    for filename in filenames:
    if filename.lower().endswith(".heic") :
    filename_no_ext = filename[:-5]
    if not os.path.exists("{converted_phone_directory}{filename_no_ext}.jpg"):
    run(f"heif-convert {raw_phone_directory}{filename} {converted_phone_directory}{filename_no_ext}.jpg")
    nb_converted += 1
    else:
    run(f"cp {raw_phone_directory}{filename} {converted_phone_directory}{filename}")
    print(f"nb file converted {nb_converted}")

    def main():
    raw_phone_directory = "/my/destination/backup/"
    converted_phone_directory = "/my/directory/where/jpg/are/located/"
    phone_mount_dir = "/directory/where/directory/is/monted"

    if pairing():
    try:
    run("idevicepair validate")
    run(f"fusermount -u {phone_mount_dir}", False)
    run(f"ifuse {phone_mount_dir}")
    run(f"rsync -avz {phone_mount_dir}/DCIM/100APPLE/ {raw_phone_directory}")
    convert(raw_phone_directory, converted_phone_directory)
    except IPhoneSyncException as e:
    err(e.cmd, e.p)
    finally:
    run(f"fusermount -u {phone_mount_dir}", False)


    if __name__ == '__main__':
    main()
    #convert()