Skip to content

Instantly share code, notes, and snippets.

@vasgat
Last active September 20, 2024 17:56
Show Gist options
  • Save vasgat/aba64ca0286b3561ad9c26daadaa1303 to your computer and use it in GitHub Desktop.
Save vasgat/aba64ca0286b3561ad9c26daadaa1303 to your computer and use it in GitHub Desktop.
This script interacts with the WikiRate API to retrieve metrics and answers based on specific keywords and corporate identifiers, and then saves the gathered data to a CSV file.
from itertools import islice
from wikirate4py import API, Cursor, to_dataframe
# splits a list into chunks of a given size
def chunked_iterable(iterable, size):
iterable = iter(iterable)
return iter(lambda: list(islice(iterable, size)), [])
# gathers metrics based on keywords
def gather_metrics(api, keywords, per_page=100):
metrics = []
for keyword in keywords:
cursor = Cursor(api.get_metrics, metric_keyword=keyword, per_page=per_page)
while cursor.has_next():
metrics.extend(item.id for item in cursor.next())
return metrics
# gathers answers based on metric and corporate identifier chunks
def gather_answers(api, metrics, corporate_identifiers, chunk_size=100):
answers = []
metric_chunks = chunked_iterable(metrics, chunk_size)
corporate_chunks = chunked_iterable(corporate_identifiers, chunk_size)
for metric_chunk in metric_chunks:
for corporate_chunk in corporate_chunks:
cursor = Cursor(api.get_answers, metric=metric_chunk, company_identifier=corporate_chunk, per_page=100)
while cursor.has_next():
answers.extend(cursor.next())
return answers
api = API('your_wikirate_api_key')
keywords = ['climate', 'deforestation']
corporate_identifiers = ['AEDFXA0M6V00', 'AEDFXA14NUL7', 'AEE000301011', 'AEE000401019',
'AEN000101016', 'ARCTIO010019', 'ARENOR010020', 'ARP432631215',
'ARP689251337', 'ARP9897X1319', 'ARSAMA010012', 'AT0000743059',
'AU0000002339', 'AU000000AAC9', 'AU000000AMI1', 'AU000000ANZ3',
'AU000000ARG4', 'AU000000AUB9', 'AU000000BGA8', 'AU000000BLD2',
'AU000000CHC0', 'AU000000CMW8', 'AU000000CWY3', 'AU000000EVN4',
'AU000000GMG2', 'AU000000GNC9', 'AU000000GOR5', 'AU000000IMD5',
'AU000000ING6', 'AU000000JHX1', 'AU000000LLC3', 'AU000000MWY2',
'AU000000NGI6', 'AU000000NUF3', 'AU000000ORG5', 'AU000000PRU3',
'AU000000QUB5', 'AU000000RRL8', 'AU000000SEQ3', 'AU000000VG15',
'AU000000WOW2', 'AU000000WPL2', 'AU0000016875', 'AU0000018236',
'AU0000109662', 'AU0000186678', 'AU0000273229', 'AU0000336679',
'AU0000340770', 'AU3CB0305118', 'AU3FN0016077', 'AU3FN0076360',
'BE0003008019', 'BE0003604155', 'BE0021271284', 'BE0029825461',
'BE0055924527', 'BE0974338700', 'BE0974413453', 'BMG0R24BAA92',
'BMG169621056', 'BMG210901242', 'BMG276311039', 'BMG3922B1072',
'BMG491BT1088', 'BMG540501027', 'BMG5485F1692', 'BMG667211046',
'BMG7000R1088', 'BMG812761002', 'BMG9349W1038', 'BRBBASACNOR3',
'BRBBDCACNPR8', 'BRBEEFACNOR6', 'BRBRFSACNOR8', 'BRBRPRACNOR9',
'BRCCROACNOR2', 'BRCYREACNOR7', 'BRELETACNPB7', 'BREVENACNOR8',
'BRGOAUACNPR8', 'BRITUBACNPR1', 'BRJBSSACNOR8', 'BRKLBNACNPR9',
'BRLRENACNOR1', 'BRMDIAACNOR7', 'BRMRFGACNOR0', 'BRMRVEACNOR2',
'BRMULTACNOR5', 'BRNTCOACNOR5', 'BRPETRACNOR9', 'BRSANBACNPR5',
'BRSAPRACNPR6', 'BRSBSPACNOR5', 'BRSUZBACNOR0', 'BRUGPAACNOR8',
'BRVALEACNOR0', 'BRVEBMCDAM15', 'CA01626P4033', 'CA05534B7604',
'AIMCCA61XXX', 'AIMCCA66XXX', 'AU000000AEF4', 'AU000000ASX7',
'AU000000COH5', 'AU000000DOW2', 'AU000000IGO4', 'AU000000JRV4',
'AU000000MPL3', 'AU000000WOR2', 'AU000000WTC3', 'AU0000064966',
'AU0000250532', 'AU3CB0292977', 'AU3CB0298487', 'AU3CB0310100',
'AU3CB0310118', 'AU3CB0310373', 'AU3CB0310381', 'BE0003764785',
'BE0003765790', 'BE0024914823', 'BE0050811802', 'BE0944264663',
'BE6221795212', 'BE6227255492', 'BE6229290471', 'BE6349293611',
'BMG0585R1060', 'BMG0585R1144', 'BMG668971101', 'BMG677491539',
'CH0005345571', 'CH0011037469', 'CH0011404735', 'CH0012032048',
'CH0012032113', 'CH0025238863', 'CH0025751329', 'CH0126881561',
'CH0286491805', 'CH0479514314', 'CH1282125983', 'DE0005089031',
'DE0005200000', 'DE0005470405', 'DE0006202005', 'DE0006231004',
'DE0006766504', 'DE0007297004', 'DE0007500001', 'DE0007664005',
'DE0007664039', 'DE000A14J587', 'DE000A1HKQE8', 'DE000A1PHFF7',
'DE000A2TSMN4', 'DE000SL0E6T6', 'DE000SL0E7R8', 'DE000SL0FME0',
'DE000SLA2VW9', 'DE000SYM7720', 'DE000SYM9999', 'DK0010213628',
'DK0010253921', 'DK0010272632', 'DK0060050474', 'DK0060050557',
'DK0060094928', 'DK0060448595', 'DK0060542181', 'ES0112501012',
'ES0116870314', 'ES0116920333', 'ES0118594417', 'ES0127797019',
'ES0127797035', 'ES0130960018', 'ES0143416115', 'ES0148396007',
'ES0177542018', 'ES0305199004', 'ES05051138V2', 'ES05051138W0',
'ES0505199721', 'ES0S00001123', 'ES0SDIV00052', 'FR0000039109',
'FR0000130403', 'FR0004048007', 'FR0004053478', 'FR0004268563',
'FR0010452433', 'FR0010533075', 'FR0011129832', 'FR0011742329',
'FR0011950732', 'FR0013154002', 'FR0013420072', 'FR0014004L86'
]
metrics = gather_metrics(api, keywords)
# Gather answers and convert to dataframe
answers = gather_answers(api, metrics, corporate_identifiers)
df = to_dataframe(answers)
df.to_csv("./answers.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment