-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
161 lines (140 loc) · 5.93 KB
/
app.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import streamlit as st
import requests
from bs4 import BeautifulSoup
from instabot import Bot
import time
import os
import glob
import random
import openai
import csv
from dotenv import load_dotenv
from typing import Iterator
from urllib.parse import urlparse, parse_qs
# Load environment variables from .env file
load_dotenv()
# Function to scrape Instagram profiles
def get_insta_accounts(location, num_pages=10):
profiles = []
for page in range(num_pages):
# Create search query for Google search
keyword_search_location = f"instagram smoke shop {location}"
start = page * 10
url = f"https://www.google.com/search?q={keyword_search_location}&start={start}"
# Set user agent header to avoid being blocked by Google
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
}
# Send request to Google and extract profiles from search results
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.text, "html.parser")
div_elements = soup.select("div.egMi0.kCrYT")
for div in div_elements:
# Extract the profile link from the href attribute
href = div.find("a")["href"]
parsed_url = urlparse(href)
query_params = parse_qs(parsed_url.query)
profile_link = query_params.get("url", [""])[0]
# Check if the profile link belongs to Instagram
if (
profile_link.startswith("https://www.instagram.com/")
and "/p/" not in profile_link
and "/explore/" not in profile_link
and "?utm_medium=copy_link" not in profile_link
and "?hl=ne" not in profile_link
):
# Extract the profile ID from the link
profile_id = profile_link.strip("/").split("/")[-1]
# Extract the title from the h3 element
title = div.select_one(
"div.DnJfK div.j039Wc h3 div.BNeawe.vvjwJb.AP7Wnd"
).text
# Append the profile information to the list
profiles.append(
{
"Title": title,
"Profile Link": profile_link,
"Profile ID": profile_id,
}
)
return profiles
# Function to generate messages using OpenAI API
def create_message(prompt, profile_id):
model_engine = "text-curie-001"
openai.api_key = os.environ['OPENAI_API_KEY'] # Access the environment variable
message_prompt = f"create an Instagram message for a new vape product 'HVQ' to {profile_id}"
response = openai.Completion.create(
engine=model_engine,
prompt=message_prompt,
temperature=0.5,
max_tokens=1024,
n=1,
stop=None,
frequency_penalty=0,
presence_penalty=0
)
return response.choices[0].text.strip()
# Function to send messages to Instagram users
def send_instagram_message(profile_id, message, bot):
# Delete existing cookie file to avoid login errors
cookie_path = "config/*cookie.json"
if glob.glob(cookie_path):
os.remove(glob.glob(cookie_path)[0])
bot.send_message(message, [profile_id])
time.sleep(random.randint(1, 5))
# Main Streamlit application function
def app():
st.title("Instagram DM Sender")
# Load the list of cities from the CSV file
cities = []
with open('./data/cities.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
city = row['City']
cities.append(city)
# Create a list of Instagram accounts
accounts = [
{
"username": os.getenv("INSTAGRAM_USERNAME_3"),
"password": os.getenv("INSTAGRAM_PASSWORD_3")
},
{
"username": os.getenv("INSTAGRAM_USERNAME_4"),
"password": os.getenv("INSTAGRAM_PASSWORD_4")
},
# Add more accounts if needed
]
# Loop through the list of cities and scrape Instagram profiles and send messages for each city
for city in cities:
# Scrape Instagram profiles for the selected city
st.write(f"Collecting Instagram profiles for {city}...")
profiles = list(get_insta_accounts(city))
st.subheader(f"Found {len(profiles)} profiles!")
if len(profiles) > 0:
st.write("Scraped profiles:")
for profile in profiles:
st.write(f"Title: {profile['Title']}")
st.write(f"Profile Link: {profile['Profile Link']}")
st.write(f"Profile ID: {profile['Profile ID']}")
st.write("---")
# Generate messages for each profile using OpenAI API
st.write("Generating messages...")
messages = []
for profile in profiles:
generated_message = create_message("create an Instagram message for a new vape product 'HVQ'", profile["Profile ID"])
messages.append(generated_message)
st.write(f"Generated message for {profile['Profile ID']}: {generated_message}")
# Send messages to each profile using different accounts
st.write("Sending messages...")
num_accounts = len(accounts)
for i, profile in enumerate(profiles):
account_index = i % num_accounts # Determine the index of the Instagram account to use
account = accounts[account_index]
bot = Bot()
bot.login(username=account["username"], password=account["password"])
send_instagram_message(profile["Profile ID"], messages[i], bot)
bot.logout()
st.write(f"Sent message to {profile['Profile ID']} using {account['username']}: {messages[i]}")
st.write("Messages sent!")
if __name__ == '__main__':
app()