Created
July 10, 2023 16:54
-
-
Save me-suzy/794c880782fb20b0be82f5f7a35b89df to your computer and use it in GitHub Desktop.
Sent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import urllib.request | |
import re | |
from subprocess import run | |
# Calea către fisierul index.html | |
index_file_path = r"file:///e:/Carte/BB/17%20-%20Site%20Leadership/Principal%202022/ro/index.html" | |
# index_file_path = "https://www.neculaifantanaru.com/index.html" | |
# Numele fisierului de stocare a ultimelor link-uri | |
link_file_path = r"link-actual.txt" | |
# Caută si salvează primul link în fisierul link-actual.txt | |
def save_first_link(): | |
response = urllib.request.urlopen(index_file_path) | |
html_content = response.read().decode() | |
pattern = r'<h3 class="font-weight-normal" itemprop="name"><a href="([\s\S]*?)" class="color-black">' | |
match = re.search(pattern, html_content) | |
if match: | |
link = match.group(1) | |
with open(link_file_path, 'w') as file: | |
file.write(link) | |
print("Link-ul actual a fost salvat:", link) | |
else: | |
print("Nu s-a găsit niciun link.") | |
# Verifică dacă un link există în fisierul link-actual.txt | |
def link_exists(link): | |
with open(link_file_path, 'r') as file: | |
links = file.readlines() | |
links = [x.strip() for x in links] | |
if link in links: | |
return True | |
else: | |
return False | |
# Rulează PERFECTO-2 pentru un link nou | |
def run_perfecto(link): | |
print("Link nou găsit:", link) | |
# Adăugați aici codul pentru a rula PERFECTO-2 cu link-ul dat | |
run(['python', 'Perfecto-2.py']) | |
# Verifică periodic index.html pentru link-uri noi | |
def monitor_index(): | |
while True: | |
response = urllib.request.urlopen(index_file_path) | |
html_content = response.read().decode() | |
pattern = r'<h3 class="font-weight-normal" itemprop="name"><a href="([\s\S]*?)" class="color-black">' | |
matches = re.findall(pattern, html_content) | |
for link in matches: | |
if not link_exists(link): | |
with open(link_file_path, 'r') as file: | |
links = file.readlines() | |
links = [x.strip() for x in links] | |
links.insert(0, link) | |
with open(link_file_path, 'w') as file: | |
file.write('\n'.join(links)) | |
run_perfecto(link) | |
# Asteaptă 1 minut înainte de a verifica din nou | |
time.sleep(60) | |
# Funcția de inițializare | |
def initialize(): | |
try: | |
# Verifică dacă fisierul link-actual.txt există si conține un link valid | |
with open(link_file_path, 'r') as file: | |
link = file.read().strip() | |
if link and link.startswith("http"): | |
print("Link-ul actual:", link) | |
else: | |
print("Fisierul link-actual.txt este gol sau conține un link nevalid.") | |
save_first_link() | |
except FileNotFoundError: | |
print("Fisierul link-actual.txt nu există.") | |
save_first_link() | |
# Apelul funcției de inițializare | |
initialize() | |
# Apelul funcției de monitorizare a index.html | |
monitor_index() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment