-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsta-archive-metadata.py
163 lines (122 loc) · 4.1 KB
/
insta-archive-metadata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""Tag an Instagram archive with metadata"""
import json
import logging
import os
import shlex
import subprocess as sp
import uuid
from datetime import datetime
from typing import Any, Sized
import click
import pytz
from tqdm.auto import tqdm
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def setup_logging() -> None:
logging.basicConfig(
level=logging.INFO,
format="[%(asctime)s] %(levelname)s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
handlers=[logging.FileHandler("ig.log")],
)
def p(collection: Sized) -> str:
return "s" if len(collection) != 1 else ""
def collect_images(source: str) -> list[dict[str, Any]]:
meta = []
target = os.path.join(source, "content", "posts_1.json")
if os.path.exists(target):
with open(target) as f:
meta += [img for item in json.load(f) for img in item["media"]]
target = os.path.join(source, "content", "profile_photos.json")
if os.path.exists(target):
with open(target) as f:
meta += json.load(f)["ig_profile_picture"]
target = os.path.join(source, "content", "stories.json")
if os.path.exists(target):
with open(target) as f:
meta += json.load(f)["ig_stories"]
# Rewrite paths
for m in meta:
m["uri"] = os.path.join(source, m["uri"])
return meta
def tag_and_copy_item(item: dict[str, Any], destdir: str) -> None:
new_uuid = uuid.uuid4()
dest_path = os.path.join(destdir, f"{new_uuid}{os.path.splitext(item['uri'])[-1]}")
timestamp = datetime.fromtimestamp(item["creation_timestamp"])
timestamp = timestamp.astimezone(pytz.timezone("America/New_York"))
# NB: ASCII so we've lost the emoji
description: str = item.get("title", "").strip()
try:
lat = list(item["media_metadata"].values())[0]["exif_data"][0]["latitude"]
lon = list(item["media_metadata"].values())[0]["exif_data"][0]["longitude"]
except KeyError:
lat, lon = None, None
run_exiftool(
source=item["uri"],
dest=dest_path,
timestamp=timestamp,
description=description,
latitude=lat,
longitude=lon,
)
run_setfile(
path=dest_path,
timestamp=timestamp,
)
def run_exiftool(
source: str,
dest: str,
timestamp: datetime,
description: str,
latitude: str,
longitude: str,
) -> None:
cmd = "exiftool"
if timestamp:
value = timestamp.strftime("%Y-%m-%dT%H:%M:%S%z")
cmd += f" -DatetimeOriginal='{value}'"
cmd += f" -DatetimeDigitized='{value}'"
if description:
cmd += f" -ImageDescription={shlex.quote(description)}"
if latitude:
cmd += f" -GPSLatitude='{latitude}'"
if longitude:
cmd += f" -GPSLongitudeRef=W -GPSLongitude='{longitude}'"
cmd += f' -o "{dest}" "{source}"'
logger.info(cmd)
job = sp.run(shlex.split(cmd), stderr=sp.PIPE, check=True, stdout=sp.DEVNULL)
if job.stderr:
logger.info(f"STDERR = {job.stderr.decode('utf8')}")
def run_setfile(path: str, timestamp: datetime) -> None:
value = timestamp.strftime("%m/%d/%y %H:%M:%S %P")
cmd = f"SetFile -d '{value}' {shlex.quote(path)}"
job = sp.run(cmd, shell=True, check=True, stderr=sp.PIPE, stdout=sp.DEVNULL)
if job.stderr:
logger.info(f"STDERR = {job.stderr.decode('utf8')}")
@click.command()
@click.option(
"--source",
"-s",
required=True,
type=click.Path(exists=True, file_okay=False),
help="Instagram archive root.",
)
@click.option(
"--output",
"-o",
required=True,
type=click.Path(exists=True, file_okay=False),
help="Export directory.",
)
def main(source: str, output: str) -> None:
setup_logging()
logger.info(f"source = {source}")
logger.info(f"output = {output}")
media = collect_images(source)
logger.info(f"Collected {len(media)} item{p(media)}")
destdir = os.path.join(output, datetime.now().strftime("%Y%m%d_%H%M%S"))
logger.info(f"Writing items to {destdir}")
for item in tqdm(media, unit="item"):
tag_and_copy_item(item, destdir)
if __name__ == "__main__":
main()