-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstatonote_backup.py
More file actions
119 lines (91 loc) · 3.56 KB
/
instatonote_backup.py
File metadata and controls
119 lines (91 loc) · 3.56 KB
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
import instaloader
import os
import glob
from jinja2 import Environment, FileSystemLoader
"""Saves instagram saved posts"""
class Instasaved:
def __init__(self, caption, shortcode):
try:
self.title = caption[:50]
self.caption = caption
except:
self.title = shortcode
self.caption = ''
self.shortcode = shortcode
self.url = f'https://instagram.com/p/{shortcode}'
self.url_text = self.url.split("/")[2]
self.image_url = f'{shortcode}.jpg'
def download_settings(username):
setting = instaloader.Instaloader(dirname_pattern='posts', save_metadata=False,
compress_json=False, download_comments=False,
post_metadata_txt_pattern='', filename_pattern='{shortcode}/{shortcode}',
download_geotags=False)
setting.interactive_login(username)
return setting
def get_profile(username, setting):
profile = instaloader.Profile.from_username(setting.context, username)
return profile
def download_saved(profile, setting):
for post in profile.get_saved_posts():
setting.download_post(post, target=None)
def make_dirs(directory):
if not os.path.exists(directory):
os.makedirs(directory)
def find_images(shortcode):
'''finds images files in directory'''
path = os.path.join('**', shortcode, '*.jpg')
image_list = glob.glob(path)
jpg_images = []
for img in image_list:
imgurl = img.split('\\')[1:]
imgurl = '/'.join(imgurl)
jpg_images.append(imgurl)
return jpg_images
def find_videos(shortcode):
path = os.path.join('**', shortcode, '*.mp4')
vid_list = glob.glob(path)
mp4_videos = []
for vid in vid_list:
vidurl = vid.split('\\')[1:]
vidurl = '/'.join(vidurl)
mp4_videos.append(vidurl)
return mp4_videos
def return_template(template):
file_loader = FileSystemLoader('templates')
env = Environment(loader=file_loader)
template = env.get_template('template.html')
return template
def jinja_output(title, url, url_text, jpg_images, mp4_videos, caption, template):
render = template.render(title=title, url=url, url_text=url_text,
images=jpg_images, videos=mp4_videos, caption=caption)
return render
def html_file(render, shortcode):
saved_html = os.path.join('posts', f'{shortcode}.html')
with open(saved_html, 'w', encoding='utf-8') as f:
f.write(render)
print(f'Saving {shortcode}')
def move_videos():
PATH = os.path.join('posts', '**', '*.mp4')
videos = glob.glob(PATH)
for vid in videos:
dest = os.path.join('posts', vid.split('\\')[-1])
os.rename(vid, dest)
print(f'moving {vid}')
def main():
make_dirs('posts')
username = input("\nEnter Instagram username: ")
setting = download_settings(username)
profile = get_profile(username, setting)
download_saved(profile, setting)
for post in profile.get_saved_posts():
I = Instasaved(post.caption, post.shortcode)
title, caption, shortcode, url, url_text, image_url = I.title, I.caption, I.shortcode, I.url, I.url_text, I.image_url
jpg_images = find_images(shortcode)
mp4_videos = find_videos(shortcode)
template = return_template('template.html')
render = jinja_output(title, url, url_text,
jpg_images, mp4_videos, caption, template)
html_file(render, shortcode)
move_videos()
if __name__ == "__main__":
main()