-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (58 loc) · 2.12 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
import requests
from bs4 import BeautifulSoup
from pyrogram import Client
from pyrogram.types import Message
from config import Config
import time
import os
from typing import List
import textwrap
PASTES_FILE = 'pastes.txt'
def scrape_pastelink() -> List[str]:
url = 'https://pastelink.net/recent'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
table = soup.find('table', id='listing')
rows = table.find_all('tr')[1:] # skip the header row
scraped_urls = set()
if os.path.exists(PASTES_FILE):
with open(PASTES_FILE, 'r') as f:
scraped_urls = set(f.read().splitlines())
messages = []
for row in rows:
cols = row.find_all('td')
paste_url = cols[0].find('a')['href']
paste_age = cols[1].text.strip()
paste_views = cols[2].text.strip()
paste_title = cols[0].text.strip()
paste_url = f'https://pastelink.net{paste_url}'
if paste_url in scraped_urls:
continue
paste_response = requests.get(paste_url)
paste_soup = BeautifulSoup(paste_response.content, 'html.parser')
paste_body = paste_soup.find('div', id='body-display').text.strip()
message = f'<b>{paste_title}</b> ({paste_views} views, {paste_age})\n{paste_url}\n\n{paste_body}\n\n'
messages.append(message)
scraped_urls.add(paste_url)
time.sleep(1)
with open(PASTES_FILE, 'w') as f:
f.write('\n'.join(scraped_urls))
return messages
def send_to_telegram(client: Client, message: Message):
paste_content = scrape_pastelink()
for paste in paste_content:
paste_chunks = textwrap.wrap(paste, width=4096, replace_whitespace=False)
for chunk in paste_chunks:
client.send_message(chat_id=Config.CHANNEL_ID, text=chunk, parse_mode='HTML')
time.sleep(4)
if __name__ == '__main__':
client = Client(
'my_session',
api_id=Config.BOT_API_ID,
api_hash=Config.BOT_API_HASH,
bot_token=Config.BOT_TOKEN
)
with client:
while True:
send_to_telegram(client, None)
time.sleep(14400)