File tree Expand file tree Collapse file tree 3 files changed +87
-11
lines changed Expand file tree Collapse file tree 3 files changed +87
-11
lines changed Original file line number Diff line number Diff line change 2929from . import siteviews
3030from . import wikidata
3131from . import wikidata_deleted
32+ from . import wikidata_novalue_snak
3233
3334SILENT_FLAG = True
3435SKIP_FLAG = ['imageinfo' , 'labels' ]
@@ -998,6 +999,12 @@ def test_wikidata_deleted(self):
998999
9991000 self .assertTrue ('requests' not in page .data )
10001001
1002+ def test_wikidata_no_value_snak (self ):
1003+ page = wptools .wikidata (skip = SKIP_FLAG , silent = SILENT_FLAG )
1004+ page .cache = {'wikidata' : wikidata_novalue_snak .cache }
1005+ page ._set_data ('wikidata' )
1006+ self .assertEqual (len (page .data ['claims' ]), 2 )
1007+
10011008
10021009class WPToolsToolTestCase (unittest .TestCase ):
10031010
Original file line number Diff line number Diff line change 1+ # -*- coding:utf-8 -*-
2+
3+ query = 'https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&formatversion=2&ids=Q483718&languages=en&props=info|claims|descriptions|labels|sitelinks&redirects=yes&sites=&titles='
4+
5+ response = r"""{
6+ "entities": {
7+ "Q483718": {
8+ "pageid": 455294,
9+ "ns": 0,
10+ "title": "Q483718",
11+ "lastrevid": 610328494,
12+ "modified": "2017-12-17T18:21:13Z",
13+ "type": "item",
14+ "id": "Q483718",
15+ "labels": {
16+ "en": {
17+ "language": "en",
18+ "value": "Foo Fighters"
19+ }
20+ },
21+ "descriptions": {
22+ "en": {
23+ "language": "en",
24+ "value": "American rock band, formed in Seattle in 1994"
25+ }
26+ },
27+ "claims": {
28+ "P576": [
29+ {
30+ "mainsnak": {
31+ "snaktype": "novalue",
32+ "property": "P576",
33+ "hash": "6a18d40010201ff9031e85a3fe98f1f0dc1d7cc4",
34+ "datatype": "time"
35+ },
36+ "type": "statement",
37+ "id": "Q483718$9da5583a-45e8-4268-2770-41d999bd53da",
38+ "rank": "normal"
39+ }
40+ ],
41+ "P373": [
42+ {
43+ "mainsnak": {
44+ "snaktype": "value",
45+ "property": "P373",
46+ "hash": "4d08e8ef6597a712d926b71c1c3b52d51d99a1a9",
47+ "datavalue": {
48+ "value": "Foo Fighters",
49+ "type": "string"
50+ },
51+ "datatype": "string"
52+ },
53+ "type": "statement",
54+ "id": "q483718$6CA37AA3-178A-41CF-B219-46F9BA68CC03",
55+ "rank": "normal"
56+ }
57+ ]
58+ },
59+ "sitelinks": {}
60+ }
61+ },
62+ "success": 1
63+ }"""
64+
65+ cache = {'query' : query , 'response' : response }
Original file line number Diff line number Diff line change @@ -355,26 +355,30 @@ def reduce_claims(query_claims):
355355 """
356356 claims = collections .defaultdict (list )
357357
358- for claim in query_claims :
358+ for claim , entities in query_claims . items () :
359359
360- for ent in query_claims . get ( claim ) :
360+ for ent in entities :
361361
362362 try :
363- snak = ent .get ('mainsnak' ).get ('datavalue' ).get ('value' )
363+ snak = ent .get ('mainsnak' )
364+ snaktype = snak .get ('snaktype' )
365+ value = snak .get ('datavalue' ).get ('value' )
364366 except AttributeError :
365367 claims [claim ] = []
366368
367369 try :
368- if snak .get ('id' ):
369- val = snak .get ('id' )
370- elif snak .get ('text' ):
371- val = snak .get ('text' )
372- elif snak .get ('time' ):
373- val = snak .get ('time' )
370+ if snaktype != 'value' :
371+ val = snaktype
372+ elif value .get ('id' ):
373+ val = value .get ('id' )
374+ elif value .get ('text' ):
375+ val = value .get ('text' )
376+ elif value .get ('time' ):
377+ val = value .get ('time' )
374378 else :
375- val = snak
379+ val = value
376380 except AttributeError :
377- val = snak
381+ val = value
378382
379383 if not val or not [x for x in val if x ]:
380384 raise ValueError ("%s %s" % (claim , ent ))
You can’t perform that action at this time.
0 commit comments