Create linktoplama.py
Browse files- linktoplama.py +85 -0
linktoplama.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
+
from tqdm import tqdm
|
4 |
+
import concurrent.futures
|
5 |
+
import multiprocessing
|
6 |
+
import time
|
7 |
+
import telegram
|
8 |
+
import asyncio
|
9 |
+
import nest_asyncio
|
10 |
+
|
11 |
+
# Jupyter Notebook'un event loop'unu iç içe yerleştirme
|
12 |
+
nest_asyncio.apply()
|
13 |
+
|
14 |
+
# Telegram bot tokenınızı ve chat ID'nizi ekleyin
|
15 |
+
bot_token = "YOUR_BOT_TOKEN"
|
16 |
+
chat_id = "YOUR_CHAT_ID"
|
17 |
+
|
18 |
+
async def send_telegram_message(message):
|
19 |
+
"""Telegram API ile mesaj gönderir."""
|
20 |
+
try:
|
21 |
+
bot = telegram.Bot(token=bot_token)
|
22 |
+
await bot.sendMessage(chat_id=chat_id, text=message)
|
23 |
+
except Exception as e:
|
24 |
+
print(f"Telegram mesajı gönderilirken hata oluştu: {e}")
|
25 |
+
|
26 |
+
def fetch_url(url):
|
27 |
+
try:
|
28 |
+
response = requests.get(url)
|
29 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
30 |
+
return soup
|
31 |
+
except Exception as e:
|
32 |
+
print(f"Hata: {e}")
|
33 |
+
return None
|
34 |
+
|
35 |
+
def process_url(url):
|
36 |
+
try:
|
37 |
+
soup = fetch_url(url)
|
38 |
+
if soup:
|
39 |
+
mesaj_sayisi_elementi = soup.find('li', {'title': 'Mesaj'})
|
40 |
+
if mesaj_sayisi_elementi:
|
41 |
+
mesaj_sayisi_raw = mesaj_sayisi_elementi.get_text(strip=True)
|
42 |
+
mesaj_sayisi = ''.join(filter(str.isdigit, mesaj_sayisi_raw))
|
43 |
+
mesaj_sayisi = int(mesaj_sayisi)
|
44 |
+
if mesaj_sayisi > 10:
|
45 |
+
sayfa_sayisi = mesaj_sayisi // 10 + 1
|
46 |
+
with open('/content/yeniurl.txt', 'a') as yeni_url_file:
|
47 |
+
yeni_url_file.write(f"{url}\n")
|
48 |
+
for sayfa in range(2, sayfa_sayisi + 1):
|
49 |
+
yeni_url = f"{url}page-{sayfa}"
|
50 |
+
with open('/content/yeniurl.txt', 'a') as yeni_url_file:
|
51 |
+
yeni_url_file.write(f"{yeni_url}\n")
|
52 |
+
else:
|
53 |
+
with open('/content/yeniurl.txt', 'a') as yeni_url_file:
|
54 |
+
yeni_url_file.write(f"{url}\n")
|
55 |
+
|
56 |
+
with open('/content/logyeni.txt', 'a') as output_file:
|
57 |
+
output_file.write(f"{url}: {mesaj_sayisi}\n")
|
58 |
+
else:
|
59 |
+
with open('/content/logyeni.txt', 'a') as output_file:
|
60 |
+
output_file.write(f"{url}: Mesaj sayısı bulunamadı\n")
|
61 |
+
except Exception as e:
|
62 |
+
with open('/content/logyeni.txt', 'a') as output_file:
|
63 |
+
output_file.write(f"{url}: Hata - {str(e)}\n")
|
64 |
+
|
65 |
+
async def main():
|
66 |
+
urls = []
|
67 |
+
with open('/content/urlss.txt', 'r') as file:
|
68 |
+
urls = file.readlines()
|
69 |
+
urls = [url.strip() for url in urls]
|
70 |
+
|
71 |
+
start_time = time.time()
|
72 |
+
processed_urls = 0
|
73 |
+
total_urls = len(urls)
|
74 |
+
|
75 |
+
with concurrent.futures.ProcessPoolExecutor(max_workers=5 * multiprocessing.cpu_count()) as executor:
|
76 |
+
for _ in tqdm(executor.map(process_url, urls), total=total_urls):
|
77 |
+
processed_urls += 1
|
78 |
+
elapsed_time = time.time() - start_time
|
79 |
+
if elapsed_time >= 600: # 10 dakika (600 saniye)
|
80 |
+
message = f"İlerleme: {processed_urls}/{total_urls}"
|
81 |
+
await send_telegram_message(message)
|
82 |
+
start_time = time.time() # Zamanlayıcıyı sıfırla
|
83 |
+
|
84 |
+
if __name__ == "__main__":
|
85 |
+
asyncio.run(main())
|