Skip to content

Modifications to get it working #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mike-burns opened this issue May 31, 2024 · 0 comments
Open

Modifications to get it working #2

mike-burns opened this issue May 31, 2024 · 0 comments

Comments

@mike-burns
Copy link

Thanks for this base repo, it saved a ton of time for me. Here are the modifications I made to get it working on 31 May 2024:

  • "geo" is a JSON array, not object. In practice on my photos, the array had one element.
  • run_console is replaced with run_local_server(port=0).
  • check for 429 and sleep for 31 seconds after each response.
diff --git a/flickr-restore.py b/flickr-restore.py
index 7b32f39..212fb6d 100644
--- a/flickr-restore.py
+++ b/flickr-restore.py
@@ -4,6 +4,7 @@ import os
 import re
 import sys
 import logging
+import time
 
 from google.auth.transport.requests import AuthorizedSession
 from google.oauth2.credentials import Credentials
@@ -35,7 +36,7 @@ def get_authorized_session(client_secrets_file, auth_token_file):
     if not creds:
         flow = InstalledAppFlow.from_client_secrets_file(
             client_secrets_file, scopes=scopes)
-        creds = flow.run_console()
+        creds = flow.run_local_server(port=0)
 
     session = AuthorizedSession(creds)
     save_credentials(creds, auth_token_file)
@@ -81,7 +82,7 @@ class PhotoUploader:
             else:
                 logging.warning("Skipping invalid photo id: {}".format(flickr_photo_id))
 
-    def get_or_create_album(self, flickr_album):
+    def get_or_create_album(self, flickr_album, retry_count=2):
 
         params = {'excludeNonAppCreatedData': True}
 
@@ -102,7 +103,13 @@ class PhotoUploader:
         logging.info("Creating new album: '%s'" % flickr_album["title"])
         r = self.session.post("https://photoslibrary.googleapis.com/v1/albums", json={"album": {"title": flickr_album["title"]}})
         logging.debug("Create album response: {}".format(r.text))
-        r.raise_for_status()
+
+        if r.status_code == 429 and retry_count > 0:
+            time.sleep(31) # https://developers.google.com/photos/library/guides/best-practices#retrying-failed-requests
+            self.get_or_create_album(flickr_album, retry_count - 1)
+        else:
+            r.raise_for_status()
+
         google_album = r.json()
 
         if flickr_album["description"]:
@@ -124,7 +131,7 @@ class PhotoUploader:
         r = self.session.post("https://photoslibrary.googleapis.com/v1/albums/%s:addEnrichment" % google_album_id, json=enrich_req_body)
         logging.debug("Enrich album response: {}".format(r.text))
 
-    def upload_photo(self, flickr_photo_id, google_album_id, is_cover_photo):
+    def upload_photo(self, flickr_photo_id, google_album_id, is_cover_photo, retry_count=2):
         flickr_photo_fspath = self.flickr.get_photo_fspath(flickr_photo_id)
         logging.info("Uploading photo: '%s: %s'" % (flickr_photo_id, flickr_photo_fspath))
 
@@ -135,7 +142,13 @@ class PhotoUploader:
                 "X-Goog-Upload-Protocol": "raw"
             }
             resp = self.session.post("https://photoslibrary.googleapis.com/v1/uploads", data=f, headers=headers)
-            resp.raise_for_status()
+
+            if resp.status_code == 429 and retry_count > 0:
+                time.sleep(31) # https://developers.google.com/photos/library/guides/best-practices#retrying-failed-requests
+                self.upload_photo(flickr_photo_id, google_album_id, is_cover_photo, retry_count - 1)
+            else:
+                resp.raise_for_status()
+
             upload_token = resp.text
             logging.debug("Received upload token: %s" % upload_token)
 
@@ -151,7 +164,13 @@ class PhotoUploader:
         if is_cover_photo:
             create_request_body["albumPosition"] = {"position": "FIRST_IN_ALBUM"}
 
-        self.session.post("https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate", json=create_request_body).raise_for_status()
+        resp = self.session.post("https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate", json=create_request_body)
+
+        if resp.status_code == 429 and retry_count > 0:
+            time.sleep(31) # https://developers.google.com/photos/library/guides/best-practices#retrying-failed-requests
+            self.upload_photo(flickr_photo_id, google_album_id, is_cover_photo, retry_count - 1)
+        else:
+            resp.raise_for_status()
 
 
 def main(config):
diff --git a/flickr.py b/flickr.py
index 8f1aa5a..de05be1 100644
--- a/flickr.py
+++ b/flickr.py
@@ -69,8 +69,12 @@ class FlickrHelper:
 
     def get_photo_lat_lon(self, photo_id):
         photo_json = self.get_photo_json(photo_id)
-        geo = photo_json["geo"]
-        return geo
+        geos = photo_json["geo"]
+        try:
+            geo = geos[0]
+            return geo
+        except IndexError:
+            return None
 
     def has_photo_json(self, photo_id):
         photo_json_file = os.path.join(self.flickr_photo_json_dir, "photo_%s.json" % photo_id)
angrycandy added a commit to angrycandy/flickr-to-google-photos that referenced this issue Dec 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant