-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
141 lines (111 loc) · 3.99 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
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
import discord
from discord.ext import commands
import asyncio, os
from selenium_driverless import webdriver
from selenium_driverless.types.by import By
from dotenv import load_dotenv
load_dotenv()
user = str(os.getenv("USER")
passd = str(os.getenv("PASS")
token = os.getenv("TOKEN")
def get_filtered_list(all_num, l2):
for index, i in enumerate(all_num):
if i == l2[0]:
newlist = all_num[index:]
if l2[:len(newlist)] == newlist:
l2 = l2[len(newlist):]
break
return l2
def remove_consecutive_duplicates(lst):
if not lst:
return []
result = []
last_seen = None
for num in lst:
if num != last_seen:
result.append(num)
last_seen = num
return result
async def get_instagram_reel_views(reel_url):
driver = await webdriver.Chrome()
total_views = 0
try:
await driver.get("https://www.instagram.com/accounts/login/?source=auth_switcher")
await asyncio.sleep(3)
username = await driver.find_element(By.NAME, "username")
await username.send_keys(user)
password = await driver.find_element(By.NAME, "password")
await password.send_keys(passd)
xpath_query = f"//div[contains(text(), 'Log in')]"
submit = await driver.find_element(By.XPATH, xpath_query)
await submit.click()
await asyncio.sleep(8)
await driver.get(reel_url)
await asyncio.sleep(15)
all_numbers = []
counter = 0
while True:
script = """
let aajyDivs = document.querySelectorAll('div._aajy');
if (!aajyDivs) {
return [];
}
let numbers = [];
aajyDivs.forEach((aajyDiv) => {
let spanElement = aajyDiv.querySelector('span.html-span');
let text = spanElement.innerText.trim();
let match = text.match(/^([\\d,]+(?:\\.\\d+)?)([KkMm]?)$/);
if (match) {
let num = match[1].replace(",", "");
let suffix = match[2];
// Convert based on suffix
if (suffix.toLowerCase() === "k") {
num = parseFloat(num) * 1000;
} else if (suffix.toLowerCase() === "m") {
num = parseFloat(num) * 1000000;
}
numbers.push(parseInt(num));
}
});
return numbers;
"""
numbers = await driver.execute_script(script)
new_numbers = []
for num in numbers:
if isinstance(num, int):
new_numbers.append(num)
else:
try:
num = int(num)
new_numbers.append(num)
except:
continue
new_numbers = remove_consecutive_duplicates(new_numbers)
new_numbers = get_filtered_list(all_numbers, new_numbers)
all_numbers.extend(new_numbers)
if new_numbers:
counter = 0
else:
if counter > 2:
break
counter += 1
await driver.execute_script("window.scrollBy(0, 950);")
await asyncio.sleep(5)
total_views = sum(all_numbers)
finally:
await driver.quit()
return total_views
intents = discord.Intents.all()
bot = commands.Bot(command_prefix="?", intents=intents)
@bot.event
async def on_ready():
print("BOT IS ONLINE")
@bot.command()
async def views(ctx, reel_url):
await ctx.send("Fetching reel views...")
try:
total_views = await get_instagram_reel_views(reel_url)
await ctx.send(f"Total Views: {total_views}")
except Exception as e:
await ctx.send(f"An error occurred: {str(e)}")
bot.run(token)