API Reference
This section is generated from the codebase using mkdocstrings.
gkc
GKC - Global Knowledge Commons
A Python package for working with the Global Knowledge Commons including Wikidata, Wikipedia, Wikimedia Commons, and OpenStreetMap.
AuthenticationError
Bases: Exception
Raised when authentication fails.
Source code in gkc/auth.py
34 35 36 37 | |
ClaimsMapBuilder
Builds claims mapping configurations from ShEx schemas.
This class combines ShEx schema analysis with live Wikidata property metadata to generate skeleton mapping configurations.
Source code in gkc/mapping_builder.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 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 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 | |
__init__(eid=None, schema_text=None, schema_file=None, user_agent=None)
Initialize the claims map builder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eid
|
Optional[str]
|
EntitySchema ID (e.g., 'E502') |
None
|
schema_text
|
Optional[str]
|
ShEx schema as text |
None
|
schema_file
|
Optional[str]
|
Path to ShEx schema file |
None
|
user_agent
|
Optional[str]
|
Custom user agent for API requests |
None
|
Source code in gkc/mapping_builder.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |
build_claims_map(include_qualifiers=True, include_references=True)
Build claims mapping structure from the loaded ShEx schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_qualifiers
|
bool
|
Whether to include qualifier properties |
True
|
include_references
|
bool
|
Whether to include reference properties |
True
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of claim mapping dictionaries |
Source code in gkc/mapping_builder.py
246 247 248 249 250 251 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 | |
build_complete_mapping(entity_type=None)
Build a complete mapping configuration skeleton.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_type
|
Optional[str]
|
Wikidata QID of the entity type (e.g., 'Q7840353') |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Complete mapping configuration dictionary |
Source code in gkc/mapping_builder.py
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 | |
load_schema()
Load the ShEx schema.
Source code in gkc/mapping_builder.py
240 241 242 243 244 | |
print_summary()
Print a summary of the ShEx schema analysis.
Source code in gkc/mapping_builder.py
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 | |
OpenStreetMapAuth
Bases: AuthBase
Authentication for OpenStreetMap.
Credentials can be provided in three ways (in order of precedence): 1. Direct parameters 2. Environment variables (OPENSTREETMAP_USERNAME, OPENSTREETMAP_PASSWORD) 3. Interactive prompt
Example
Using environment variables
auth = OpenStreetMapAuth()
Direct parameters
auth = OpenStreetMapAuth(username="myuser", password="mypass")
Interactive prompt
auth = OpenStreetMapAuth(interactive=True) Enter OpenStreetMap username: myuser Enter OpenStreetMap password: ****
Source code in gkc/auth.py
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 | |
__init__(username=None, password=None, interactive=False)
Initialize OpenStreetMap authentication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
username
|
Optional[str]
|
OpenStreetMap username. If not provided, reads from OPENSTREETMAP_USERNAME environment variable. |
None
|
password
|
Optional[str]
|
OpenStreetMap password. If not provided, reads from OPENSTREETMAP_PASSWORD environment variable. |
None
|
interactive
|
bool
|
If True and credentials are not found, prompt user for input. |
False
|
Source code in gkc/auth.py
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 | |
PropertyInfo
Container for Wikidata property information.
Source code in gkc/mapping_builder.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
get_description(language='en')
Get property description in specified language.
Source code in gkc/mapping_builder.py
33 34 35 36 37 | |
get_label(language='en')
Get property label in specified language.
Source code in gkc/mapping_builder.py
27 28 29 30 31 | |
SPARQLError
Bases: Exception
Raised when a SPARQL query fails.
Source code in gkc/sparql.py
26 27 28 29 | |
SPARQLQuery
Execute SPARQL queries against a SPARQL endpoint.
Source code in gkc/sparql.py
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 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 | |
__init__(endpoint=DEFAULT_WIKIDATA_ENDPOINT, user_agent=DEFAULT_USER_AGENT, timeout=30)
Initialize SPARQL query executor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
SPARQL endpoint URL (default: Wikidata) |
DEFAULT_WIKIDATA_ENDPOINT
|
user_agent
|
str
|
User agent string for HTTP requests |
DEFAULT_USER_AGENT
|
timeout
|
int
|
Request timeout in seconds |
30
|
Source code in gkc/sparql.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
normalize_query(query)
staticmethod
Normalize a SPARQL query string.
If the query appears to be a Wikidata Query Service URL, extract and decode it. Otherwise, return as-is.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
SPARQL query string or Wikidata Query Service URL |
required |
Returns:
| Type | Description |
|---|---|
str
|
Normalized SPARQL query string |
Source code in gkc/sparql.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
parse_wikidata_query_url(url)
staticmethod
Extract and decode SPARQL query from Wikidata Query Service URL.
The Wikidata Query Service URL format is:
https://query.wikidata.org/#
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Wikidata Query Service URL |
required |
Returns:
| Type | Description |
|---|---|
str
|
Decoded SPARQL query string |
Raises:
| Type | Description |
|---|---|
SPARQLError
|
If URL is not a valid Wikidata Query Service URL |
Example
url = "https://query.wikidata.org/#SELECT%20%3Fitem..." query = SPARQLQuery.parse_wikidata_query_url(url)
Source code in gkc/sparql.py
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
query(query, format='json', raw=False)
Execute a SPARQL query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
SPARQL query string or Wikidata Query Service URL |
required |
format
|
str
|
Response format ('json', 'xml', 'csv', 'tsv') |
'json'
|
raw
|
bool
|
If False, parse JSON to Python dict; if True, return raw string |
False
|
Returns:
| Type | Description |
|---|---|
Any
|
Query results (dict if JSON and raw=False, else string) |
Raises:
| Type | Description |
|---|---|
SPARQLError
|
If query fails |
Example
executor = SPARQLQuery() results = executor.query( ... '''SELECT ?item ?itemLabel WHERE { ... ?item wdt:P31 wd:Q7840353 . ... SERVICE wikibase:label { ... bd:serviceParam wikibase:language "en" . ... } ... }''' ... )
Source code in gkc/sparql.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
to_csv(query, filepath=None)
Execute a SPARQL query and return results as CSV.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
SPARQL query string or Wikidata Query Service URL |
required |
filepath
|
Optional[str]
|
Optional file path to save CSV results |
None
|
Returns:
| Type | Description |
|---|---|
str
|
CSV string |
Example
executor = SPARQLQuery() csv_data = executor.to_csv( ... 'SELECT ?item ?itemLabel WHERE { ... }', ... filepath="results.csv" ... )
Source code in gkc/sparql.py
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 | |
to_dataframe(query)
Execute a SPARQL query and return results as a pandas DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
SPARQL query string or Wikidata Query Service URL |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pandas DataFrame with query results |
Raises:
| Type | Description |
|---|---|
SPARQLError
|
If pandas is not installed or query fails |
Example
executor = SPARQLQuery() df = executor.to_dataframe( ... 'SELECT ?item ?itemLabel WHERE { ... }' ... ) print(df.head())
Source code in gkc/sparql.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
to_dict_list(query)
Execute a SPARQL query and return results as a list of dicts.
Each dict represents one result row, with variable names as keys and result values as values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
SPARQL query string or Wikidata Query Service URL |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, str]]
|
List of dictionaries |
Example
executor = SPARQLQuery() results = executor.to_dict_list( ... 'SELECT ?item ?itemLabel WHERE { ... }' ... ) for row in results: ... print(row)
Source code in gkc/sparql.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
ShExPropertyExtractor
Extracts property information from ShEx schema text.
Source code in gkc/mapping_builder.py
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
extract()
Extract all properties from ShEx schema with context.
Returns:
| Type | Description |
|---|---|
dict[str, dict]
|
Dictionary mapping property IDs to their context information |
Source code in gkc/mapping_builder.py
57 58 59 60 61 62 63 64 65 66 | |
ShExValidationError
Bases: Exception
Raised when ShEx validation encounters an error.
Source code in gkc/shex.py
21 22 23 24 | |
ShExValidator
Validate RDF data against Shape Expression (ShEx) schemas.
This class provides a flexible interface for validating RDF data against ShEx schemas, supporting multiple input sources for both RDF and schemas.
Example
Validate a Wikidata item against an EntitySchema
validator = ShExValidator(qid='Q42', eid='E502') result = validator.validate() print(result.results)
Use local schema file
validator = ShExValidator( ... qid='Q42', ... schema_file='schema.shex' ... ) validator.validate()
Use RDF text directly
validator = ShExValidator( ... rdf_text=my_rdf_data, ... schema_text=my_schema ... ) validator.validate()
Source code in gkc/shex.py
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
__init__(qid=None, eid=None, user_agent=None, schema_text=None, schema_file=None, rdf_text=None, rdf_file=None)
Initialize the ShEx validator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qid
|
Optional[str]
|
Wikidata entity ID (e.g., 'Q42'). Optional if rdf_text or rdf_file provided. |
None
|
eid
|
Optional[str]
|
EntitySchema ID (e.g., 'E502'). Optional if schema_text or schema_file provided. |
None
|
user_agent
|
Optional[str]
|
Custom user agent for Wikidata requests. |
None
|
schema_text
|
Optional[str]
|
ShExC schema as a string (alternative to eid). |
None
|
schema_file
|
Optional[str]
|
Path to file containing ShExC schema (alternative to eid). |
None
|
rdf_text
|
Optional[str]
|
RDF data as a string (alternative to qid). |
None
|
rdf_file
|
Optional[str]
|
Path to file containing RDF data (alternative to qid). |
None
|
Source code in gkc/shex.py
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 83 84 85 86 87 88 89 90 | |
__repr__()
String representation of validator.
Source code in gkc/shex.py
261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
evaluate()
Evaluate RDF data against the ShEx schema.
Must call load_schema() and load_rdf() first, or use validate().
Returns:
| Type | Description |
|---|---|
ShExValidator
|
Self with results populated |
Raises:
| Type | Description |
|---|---|
ShExValidationError
|
If evaluation fails or data not loaded |
Source code in gkc/shex.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
is_valid()
Check if validation passed.
Returns:
| Type | Description |
|---|---|
bool
|
True if validation passed, False otherwise |
Raises:
| Type | Description |
|---|---|
ShExValidationError
|
If validate() hasn't been called yet |
Source code in gkc/shex.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
load_rdf()
Load RDF data from configured source.
Tries sources in order: rdf_text, rdf_file, qid (from Wikidata).
Returns:
| Type | Description |
|---|---|
ShExValidator
|
Self for method chaining |
Raises:
| Type | Description |
|---|---|
ShExValidationError
|
If no valid RDF source or loading fails |
Source code in gkc/shex.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
load_schema()
Load the ShEx schema from configured source.
Tries sources in order: schema_text, schema_file, eid (from Wikidata).
Returns:
| Type | Description |
|---|---|
ShExValidator
|
Self for method chaining |
Raises:
| Type | Description |
|---|---|
ShExValidationError
|
If no valid schema source or loading fails |
Source code in gkc/shex.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
validate()
Convenience method: load schema, load RDF, and evaluate in one call.
Returns:
| Type | Description |
|---|---|
ShExValidator
|
Self with results populated |
Example
validator = ShExValidator(qid='Q42', eid='E502') validator.validate() if validator.results: ... print("Validation passed!")
Source code in gkc/shex.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
SitelinkValidator
Validates Wikipedia and Wikimedia project sitelinks.
Source code in gkc/sitelinks.py
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
__init__(user_agent=DEFAULT_USER_AGENT, timeout=10)
Initialize the sitelink validator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_agent
|
str
|
User agent string for API requests |
DEFAULT_USER_AGENT
|
timeout
|
int
|
Timeout in seconds for API requests |
10
|
Source code in gkc/sitelinks.py
40 41 42 43 44 45 46 47 48 49 50 51 | |
check_page_exists(title, site_code, allow_redirects=False)
Check if a Wikipedia/Wikimedia page exists and optionally check for redirects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
Page title to check |
required |
site_code
|
str
|
Site code (e.g., 'enwiki', 'commonswiki') |
required |
allow_redirects
|
bool
|
If False, return False for redirect pages |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
Tuple of (exists: bool, message: Optional[str]) |
Optional[str]
|
|
tuple[bool, Optional[str]]
|
|
Source code in gkc/sitelinks.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
filter_valid_sitelinks(sitelinks, verbose=False)
Filter out invalid sitelinks, returning only valid ones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sitelinks
|
dict[str, dict]
|
Dictionary of sitelinks to validate |
required |
verbose
|
bool
|
If True, print validation results |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, dict]
|
Filtered dictionary containing only valid sitelinks |
Source code in gkc/sitelinks.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
validate_sitelinks(sitelinks, delay_between_checks=0.1)
Validate multiple sitelinks at once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sitelinks
|
dict[str, dict]
|
Dictionary of sitelinks from transform_to_wikidata() Format: {"enwiki": {"site": "enwiki", "title": "...", "badges": []}} |
required |
delay_between_checks
|
float
|
Delay in seconds between API requests (rate limiting) |
0.1
|
Returns:
| Type | Description |
|---|---|
dict[str, tuple[bool, Optional[str]]]
|
Dictionary mapping site codes to (valid: bool, message: Optional[str]) |
Example
validator = SitelinkValidator() sitelinks = { ... "enwiki": {"site": "enwiki", "title": "Example", "badges": []}, ... "frwiki": {"site": "frwiki", "title": "Exemple", "badges": []} ... } results = validator.validate_sitelinks(sitelinks) results { "enwiki": (True, None), "frwiki": (False, "Page does not exist") }
Source code in gkc/sitelinks.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
WikidataFetchError
Bases: Exception
Raised when fetching data from Wikidata fails.
Source code in gkc/wd.py
15 16 17 18 | |
WikidataPropertyFetcher
Fetches property metadata from Wikidata API.
Source code in gkc/mapping_builder.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 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 202 203 204 205 206 | |
fetch_properties(property_ids)
Fetch metadata for multiple properties from Wikidata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
property_ids
|
list[str]
|
List of property IDs (e.g., ['P31', 'P571']) |
required |
Returns:
| Type | Description |
|---|---|
dict[str, PropertyInfo]
|
Dictionary mapping property IDs to PropertyInfo objects |
Source code in gkc/mapping_builder.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
WikiverseAuth
Bases: AuthBase
Authentication for Wikimedia projects (Wikidata, Wikipedia, Wikimedia Commons).
Designed for bot accounts using bot passwords. The same credentials work across all Wikimedia projects due to Single User Login (SUL).
Supports both default Wikimedia instances and custom MediaWiki installations.
Credentials can be provided in three ways (in order of precedence): 1. Direct parameters 2. Environment variables (WIKIVERSE_USERNAME, WIKIVERSE_PASSWORD, WIKIVERSE_API_URL) 3. Interactive prompt
Example
Authenticate to Wikidata (default)
auth = WikiverseAuth() auth.login()
Direct parameters (bot password format)
auth = WikiverseAuth( ... username="MyUsername@MyBot", ... password="abc123def456ghi789", ... api_url="https://www.wikidata.org/w/api.php" ... ) auth.login()
Custom MediaWiki instance
auth = WikiverseAuth( ... username="MyUsername@MyBot", ... password="abc123def456ghi789", ... api_url="https://my-wiki.example.com/w/api.php" ... ) auth.login()
Use authenticated session for API requests
response = auth.session.get(auth.api_url, params={ ... "action": "query", ... "format": "json" ... })
Source code in gkc/auth.py
61 62 63 64 65 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 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 | |
__init__(username=None, password=None, api_url=None, interactive=False)
Initialize Wikiverse authentication for bot accounts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
username
|
Optional[str]
|
Bot password username in format "Username@BotName". If not provided, reads from WIKIVERSE_USERNAME environment variable. |
None
|
password
|
Optional[str]
|
Bot password. If not provided, reads from WIKIVERSE_PASSWORD environment variable. |
None
|
api_url
|
Optional[str]
|
MediaWiki API endpoint URL. If not provided, reads from WIKIVERSE_API_URL environment variable, or defaults to Wikidata. Can also use shortcuts: "wikidata", "wikipedia", "commons" |
None
|
interactive
|
bool
|
If True and credentials are not found, prompt user for input. |
False
|
Source code in gkc/auth.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
get_account_name()
Extract account name from username.
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Account name if username is in bot password format, None otherwise. |
Example
auth = WikiverseAuth(username="Alice@MyBot") auth.get_account_name() 'Alice'
Source code in gkc/auth.py
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | |
get_bot_name()
Extract bot name from username.
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Bot name if username is in bot password format, None otherwise. |
Example
auth = WikiverseAuth(username="Alice@MyBot") auth.get_bot_name() 'MyBot'
Source code in gkc/auth.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | |
get_csrf_token()
Get a CSRF token for making edits.
Returns:
| Type | Description |
|---|---|
str
|
CSRF token string. |
Raises:
| Type | Description |
|---|---|
AuthenticationError
|
If not logged in or token retrieval fails. |
Example
auth = WikiverseAuth(username="User@Bot", password="secret") auth.login() token = auth.get_csrf_token()
Use token for edits
Source code in gkc/auth.py
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 | |
is_logged_in()
Check if currently logged in to MediaWiki API.
Returns:
| Type | Description |
|---|---|
bool
|
True if logged in, False otherwise. |
Source code in gkc/auth.py
239 240 241 242 243 244 245 246 | |
login()
Perform login to MediaWiki API using bot password credentials.
Returns:
| Type | Description |
|---|---|
bool
|
True if login successful, False otherwise. |
Raises:
| Type | Description |
|---|---|
AuthenticationError
|
If login fails with detailed error message. |
Example
auth = WikiverseAuth(username="User@Bot", password="secret") if auth.login(): ... print("Successfully logged in!")
Source code in gkc/auth.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
logout()
Logout from MediaWiki API and clear session.
Example
auth = WikiverseAuth(username="User@Bot", password="secret") auth.login()
... do some work ...
auth.logout()
Source code in gkc/auth.py
248 249 250 251 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 | |
check_wikipedia_page(title, site_code='enwiki', allow_redirects=False)
Convenience function to check if a Wikipedia page exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
Page title to check |
required |
site_code
|
str
|
Wikipedia site code (default: "enwiki" for English Wikipedia) |
'enwiki'
|
allow_redirects
|
bool
|
If False, reject redirect pages |
False
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The title if page exists and is valid, None otherwise |
Example
check_wikipedia_page("Python (programming language)") 'Python (programming language)' check_wikipedia_page("NonexistentPage123") None
Source code in gkc/sitelinks.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
execute_sparql(query, endpoint=DEFAULT_WIKIDATA_ENDPOINT, format='json')
Convenience function to execute a single SPARQL query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
SPARQL query string or Wikidata Query Service URL |
required |
endpoint
|
str
|
SPARQL endpoint (default: Wikidata) |
DEFAULT_WIKIDATA_ENDPOINT
|
format
|
str
|
Response format ('json', 'xml', 'csv', 'tsv') |
'json'
|
Returns:
| Type | Description |
|---|---|
Any
|
Query results |
Example
results = execute_sparql( ... 'SELECT ?item ?itemLabel WHERE { ... }' ... )
Source code in gkc/sparql.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | |
execute_sparql_to_dataframe(query, endpoint=DEFAULT_WIKIDATA_ENDPOINT)
Convenience function to execute a SPARQL query and return DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
SPARQL query string or Wikidata Query Service URL |
required |
endpoint
|
str
|
SPARQL endpoint (default: Wikidata) |
DEFAULT_WIKIDATA_ENDPOINT
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pandas DataFrame with query results |
Example
df = execute_sparql_to_dataframe( ... 'SELECT ?item ?itemLabel WHERE { ... }' ... )
Source code in gkc/sparql.py
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
fetch_entity_rdf(qid, format='ttl', user_agent=None)
Fetch RDF data for a Wikidata entity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qid
|
str
|
Wikidata entity ID (e.g., 'Q42', 'P31') |
required |
format
|
str
|
RDF format - 'ttl' (Turtle), 'rdf' (RDF/XML), 'nt' (N-Triples) |
'ttl'
|
user_agent
|
Optional[str]
|
Custom user agent string |
None
|
Returns:
| Type | Description |
|---|---|
str
|
RDF data as string |
Raises:
| Type | Description |
|---|---|
WikidataFetchError
|
If fetch fails |
Example
rdf = fetch_entity_rdf('Q42') # Get Douglas Adams RDF rdf = fetch_entity_rdf('P31', format='nt') # Get property in N-Triples
Source code in gkc/wd.py
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 | |
fetch_entity_schema(eid, user_agent=None)
Fetch ShExC schema text for a Wikidata EntitySchema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eid
|
str
|
EntitySchema ID (e.g., 'E502') |
required |
user_agent
|
Optional[str]
|
Custom user agent string |
None
|
Returns:
| Type | Description |
|---|---|
str
|
ShExC schema text as string |
Raises:
| Type | Description |
|---|---|
WikidataFetchError
|
If fetch fails |
Example
schema = fetch_entity_schema('E502') # Schema for organisms
Source code in gkc/wd.py
63 64 65 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 | |
validate_sitelink_dict(sitelinks)
Convenience function to validate and filter sitelinks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sitelinks
|
dict[str, dict]
|
Dictionary of sitelinks from transform_to_wikidata() |
required |
Returns:
| Type | Description |
|---|---|
dict[str, dict]
|
Filtered dictionary containing only valid sitelinks |
Example
sitelinks = { ... "enwiki": {"site": "enwiki", "title": "Example", "badges": []}, ... "frwiki": {"site": "frwiki", "title": "BadPage", "badges": []} ... } valid = validate_sitelink_dict(sitelinks)
Returns only valid sitelinks
Source code in gkc/sitelinks.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |