pythontwittermediawiki-apiwikidata-apiwikidata-query-service

Search for Q-ID (from Wikidata) with Twitter Username ID. (Python)


I have a list of verified Twitter User-IDs.

data['screen_name'] = [MOFAJapan_en, serenawilliams, JeffBezos ....]
data['twitter_ids'] =  [303735625, 26589987, 15506669 ....]

and I want to get their respective Q-IDs from Wikidata. For the above Twitter-username-IDs, it will look sort of like this:

q_id_list = [Q222241, Q11459, Q312556 ....]

I ran into a slight complication here: if you search for MOFAJapan_en or MOFA of Japan, Wikidata API cannot recognize it. However, MOFAJapan has a wikidata page.

example wikidata entity for Twitter ID

I know that the Property # for Twitter username is P2002, but how do I query for this without knowing the Q-ID?

Thank you in advance.


Solution

  • Given a list of Twitter names (inside VALUES), this SPARQL query will find the persons:

    SELECT ?twitterName ?person
    WHERE {
    
      VALUES ?twitterName {
        "MOFAJapan_en"
        "serenawilliams"
        "JeffBezos"
      }
    
      ?person wdt:P2002 ?twitterName .
      
    }
    

    It won’t find anything for MOFAJapan_en, as the correct value seems to be MofaJapan_en. To ignore case, you can use a FILTER with LCASE, but this will increase runtime performance:

    SELECT ?twitterName ?person
    WHERE {
    
      VALUES ?twitterName_anyCase {
        "MOFAJapan_en"
        "serenawilliams"
        "JeffBezos"
      }
      
      FILTER( LCASE(?twitterName_anyCase) = LCASE(?twitterName) ) .
    
      ?person wdt:P2002 ?twitterName .
      
    }