Last active
April 10, 2024 11:45
-
-
Save alexwlchan/0acdb92d2e94a1c47d54fcef5b3e1fe9 to your computer and use it in GitHub Desktop.
Flickypedia bot help question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import httpx | |
import keyring | |
# 1. Retrieve the Wikimedia API token from the system keychain | |
api_token = keyring.get_password("flickypedia_backfillr_bot", "api_token") | |
# 2. Create an HTTP client | |
client = httpx.Client( | |
headers={ | |
"Authorization": f"Bearer {api_token}", | |
"User-Agent": "FlickypediaBackfillrBot <flickypedia@flickr.org>", | |
} | |
) | |
# 3. Fetch a CSRF token | |
csrf_resp = client.get( | |
"https://commons.wikimedia.org/w/api.php", | |
params={"action": "query", "meta": "tokens", "type": "csrf", "format": "json"}, | |
) | |
csrf_token = csrf_resp.json()["query"]["tokens"]["csrftoken"] | |
# 4. Send an edit request to send to the Commons API. | |
data = { | |
"action": "wbeditentity", | |
"bot": "1", | |
"data": json.dumps( | |
{ | |
"claims": [ | |
# Add the Flickr Photo ID property (P12120) | |
{ | |
"mainsnak": { | |
"datavalue": {"value": "46181673152", "type": "string"}, | |
"property": "P12120", | |
"snaktype": "value", | |
}, | |
"type": "statement", | |
}, | |
] | |
} | |
), | |
"format": "json", | |
"maxlag": "2", | |
"site": "commonswiki", | |
"summary": "Bot adding [[Commons:Structured data|SDC]] based on metadata from Flickr", | |
"tags": "BotSDC", | |
"title": 'File:"Be Not Afraid of Going Slowly, Be Afraid of Standing Still" (46181673152).jpg', | |
"token": csrf_token, | |
} | |
edit_resp = client.post("https://commons.wikimedia.org/w/api.php", data=data) | |
print(edit_resp) | |
print(edit_resp.json()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Response [200 OK]> | |
{'entity': {'type': 'mediainfo', 'id': 'M124358413', 'labels': {}, 'descriptions': {}, 'statements': {'P1163': [{'mainsnak': {'snaktype': 'value', 'property': 'P1163', 'hash': '723d30b878d4a8deb968b0db429888e5353661a2', 'datavalue': {'value': 'image/jpeg', 'type': 'string'}}, 'type': 'statement', 'id': 'M124358413$0EA96754-84A2-4E2E-87A4-CE5CC65706A4', 'rank': 'normal'}], 'P6216': [{'mainsnak': {'snaktype': 'value', 'property': 'P6216', 'hash': '5570347fdc76d2a80732f51ea10ee4b144a084e0', 'datavalue': {'value': {'entity-type': 'item', 'numeric-id': 50423863, 'id': 'Q50423863'}, 'type': 'wikibase-entityid'}}, 'type': 'statement', 'id': 'M124358413$75A0F52E-E307-41A8-9CE4-57E8C45C7787', 'rank': 'normal'}], 'P275': [{'mainsnak': {'snaktype': 'value', 'property': 'P275', 'hash': '82d5b402aa0a79040483a8c9264bd484c6c13f67', 'datavalue': {'value': {'entity-type': 'item', 'numeric-id': 19125117, 'id': 'Q19125117'}, 'type': 'wikibase-entityid'}}, 'type': 'statement', 'id': 'M124358413$D3B1AAF8-30AF-4A7B-B096-CAE195D03F70', 'rank': 'normal'}], 'P7482': [{'mainsnak': {'snaktype': 'value', 'property': 'P7482', 'hash': 'b82dd47222a210c6dc1f428b693c2fbf7b0e0d6e', 'datavalue': {'value': {'entity-type': 'item', 'numeric-id': 74228490, 'id': 'Q74228490'}, 'type': 'wikibase-entityid'}}, 'type': 'statement', 'qualifiers': {'P137': [{'snaktype': 'value', 'property': 'P137', 'hash': '3d8cd4c1ab91d3d9a14900210faaebf1a98b947b', 'datavalue': {'value': {'entity-type': 'item', 'numeric-id': 103204, 'id': 'Q103204'}, 'type': 'wikibase-entityid'}}], 'P973': [{'snaktype': 'value', 'property': 'P973', 'hash': '3db31c0e8a4f73bfee28586b25bc8e2f8011bd47', 'datavalue': {'value': 'https://www.flickr.com/photos/geewhypics/46181673152', 'type': 'string'}}]}, 'qualifiers-order': ['P137', 'P973'], 'id': 'M124358413$E322B74B-C3A7-4CF6-BF74-57A9FB01F5A1', 'rank': 'normal'}], 'P4092': [{'mainsnak': {'snaktype': 'value', 'property': 'P4092', 'hash': '1f19e092e415934a279b35f1ad7fd2dc2c6cd8df', 'datavalue': {'value': 'd3b9a0ecb565870b08d6a67d2a09a77ca071f095', 'type': 'string'}}, 'type': 'statement', 'qualifiers': {'P459': [{'snaktype': 'value', 'property': 'P459', 'hash': '75dff03c151b13fbab93742164121c16a6aa0de1', 'datavalue': {'value': {'entity-type': 'item', 'numeric-id': 13414952, 'id': 'Q13414952'}, 'type': 'wikibase-entityid'}}]}, 'qualifiers-order': ['P459'], 'id': 'M124358413$7C5573E6-9618-4A07-8915-A90B72B5FBD2', 'rank': 'normal'}], 'P3575': [{'mainsnak': {'snaktype': 'value', 'property': 'P3575', 'hash': '4806801ea4001dc9d15e93913b091a8d8b55ff3d', 'datavalue': {'value': {'amount': '+18291835', 'unit': 'http://www.wikidata.org/entity/Q8799'}, 'type': 'quantity'}}, 'type': 'statement', 'id': 'M124358413$24A73C05-F5E0-4168-8A9E-74F869FDA726', 'rank': 'normal'}], 'P2048': [{'mainsnak': {'snaktype': 'value', 'property': 'P2048', 'hash': '300c470905f45c160e99be1547604fa83e267c50', 'datavalue': {'value': {'amount': '+3132', 'unit': 'http://www.wikidata.org/entity/Q355198'}, 'type': 'quantity'}}, 'type': 'statement', 'id': 'M124358413$A8D4FC9C-DDD4-4B88-B8AF-476946EB1E0B', 'rank': 'normal'}], 'P2049': [{'mainsnak': {'snaktype': 'value', 'property': 'P2049', 'hash': '04497bcc9c349831e289ca66e91e4f2a4648b0b0', 'datavalue': {'value': {'amount': '+5568', 'unit': 'http://www.wikidata.org/entity/Q355198'}, 'type': 'quantity'}}, 'type': 'statement', 'id': 'M124358413$E5BECA62-193E-49DF-BDA1-56065F22982E', 'rank': 'normal'}], 'P12120': [{'mainsnak': {'snaktype': 'value', 'property': 'P12120', 'hash': '545c870458c24451febfb6b0e4a3724051a82c30', 'datavalue': {'value': '46181673152', 'type': 'string'}}, 'type': 'statement', 'id': 'M124358413$0EC48430-E3CA-4B6D-AAA2-2D6FDAFE5955', 'rank': 'normal'}]}, 'lastrevid': 867203113}, 'success': 1} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's what the edit looks like in the global list of recent edits, notice the lack of b: