-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_data.py
69 lines (65 loc) · 2.56 KB
/
fetch_data.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
from instagrapi import Client
import json
import sqlite3
from pathlib import Path
con = sqlite3.connect('karani.db')
cur = con.cursor()
cl = Client(json.load(open('session.json')))
# cl.login("naren.alt", "Saapa1@2")
# json.dump(
# cl.get_settings(),
# open('session.json', 'w')
# )
test = cl.account_info().dict()
account = cl.account_info()
# max_id = ""
# new_max_id = "123"
# user_list = []
# while True:
# user_list, new_max_id = cl.user_following_v1_chunk(account.pk, 50, max_id)
# # store user objects from here
# if new_max_id == max_id:
# break
# max_id = new_max_id
following_dict = cl.user_following(account.pk, amount=10)
for user in following_dict.values():
folder_path = Path('storage/' + user.username)
if not folder_path.exists():
folder_path.mkdir()
profile_pic_path = str(folder_path) + '/profile_picture'
cl.photo_download_by_url(user.profile_pic_url, 'profile_picture', folder_path)
insert = f"""
INSERT INTO "main"."USER" ("id", "name", "profile_pic", "username", "bio", "location_id")
VALUES ('{str(user.pk)}', '{user.full_name}', '{profile_pic_path}', '{user.username}', '', '')
ON CONFLICT(id) DO NOTHING;
"""
cur.execute(insert)
con.commit()
posts = cl.user_medias_v1(user.pk, 20)
for post in posts:
if post.thumbnail_url is not None:
image_path = str(cl.photo_download_by_url(post.thumbnail_url, str(post.pk), folder_path))
insert = f"""
INSERT INTO "main"."POSTS" ("post_id", "media", "timestamp", "user_id")
VALUES ('{str(post.pk)}', '{image_path}', '{post.taken_at}', '{str(user.pk)}')
ON CONFLICT(post_id) DO NOTHING;
"""
cur.execute(insert)
con.commit()
else:
for resource in post.resources:
image_path = str(cl.photo_download_by_url(resource.thumbnail_url, str(resource.pk), folder_path))
insert = f"""
INSERT INTO "main"."POSTS" ("post_id", "media", "timestamp", "user_id")
VALUES ('{str(resource.pk)}', '{image_path}', '{post.taken_at}', '{str(user.pk)}')
ON CONFLICT(post_id) DO NOTHING;
"""
cur.execute(insert)
con.commit()
# end_cursor = ""
# while True:
# new_posts, end_cursor = cl.user_medias_paginated(user.pk, amount=10, end_cursor=end_cursor)
# if not new_posts:
# break
# posts.append(new_posts)
# store posts