client
This module provides a Scopus client that efficiently uses your API keys.
It is highly recommended to use trio to run async functions
as it was much faster on our tests.
InvalidStringError
Bases: Exception
The response has a status code of 413 or 400. The search string might be too long.
Source code in src/sesg/scopus/client.py
224 225 | |
OutOfAPIKeysError
Bases: Exception
All API keys available are expired.
Source code in src/sesg/scopus/client.py
228 229 | |
Page
dataclass
A successfull Scopus Response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_results |
int
|
Number of results for this query. Notice that even if it displays more than 5000 results, Scopus will limit to retrieve only 5000. |
required |
n_pages |
int
|
Number of pages that needs to be fetched to get all results. Limited to 200 due to Scopus API 5000 entries limit. |
required |
current_page |
int
|
Current page being fetched. Starts at 1, being at most 200. |
required |
entries |
list[Entry]
|
Studies returned from the API. |
required |
Source code in src/sesg/scopus/client.py
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 | |
Entry
dataclass
A study entry returned from the API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scopus_id |
str
|
The ID of the study determined by Scopus. |
required |
title |
str
|
The title of the study. |
required |
cited_by_count |
Optional[int]
|
How many studies cites this one. |
required |
Source code in src/sesg/scopus/client.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
ScopusClient
Creates a client that cycles through the available keys to perform efficient searches.
Attributes:
| Name | Type | Description |
|---|---|---|
DUMMY_QUERY |
str
|
Used when a dummy query is needed. This value is used, for example, to check if the API key is expired. |
To perform a search, use the search method.
Note
You can purge the expired API keys with the purge_expired_keys method.
Source code in src/sesg/scopus/client.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | |
__init__(api_keys_list)
Initializes the instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_keys_list |
list[str]
|
List with API keys. |
required |
Source code in src/sesg/scopus/client.py
266 267 268 269 270 271 272 273 274 275 | |
delete_client(client)
Deletes a client from the list of clients.
Used when the client's API key is expired.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client |
httpx.AsyncClient
|
Client to be deleted. |
required |
Source code in src/sesg/scopus/client.py
277 278 279 280 281 282 283 284 285 286 287 288 | |
fetch(params)
async
Sends a request with the given params, if a client is available and returns the response.
Will recursively retry with another API key if the response's status code is 429.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params |
ScopusParams
|
Dictionary with the fetch parameters. |
required |
Raises:
| Type | Description |
|---|---|
OutOfAPIKeysError
|
If all API keys are expired. |
InvalidStringError
|
If the string is too long, meaning the response's status code is either 413, 429. |
Returns:
| Type | Description |
|---|---|
httpx.Response
|
The response obtained. |
Source code in src/sesg/scopus/client.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | |
fetch_and_parse(params)
async
Makes a request using the given parameters, and parses the response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params |
ScopusParams
|
Parameters of the request. |
required |
Raises:
| Type | Description |
|---|---|
InvalidStringError
|
If the response has a status code of 400 or 413. |
TooManyJSONDecodeErrors
|
If the maximum number of attempts on JSONDecodeError is reached. |
TooManyKeyErrors
|
If the maximum number of attempts on KeyError is reached. |
TooManySSLSErrors
|
If the maximum number of attempts on SSLError is reached. |
OutOfAPIKeysError
|
If all API keys are expired. |
Returns:
| Type | Description |
|---|---|
Page
|
A parsed response, meaning a |
Source code in src/sesg/scopus/client.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | |
fetch_first_page(query)
async
Requests for the first page of a query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query |
str
|
Query to request for the first page. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Page, list[ScopusParams]]
|
A tuple with the parsed response and a list of ScopusParams for pagination. |
Source code in src/sesg/scopus/client.py
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
get_expired_clients()
async
Verifies which clients have expired API keys.
Returns:
| Type | Description |
|---|---|
list[httpx.AsyncClient]
|
List of clients with expired API keys. |
Source code in src/sesg/scopus/client.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | |
purge_expired_clients()
async
Removes all clients with expired API keys from the list of clients.
Source code in src/sesg/scopus/client.py
466 467 468 469 470 471 | |
search(query, max_concurrent_tasks=None)
async
Performs concurrent requests to all of the pages of the given query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query |
str
|
The query to search for. |
required |
max_concurrent_tasks |
Optional[int]
|
The maximum number of concurrently running tasks. If None, will set to the number of pages of the query. |
None
|
Raises:
| Type | Description |
|---|---|
InvalidStringError
|
If the response has a status code of 400 or 413. |
TooManyJSONDecodeErrors
|
If the maximum number of attempts on JSONDecodeError is reached. |
TooManyKeyErrors
|
If the maximum number of attempts on KeyError is reached. |
OutOfAPIKeysError
|
If all API keys are expired. |
Yields:
| Type | Description |
|---|---|
AsyncIterable[Page]
|
A |
Source code in src/sesg/scopus/client.py
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | |
ScopusInternalError
Bases: Exception
The response has a status code of 500.
Source code in src/sesg/scopus/client.py
220 221 | |
ScopusParams
Bases: TypedDict
Data container for the required Scopus Params.
Attributes:
| Name | Type | Description |
|---|---|---|
query |
str
|
Query to be searched. |
start |
int
|
Paginator parameter. |
Examples:
>>> p: ScopusParams = {"query": "machine learning", "start": 0}
>>> p["query"] == "machine learning"
True
>>> p["start"] == 0
True
Source code in src/sesg/scopus/client.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
TooManyJSONDecodeErrors
Bases: Exception
Reached the maximum number of attempts on JSONDecodeError.
Source code in src/sesg/scopus/client.py
204 205 | |
TooManyKeyErrors
Bases: Exception
Reached the maximum number of attempts on KeyError.
Source code in src/sesg/scopus/client.py
208 209 | |
TooManySSLErrors
Bases: Exception
Reached the maximum number of attempts on SSLError.
Source code in src/sesg/scopus/client.py
216 217 | |
TooManyScopusInternalErrors
Bases: Exception
Reached the maximum number of attempts on ScopusInternalError.
Source code in src/sesg/scopus/client.py
212 213 | |
check_api_key_is_expired(response)
Checks if the given response indicates that the API key is expired.
An API key is expired if the response status code is 429.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response |
httpx.Response
|
Response to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the API key is expired, False otherwise. |
Source code in src/sesg/scopus/client.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
check_string_is_invalid(response)
Checks if the given response indicates that the string is invalid.
A string is invalid if the response status code is 400 or 413.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response |
httpx.Response
|
Response to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the string is invalid, False otherwise. |
Source code in src/sesg/scopus/client.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
create_clients_list(api_keys_list)
Creates a list of async httpx clients that can be used for Scopus queries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_keys_list |
list[str]
|
List with the API keys to be used. Each client will use one API Key. |
required |
Returns:
| Type | Description |
|---|---|
list[httpx.AsyncClient]
|
List of async clients. |
Source code in src/sesg/scopus/client.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
create_params_pagination(query, n_results)
Creates a list of ScopusParams for pagination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query |
str
|
Query to be searched. |
required |
n_results |
int
|
Number of results returned by Scopus. |
required |
Returns:
| Type | Description |
|---|---|
list[ScopusParams]
|
List of ScopusParams. |
Source code in src/sesg/scopus/client.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
parse_response(response)
Parses a Scopus API response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response |
httpx.Response
|
A Scopus API response. |
required |
Returns:
| Type | Description |
|---|---|
Page
|
A |
Source code in src/sesg/scopus/client.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
raise_too_many_json_decode_errors()
Raises a TooManyJSONDecodeErrors exception.
Source code in src/sesg/scopus/client.py
232 233 234 | |
raise_too_many_key_errors()
Raises a TooManyKeyErrors exception.
Source code in src/sesg/scopus/client.py
237 238 239 | |
raise_too_many_scopus_internal_errors()
Raises a TooManyScopusInternalErrors exception.
Source code in src/sesg/scopus/client.py
242 243 244 | |
raise_too_many_ssl_errors()
Raises a TooManySSLErrors exception.
Source code in src/sesg/scopus/client.py
247 248 249 | |