-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
82 lines (69 loc) · 2.89 KB
/
main.py
File metadata and controls
82 lines (69 loc) · 2.89 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import asyncio
import httpx
from modules.data_extraction import (
station_data_extraction,
ticketcount_data_extraction,
hourly_data_extraction,
)
from modules.data_store import ticket_count_dataset, hourly_dataset, station_dataset
from modules.logger import setup_logger
# Setup logger
logger = setup_logger()
allTicketCount_url = (
"https://commuters-dataapi.chennaimetrorail.org/api/PassengerFlow/allTicketCount/1"
)
hourlybaseddata_url = (
"https://commuters-dataapi.chennaimetrorail.org/api/PassengerFlow/hourlybaseddata/1"
)
stationData_url = (
"https://commuters-dataapi.chennaimetrorail.org/api/PassengerFlow/stationData/1"
)
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36"
}
async def scrape_ticketcount(client, url):
logger.info("Starting ticket count data scraping")
response = await client.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
ticketcount_df = ticketcount_data_extraction(data)
ticket_count_dataset(ticketcount_df)
logger.info("Successfully scraped and stored ticket count data")
else:
logger.error(f"Failed to fetch ticket count data: {response.status_code}")
async def scrape_hourly_data(client, url):
logger.info("Starting hourly data scraping")
response = await client.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
hourly_df = hourly_data_extraction(data)
hourly_dataset(hourly_df)
logger.info("Successfully scraped and stored hourly data")
else:
logger.error(f"Failed to fetch hourly data: {response.status_code}")
async def scrape_station_data(client, url):
logger.info("Starting station data scraping")
response = await client.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
formatted_data = station_data_extraction(data)
for line_no, line_df in formatted_data.items():
station_dataset(line_no, line_df)
logger.info(f"Successfully stored data for line {line_no}")
logger.info("Successfully scraped and stored all station data")
else:
logger.error(f"Failed to fetch station data: {response.status_code}")
async def main():
logger.info("Starting CMRL data scraping process")
async with httpx.AsyncClient() as client:
try:
await asyncio.gather(
scrape_ticketcount(client, allTicketCount_url),
scrape_hourly_data(client, hourlybaseddata_url),
scrape_station_data(client, stationData_url),
)
logger.info("Completed CMRL data scraping process")
except Exception as e:
logger.error(f"An error occurred during scraping: {str(e)}", exc_info=True)
if __name__ == "__main__":
asyncio.run(main())