-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_post.py
More file actions
85 lines (62 loc) · 1.94 KB
/
upload_post.py
File metadata and controls
85 lines (62 loc) · 1.94 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
# Upload post on Instagram
import os
import json
from notify import notify
from instagrapi import Client
from dotenv import load_dotenv
from config import SOURCE_USERNAME
def login_insta():
load_dotenv()
cl = Client()
try:
cl.load_settings("session.json")
except FileNotFoundError:
print("No existing session found, logging in...")
source_password = os.getenv('SOURCE_PASSWORD')
cl.login(SOURCE_USERNAME, source_password)
cl.dump_settings("session.json")
print("Login successful, session saved")
return cl
def upload_post():
cl = login_insta()
# Load downloaded posts
with open("downloaded_posts.json", "r") as f:
data = json.load(f)
shortcodes = data["shortcodes"]
# Get the first shortcode
if shortcodes:
shortcode = shortcodes[0]
else:
# Exit function
print("No post to upload.")
return
# Upload post
print(f"Posting for: {shortcode}")
print("-" * 50)
# Get image and caption paths
img_path = "created_posts/" + shortcode + '.png'
caption_path = "created_posts/" + shortcode + '.txt'
# Get caption text
with open(caption_path, "r") as f:
caption_text = f.read()
# Upload photo
cl.photo_upload(
path=img_path,
caption=caption_text
)
print("Removing files...")
print("-" * 50)
# Remove from downloaded_posts
os.remove("downloaded_posts/" + shortcode + '.jpg')
os.remove("downloaded_posts/" + shortcode + '.txt')
# Remove from created_posts
os.remove(img_path)
os.remove(caption_path)
# Remove from downloaded_posts.json
data["shortcodes"] = [s for s in data["shortcodes"] if s != shortcode]
# Save updated data
with open("downloaded_posts.json", "w") as f:
json.dump(data, f, indent=2)
print(f"Post {shortcode} uploaded to Instagram successfully.")
notify("New post uploaded.")
upload_post()