-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
78 lines (66 loc) · 3.64 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
import os
import threading
from concurrent.futures import ThreadPoolExecutor, as_completed
from openai import OpenAI
from dotenv import load_dotenv
import instaloader
load_dotenv()
def scrape_instagram_profile(username):
loader = instaloader.Instaloader()
try:
profile = instaloader.Profile.from_username(loader.context, username)
return f"Here's my Instagram details:\nName: {profile.full_name} \nBio: {profile.biography}"
except Exception as e:
print(f"An error occurred: {e}")
return ""
def download_latest_captions(username, num_posts=10):
loader = instaloader.Instaloader()
try:
profile = instaloader.Profile.from_username(loader.context, username)
captions = []
for i, post in enumerate(profile.get_posts()):
if i >= num_posts:
break
captions.append(f"Post {i + 1}, Published Date: {post.date}:\n{post.caption}\n")
return "\n".join(captions)
except Exception as e:
print(f"An error occurred: {e}")
return ""
def generate_response(ig_username, industry, niche, extra, week):
try:
print("Calling----------------------------------------------------------------------------")
# Run Instagram scraping functions concurrently using ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=2) as executor:
future_bio = executor.submit(scrape_instagram_profile, ig_username)
future_captions = executor.submit(download_latest_captions, ig_username)
ig_bio = future_bio.result()
ig_captions = future_captions.result()
chat_prompt = f"""Act as a social media marketer with 10 years of experience.
I want you to generate a {week} content plan for Instagram that includes 1 reel and 1 post daily.
Give me detailed content idea plan for each reel and post for the day and not just the heading.
Use their latest posts caption data to learn about what kind of content these user upload on its
Instagram page and based on it generate content ideas and plan.
Here are some details of my Instagram account: {ig_bio}.The industry of the creator: {industry}
and niche is {niche} and some extra information - {extra}. Also here are the latest 10 post captions
of this instagram page. You can understand from these captions how and which type of content,
this instagram page mostly put on. Saying it again, To understand their Instagram page better,
understand the type of content from the latest posts. Latest 10 post(including reels) captions - {ig_captions}. Make
sure that the output is in HTML format only. Also dont mention the real world date and time, just put Day 1, Day2 and
so on. And dont print anything else"""
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a social media marketer with expertise of creating content plan for Instagram users."},
{"role": "user", "content": chat_prompt}
]
)
resp = completion.choices[0].message.content
print("Content plan generated.....................................................................")
return resp
except Exception as e:
return f"Couldn't generate response due to following error: {e}\nTry again after few minutes"
if __name__ == "__main__":
ig_username = "narendramodi"
result = generate_response(ig_username, "industry", "niche", "extra", "1 week")
print(result)