-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
50 lines (37 loc) · 1.73 KB
/
script.py
File metadata and controls
50 lines (37 loc) · 1.73 KB
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
import csv
import requests
def audit_csv():
# Using the exact filename
file_path = 'Task 2 - Intern.csv'
# Adding contact info to User-Agent is a Wikimedia best practice
headers = {
'User-Agent': 'Outreachy-Auditor/1.0 (shehrbanoali2230@gmail.com)'
}
print(f"--- Starting Audit on {file_path} ---")
try:
with open(file_path, mode='r', encoding='utf-8') as file:
reader = csv.reader(file)
# FIX 1: SKIPPING THE HEADER
# The None ensures it won't crash even if the file is empty
next(reader, None)
for row in reader:
# Skips empty rows to prevent index errors
if not row:
continue
#Joins row with commas in case the URL contained one
url = ",".join(row).strip()
try:
# Timeout is crucial for large-scale audits
res = requests.get(url, headers=headers, timeout=5)
# res.reason will automatically provide names like OK or Not Found
print(f"[{res.status_code} {res.reason}] {url}")
# FIX 2: PRINTING THE SPECIFIC EXCEPTION NAME
except requests.exceptions.RequestException as e:
# Extracts name like 'ConnectionError' or 'ReadTimeout'
error_name = type(e).__name__
print(f"[FAILED: {error_name}] {url}")
except FileNotFoundError:
print(f"Error: {file_path} not found. Please check the file name.")
print("--- Audit Complete ---")
if __name__ == "__main__":
audit_csv()