0

I'm working on a Python script to sync Google Photos/Picasa with a local folder (on Mac). Each album on Picasa = a local folder.

Some of my album titles have non-Ascii characters in them, and I cannot work out how to properly compare these album titles with the local folder names. A match is never found, and so the local folder is always deleted and then recreated. Can anyone help me solve this?

The code below shows my current script, including my attempt to convert album titles and folder names to unicode, which I thought would be the answer:

import os
import gdata.photos.service

username = 'mattbarr99'
target = '/Users/home/Desktop/GooglePhotos/'

gd_client = gdata.photos.service.PhotosService()

# get Google album titles
google_albums = gd_client.GetUserFeed(user=username)
google_album_titles = [unicode(album.title.text, 'utf-8') for album in google_albums.entry]

# check local folders
for entry in os.listdir(target):
    if not os.path.isdir(os.path.join(target, entry)):
        continue
    if unicode(entry, 'utf-8') not in google_album_titles:
        print "removing local folder: %s" % unicode(entry, 'utf-8')
        os.rmdir(os.path.join(target, entry))

# check Google albums
for album in google_albums.entry:
    local_album_path = os.path.join(target, album.title.text)
    if not os.path.exists(local_album_path):
        print "creating local folder: %s" % local_album_path
        os.mkdir(local_album_path)

The print out that I see when running the script is always:

removing local folder: Aspö 2015
creating local folder: /Users/home/Desktop/GooglePhotos/Aspö 2015

I'm assuming that the problem is text encoding, and that I just don't know the right way to handle it. I hope I've provided enough information: this is my first post on stack overflow. Any help much appreciated!

Matt Barr
  • 1
  • 1

1 Answers1

0

I figured it out. The problem was on the Mac side rather than the Google side. Here's the changed line of code that resolved the problem:

...
if unicodedata.normalize('NFC', unicode(entry, 'utf-8')) not in google_album_titles:
...

And for more info, here's the stack overflow page I found that helped me figure it:

Unicode encoding for filesystem in Mac OS X not correct in Python?

Community
  • 1
  • 1
Matt Barr
  • 1
  • 1