This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
92 lines (80 loc) · 3.78 KB
/
main.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
from WorksheetBuilder import Worksheet
from InstaFile import InstaFile
from InstaPost import InstaPost
from os.path import join, isdir, isfile
from os import listdir
from GUI import GUI
"""
This small application is a stand-alone extension for Instaloader (https://instaloader.github.io/). Its
task is to merge *.json.xz files (metadata of instagram posts), images and comments generated by Instaloader into an
*.xlsx file. This makes it easier to view the post texts in context with the images. Before using the tool, please read
the README first.
"""
def main():
# let the user define a source directory containing the *xz.json-files
src_path = GUI()
src_path.browse_folder_layout()
src_path.create_window()
# let the user define a path and a name for the generated xlsx-file
save_path = GUI()
save_path.save_to_file_layout()
save_path.create_window()
# looping over the directories and files in the given folder to retrieve the file paths, if the files fit the schema
list_json_path = []
for item in listdir(src_path.values[0]):
# check for sub-folders in the directory and if there are, loop over them
if isdir(join(src_path.values[0], item)):
for file in listdir(join(src_path.values[0], item)):
# check if the files' extension is *.xz.json
if file[-8:].lower() == InstaFile.meta_ext:
list_json_path.append(join(src_path.values[0], item, file))
# check if the files' extension is *.xz.json
if file[-4:].lower() == ".jpg":
# raising the counter for each counted image
GUI.total_jpg += 1
# if the handled item is a file and not a folder (see above), do the same checking steps
elif isfile(join(src_path.values[0], item)):
if item[-8:].lower() == InstaFile.meta_ext:
list_json_path.append(join(src_path.values[0], item))
if item[-4:].lower() == ".jpg":
GUI.total_jpg += 1
# check if there were xz.json-files in the directory
if not list_json_path:
GUI.no_insta_load_files()
# initialize the progress bar for computing the files
progress = GUI()
progress.progress_bar_layout()
progress.create_progress_bar()
# loop over the retrieved xz.json paths and bind the corresponding image or images to them
for json_path in list_json_path:
insta_file = InstaFile()
insta_file.parse_json_path(json_path)
# collect the corresponding images in a list
insta_file.get_jpg_files()
# collect the corresponding comments path
insta_file.get_comments()
post = InstaPost(insta_file)
# decompress the xz.json file and extract only the necessary data (drop the Instaloader-Key)
post.json_to_meta_data()
# save the extracted metadata in the post object
post.pipe_meta_data()
# save the extracted comment data in the post object
post.json_to_comment_data()
post.pipe_comment_data()
# rescale all images and store them in a folder named thumbnails in the images' directory
post.make_img()
# bind the metadata and the image(s) in a dictionary in the post object
post.add_to_all_posts()
# update the progress bar for each handled post
progress.progress_bar_update(GUI.jpg_counter, GUI.total_jpg)
ws = Worksheet(InstaPost.all_posts)
# add the original posts to the xlsx-worksheet
ws.add_to_post_ws()
# add the comments to the xlsx-worksheet
ws.add_to_comments_ws()
# create workbook with the user defined path
ws.create_workbook(save_path.values[0])
progress.progress_bar_update(GUI.total_jpg, GUI.total_jpg)
progress.close_progress_bar()
if __name__ == '__main__':
main()