Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Refactoring and Efficiency Improvements #49

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 29 additions & 75 deletions IGDTUW/main.py
Original file line number Diff line number Diff line change
@@ -1,92 +1,46 @@
import requests #importing modules
import requests
import lxml.html
import time
import json

html = requests.get('https://www.igdtuw.ac.in/')
doc = lxml.html.fromstring(html.content)
# Function to extract the latest notice information
def get_latest_notice(doc):
title = doc.xpath('//div[@class="container"]/div[@class="row"]//div[@class="col-sm-7"]/div[@class="bigBox"]/div[@class="bigBoxDiv"]/ul[@class="ENABox Events"]/li/a/text()')[0]
link = doc.xpath('//div[@class="container"]/div[@class="row"]//div[@class="col-sm-7"]/div[@class="bigBox"]/div[@class="bigBoxDiv"]/ul[@="ENABox Events"]/li/a/@href')[0]
return {
"title": title,
"link": "https://www.igdtuw.ac.in/" + link,
"tab": "Notices/Circulars"
}

prev = doc.xpath(
'//div[@class="container"]/div[@class="row"]//div[@class="col-sm-7"]/div[@class="bigBox"]/div[@class="bigBoxDiv"]/ul[@class="ENABox Events"]/li/a'
)[0]
# Your endpoint URL
endpoint_url = 'http://your_endpoint'

print("running")
while True:
try:
last_title = doc.xpath(
'//div[@class="container"]/div[@class="row"]//div[@class="col-sm-7"]/div[@class="bigBox"]/div[@class="bigBoxDiv"]/ul[@class="ENABox Events"]/li/a/text()'
)[0]
last_link = doc.xpath(
'//div[@class="container"]/div[@class="row"]//div[@class="col-sm-7"]/div[@class="bigBox"]/div[@class="bigBoxDiv"]/ul[@class="ENABox Events"]/li/a/@href'
)[0]

last_notice = {
"title": last_title,
"link": "https://www.igdtuw.ac.in/" + last_link,
"tab": "Notices/Circulars"
}
# url3 = 'http://20.205.15.220/last'
# x3 = requests.post(url3, json=last_notice)
# print(x3.text)
print(last_notice)
except Exception as e:
print(e)
notice = {"title": "Bot Down", "link": "", "tab": ""}
url = 'http://your_endpoint'
x = requests.post(url, json=notice)
break
# Initialize previous notice
prev_notice = get_latest_notice(lxml.html.fromstring(requests.get('https://www.igdtuw.ac.in/').content))

print("Running")
while True:
try:
prev_hash = hash(prev)
new_release = doc.xpath(
'//div[@class="container"]/div[@class="row"]//div[@class="col-sm-7"]/div[@class="bigBox"]/div[@class="bigBoxDiv"]/ul[@class="ENABox Events"]/li/a'
)[0]
new_hash = hash(new_release)
new_notice = get_latest_notice(lxml.html.fromstring(requests.get('https://www.igdtuw.ac.in/').content))

if (new_release != prev):
if new_notice != prev_notice:
# Send the new notice to your endpoint
response = requests.post(endpoint_url, json=new_notice)
print(new_notice)

prev_hash = hash(prev)
new_release = doc.xpath(
'//div[@class="container"]/div[@class="row"]//div[@class="col-sm-7"]/div[@class="bigBox"]/div[@class="bigBoxDiv"]/ul[@class="ENABox Events"]/li/a'
)[0]
new_hash = hash(new_release)
title = doc.xpath(
'//div[@class="container"]/div[@class="row"]//div[@class="col-sm-7"]/div[@class="bigBox"]/div[@class="bigBoxDiv"]/ul[@class="ENABox Events"]/li/a/text()'
)[0]
link = doc.xpath(
'//div[@class="container"]/div[@class="row"]//div[@class="col-sm-7"]/div[@class="bigBox"]/div[@class="bigBoxDiv"]/ul[@class="ENABox Events"]/li/a/@href'
)[0]
html2 = requests.get("https://www.igdtuw.ac.in/"+link)
doc2 = lxml.html.fromstring(html2.content)
new_release_data_link = doc2.xpath('//div[@class="headingPara"]//table[@class="facultyTable"]//a/@href')[0]
notice = {
"title": title,
"link": "https://www.igdtuw.ac.in/" + new_release_data_link,
"new_link":"https://www.igdtuw.ac.in/" + link,
"tab": "Notices/Circulars"
}
url = 'http://your_endpoint'
data = json.dumps(notice)
# x = requests.post(url, json=notice)
# print(x.text)
print(notice)
prev = new_release
if response.status_code == 200:
prev_notice = new_notice
else:
print("Failed to send notice to the endpoint")

else:
print("no update")
prev_hash = hash(prev)
new_release = doc.xpath(
'//div[@class="container"]/div[@class="row"]//div[@class="col-sm-7"]/div[@class="bigBox"]/div[@class="bigBoxDiv"]/ul[@class="ENABox Events"]/li/a'
)[0]
print("No update")

new_hash = hash(new_release)

time.sleep(10) #the program will run every 10 seconds.
continue
time.sleep(10) # The program will run every 10 seconds.

except Exception as e:
print(e)
notice = {"title": "Bot Down", "link": "", "tab": ""}
url = 'http://your_endpoint'
x = requests.post(url, json=notice)
break
response = requests.post(endpoint_url, json=notice)
break