refactor the semaphore when dumping listings
This commit is contained in:
parent
b7a2ea75aa
commit
29213f3d26
2 changed files with 34 additions and 29 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
from dataclasses import dataclass
|
|
||||||
import pathlib
|
import pathlib
|
||||||
|
from typing import Any
|
||||||
from rec.query import listing_query, QueryParameters
|
from rec.query import listing_query, QueryParameters
|
||||||
from rec.districts import get_districts
|
from rec.districts import get_districts
|
||||||
from data_access import Listing
|
from data_access import Listing
|
||||||
|
|
@ -24,20 +24,7 @@ async def dump_listings(
|
||||||
semaphore = asyncio.Semaphore(5) # if too high, rightmove drops connections
|
semaphore = asyncio.Semaphore(5) # if too high, rightmove drops connections
|
||||||
json_responses = await asyncio.gather(
|
json_responses = await asyncio.gather(
|
||||||
*[
|
*[
|
||||||
listing_query(
|
_dump_listings_with_semaphore(semaphore, i, parameters, locid)
|
||||||
page=i,
|
|
||||||
channel=parameters.listing_type,
|
|
||||||
min_bedrooms=parameters.min_bedrooms,
|
|
||||||
max_bedrooms=parameters.max_bedrooms,
|
|
||||||
radius=parameters.radius,
|
|
||||||
min_price=parameters.min_price,
|
|
||||||
max_price=parameters.max_price,
|
|
||||||
location_id=locid,
|
|
||||||
page_size=parameters.page_size,
|
|
||||||
max_days_since_added=parameters.max_days_since_added,
|
|
||||||
furnish_types=parameters.furnish_types or [],
|
|
||||||
semaphore=semaphore,
|
|
||||||
)
|
|
||||||
for locid in districts.values()
|
for locid in districts.values()
|
||||||
for i in [1, 2]
|
for i in [1, 2]
|
||||||
]
|
]
|
||||||
|
|
@ -57,3 +44,26 @@ async def dump_listings(
|
||||||
listings.append(listing)
|
listings.append(listing)
|
||||||
|
|
||||||
return listings
|
return listings
|
||||||
|
|
||||||
|
|
||||||
|
async def _dump_listings_with_semaphore(
|
||||||
|
semaphore: asyncio.Semaphore,
|
||||||
|
page_id: int,
|
||||||
|
parameters: QueryParameters,
|
||||||
|
location_id: str,
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
async with semaphore:
|
||||||
|
listing = await listing_query(
|
||||||
|
page=page_id,
|
||||||
|
channel=parameters.listing_type,
|
||||||
|
min_bedrooms=parameters.min_bedrooms,
|
||||||
|
max_bedrooms=parameters.max_bedrooms,
|
||||||
|
radius=parameters.radius,
|
||||||
|
min_price=parameters.min_price,
|
||||||
|
max_price=parameters.max_price,
|
||||||
|
location_id=location_id,
|
||||||
|
page_size=parameters.page_size,
|
||||||
|
max_days_since_added=parameters.max_days_since_added,
|
||||||
|
furnish_types=parameters.furnish_types or [],
|
||||||
|
)
|
||||||
|
return listing
|
||||||
|
|
|
||||||
|
|
@ -121,7 +121,6 @@ async def listing_query(
|
||||||
property_type: list[PropertyType] = [],
|
property_type: list[PropertyType] = [],
|
||||||
page_size: int = 25,
|
page_size: int = 25,
|
||||||
furnish_types: list[FurnishType] = [],
|
furnish_types: list[FurnishType] = [],
|
||||||
semaphore: asyncio.Semaphore | None = None,
|
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
params: dict[str, str] = {
|
params: dict[str, str] = {
|
||||||
"locationIdentifier": location_id,
|
"locationIdentifier": location_id,
|
||||||
|
|
@ -164,16 +163,12 @@ async def listing_query(
|
||||||
"Connection": "keep-alive",
|
"Connection": "keep-alive",
|
||||||
}
|
}
|
||||||
|
|
||||||
if semaphore is None:
|
async with aiohttp.ClientSession(trust_env=True) as session:
|
||||||
semaphore = asyncio.Semaphore(1)
|
async with session.get(
|
||||||
|
"https://api.rightmove.co.uk/api/property-listing",
|
||||||
async with semaphore:
|
params=params,
|
||||||
async with aiohttp.ClientSession(trust_env=True) as session:
|
headers=headers,
|
||||||
async with session.get(
|
) as response:
|
||||||
"https://api.rightmove.co.uk/api/property-listing",
|
if response.status != 200:
|
||||||
params=params,
|
raise Exception(f"Failed due to: {await response.text()}")
|
||||||
headers=headers,
|
return await response.json()
|
||||||
) as response:
|
|
||||||
if response.status != 200:
|
|
||||||
raise Exception(f"Failed due to: {await response.text()}")
|
|
||||||
return await response.json()
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue