Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3099949f6c | |||
| b7d3c30c86 |
@@ -1,63 +1,79 @@
|
|||||||
|
import requests
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
import re
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
db_name = 'mydb.db'
|
class Spyder:
|
||||||
|
def __init__(self):
|
||||||
class User:
|
self.db_name = 'database.db'
|
||||||
def __init__(self,name,email,age):
|
self.conn = sqlite3.connect(self.db_name)
|
||||||
self.__id = None
|
self.cursor = self.conn.cursor()
|
||||||
self.__name = name
|
def init_db(self):
|
||||||
self.__email = email
|
query = '''CREATE TABLE links(
|
||||||
self.__age = age
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
link TEXT NOT NULL,
|
||||||
@property
|
last_scan INTEGER DEFAULT 0,
|
||||||
def name(self):
|
title TEXT DEFAULT ''
|
||||||
return self.__name
|
)'''
|
||||||
@name.setter
|
self.cursor.execute(query)
|
||||||
def name(self,value):
|
def scan(self,url):
|
||||||
self.__name = value
|
if url.endswith('.pdf'):
|
||||||
|
return
|
||||||
@property
|
headers = {
|
||||||
def email(self):
|
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36'
|
||||||
return self.__email
|
}
|
||||||
|
try:
|
||||||
@email.setter
|
response = requests.get(
|
||||||
def email(self,value):
|
url,
|
||||||
if '@' in value:
|
headers=headers,
|
||||||
self.__email = value
|
timeout=5
|
||||||
|
)
|
||||||
|
except:
|
||||||
|
self.update_url(url,'Scan error')
|
||||||
|
return
|
||||||
|
html = response.text
|
||||||
|
soup = BeautifulSoup(html,'html.parser')
|
||||||
|
result = soup.find_all("a", href=True)
|
||||||
|
title = soup.title.text
|
||||||
|
self.update_url(url,title)
|
||||||
|
for el in result:
|
||||||
|
if el['href'].endswith('.pdf'):
|
||||||
|
continue
|
||||||
|
if not el['href'].startswith('http'):
|
||||||
|
match = re.findall(r'https?://[A-Za-z0-9.-]+',url)
|
||||||
|
link = f"{match[0]}{el['href']}"
|
||||||
else:
|
else:
|
||||||
raise ValueError('Не корректный адрес почты')
|
link = el['href']
|
||||||
@property
|
query = f'SELECT id FROM links WHERE link = ?'
|
||||||
def age(self):
|
self.cursor.execute(query,(link,))
|
||||||
return self.__age
|
if self.cursor.fetchone():
|
||||||
@age.setter
|
continue
|
||||||
def age(self,value):
|
query = f'INSERT INTO links(link) VALUES(?)'
|
||||||
if 0 <= value <= 120:
|
self.cursor.execute(query,(link,))
|
||||||
self.__age = value
|
self.conn.commit()
|
||||||
else:
|
def update_url(self,url,title):
|
||||||
raise ValueError('Неправильно указан возраст')
|
query = "SELECT id FROM links WHERE link = ?"
|
||||||
|
self.cursor.execute(query,(url,))
|
||||||
|
link_id = self.cursor.fetchone()
|
||||||
|
if link_id:
|
||||||
|
query = "UPDATE links SET title=?, last_scan=? WHERE id=?"
|
||||||
|
self.cursor.execute(query,(title,datetime.now().timestamp(),link_id[0]))
|
||||||
|
self.conn.commit()
|
||||||
|
def get_url(self):
|
||||||
|
query = "SELECT link FROM links ORDER BY last_scan LIMIT 1"
|
||||||
|
self.cursor.execute(query)
|
||||||
|
return self.cursor.fetchone()
|
||||||
|
def close(self):
|
||||||
|
self.conn.close()
|
||||||
|
|
||||||
def save(self):
|
if __name__ == '__main__':
|
||||||
conn = sqlite3.connect(db_name)
|
spyder = Spyder()
|
||||||
cursor = conn.cursor()
|
#spyder.init_db()
|
||||||
property_list = [f"{k}" for k, v in User.__dict__.items() if isinstance(v, property)]
|
#spyder.scan('https://habr.com/ru/news/1046177/')
|
||||||
if '_User__id' in self.__dict__.keys():
|
for _ in range(5):
|
||||||
update_string = ','.join([f'`{p_name}`= ? ' for p_name in property_list])
|
url = spyder.get_url()
|
||||||
update_values = [getattr(self,p) for p in property_list] + [self.__id]
|
print(url[0])
|
||||||
query = f"UPDATE {User.__name__} SET {update_string} WHERE id = ?"
|
spyder.scan(url[0])
|
||||||
print(query,update_values)
|
spyder.close()
|
||||||
else:
|
|
||||||
property_count = ('?,'*len(property_list))[:-1] #','.join(['?' for _ in range(len(property_list))])
|
|
||||||
property_names = ','.join(property_list)
|
|
||||||
property_values = [getattr(self,p) for p in property_list]
|
|
||||||
query = f"INSERT INTO {User.__name__}({property_names}) VALUES({property_count})"
|
|
||||||
print(query,property_values)
|
|
||||||
cursor.execute(query,property_values)
|
|
||||||
conn.commit()
|
|
||||||
self.__id = cursor.lastrowid
|
|
||||||
conn.close()
|
|
||||||
#print(User.__dict__)
|
|
||||||
|
|
||||||
user1 = User('Igor','igor@example.com',44)
|
|
||||||
user1.save()
|
|
||||||
user1.age = 55
|
|
||||||
user1.save()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user