Skip to content

Instantly share code, notes, and snippets.

@ymyzk
Created April 21, 2018 15:47
Show Gist options
  • Save ymyzk/af872107d9c30515b58dd1d534d0a9ab to your computer and use it in GitHub Desktop.
Save ymyzk/af872107d9c30515b58dd1d534d0a9ab to your computer and use it in GitHub Desktop.
python-gyazo-backup migrator
#!/usr/bin/env python3
import json
import sys
def main(new_filename, old_filename):
with open(old_filename) as f:
old_images = json.load(f)
with open(new_filename) as f:
new_images = json.load(f)
created_at_images_map = {}
for image in old_images:
created_at = image["created_at"]
candidates = created_at_images_map.get(created_at, [])
candidates.append(image)
created_at_images_map[created_at] = candidates
created_at_id_map = {}
for created_at, candidates in created_at_images_map.items():
for image in candidates:
image_id = image.get("image_id")
if image_id:
created_at_id_map[created_at] = image_id
for image in new_images:
created_at = image["created_at"]
if image.get("image_id"):
continue
image_id = created_at_id_map.get(created_at)
if not image_id:
print("failed to match: " + image["created_at"])
continue
image["image_id"] = image_id
image["permalink_url"] = "https://api.gyazo.com/" + image_id
image["url"] = "https://i.gyazo.com/" + image_id + "." + image["type"]
with open(new_filename, "w") as f:
json.dump(new_images, f, indent=2)
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Please specify 'new_images.json' and 'old_images.json'", file=sys.stderr)
sys.exit(1)
main(sys.argv[1], sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment