Facebook
From Hasnyne Mostafa, 10 Months ago, written in Python.
Embed
Download Paste or View Raw
Hits: 210
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import concurrent.futures
  4. import re
  5. import logging
  6. import threading
  7.  
  8. logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
  9.  
  10. # Lock for thread-safe file operations
  11. write_lock = threading.Lock()
  12.  
  13. # Global counter for numbering the results
  14. result_counter = 0
  15.  
  16. def get_result_data(roll):
  17.     url = 'https://sresult.bise-ctg.gov.bd/s24/individual/result.php'
  18.     data = {'roll': roll}
  19.     try:
  20.         res = requests.post(url, data)
  21.         res.raise_for_status()
  22.     except requests.RequestException as e:
  23.         logging.error(f"Request failed for roll {roll}: {e}")
  24.         return None
  25.    
  26.     sp = BeautifulSoup(res.text, 'html.parser')
  27.     if sp.find('h2', string='SSC Result 2024') is None:
  28.         logging.warning(f"No result found for roll {roll}")
  29.         return None
  30.  
  31.     try:
  32.         nums = sp.find_all('td', class_='cap_lt')
  33.         marks = sp.find_all('td', class_='bg_grey')
  34.  
  35.         total = 0
  36.         gpa = ''
  37.         group = ''
  38.         school = ''
  39.         subject_marks = []
  40.  
  41.         for j, i in enumerate(nums):
  42.             if re.search(r'GPA=\d+\.\d+', str(i)) is not None:
  43.                 gpa = re.search(r'GPA=(\d+\.\d+)', str(i)).group(1)
  44.             if j == 3:
  45.                 group = i.text if i else ''
  46.             if j == 8:
  47.                 school = i.text if i else ''
  48.  
  49.         name_elem = sp.find('td', width=True, class_='cap_lt')
  50.         name = name_elem.text if name_elem else ''
  51.  
  52.         for mark in marks:
  53.             mark_text = mark.text if mark else ''
  54.             if re.search(r'\d{3}\(.+\)', mark_text) is not None:
  55.                 subject_marks.append(re.search(r'(\d{3})', mark_text).group(1))
  56.                 total += int(re.search(r'(\d{3})', mark_text).group(1))
  57.  
  58.         result_data = {
  59.             'roll': roll,
  60.             'name': name,
  61.             'total': total,
  62.             'gpa': gpa,
  63.             'group': group,
  64.             'school': school,
  65.             'subject_marks': subject_marks
  66.         }
  67.         return result_data
  68.     except Exception as e:
  69.         logging.error(f"Error parsing data for roll {roll}: {e}")
  70.         return None
  71.  
  72. def write_result_data(result_data):
  73.     global result_counter
  74.     with write_lock:
  75.         result_counter += 1
  76.         with open("result_data.txt", "a") as f_main, open("subject_marks.txt", "a") as f_marks:
  77.             main_result_str = f"{result_counter}\t{result_data['name']}\t{result_data['roll']}\t{result_data['gpa']}\t{result_data['total']}\t{result_data['school']}"
  78.             f_main.write(main_result_str + '\n')
  79.  
  80.             marks_result_str = f"{result_data['roll']}\t" + "\t".join(result_data['subject_marks'])
  81.             f_marks.write(marks_result_str + '\n')
  82.  
  83. def process_rolls(rolls):
  84.     results = []
  85.     for roll in rolls:
  86.         result_data = get_result_data(str(roll))
  87.         if result_data:
  88.             results.append(result_data)
  89.     for result_data in results:
  90.         write_result_data(result_data)
  91.  
  92. def process_range(start, end, max_workers=50):
  93.     logging.info(f"Processing range {start}-{end}...")
  94.     rolls = list(range(start, end))
  95.     chunk_size = len(rolls) // max_workers
  96.     with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
  97.         executor.map(process_rolls, [rolls[i:i + chunk_size] for i in range(0, len(rolls), chunk_size)])
  98.     logging.info(f"Range {start}-{end} complete")
  99.  
  100. if __name__ == "__main__":
  101.     start_range = int(input("starting roll: "))
  102.     end_range = int(input("Final roll: "))
  103.     max_worker = 50
  104.  
  105.     step = max(1, (end_range - start_range) // max_worker)
  106.     ranges = [(i, min(end_range, i + step)) for i in range(start_range, end_range, step)]
  107.  
  108.     for start, end in ranges:
  109.         process_range(start, end, max_worker)
  110.  
  111.     logging.info("Processing complete. Results are written to 'result_data.txt' and 'subject_marks.txt'.")
  112.