Created
January 2, 2020 06:55
-
-
Save jossef/ce3b856cf1d151183d6b2452f819bb12 to your computer and use it in GitHub Desktop.
compare AWS and Twilio SMS prices
This file contains hidden or 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
| #!/usr/bin/env python3 | |
| from collections import defaultdict | |
| import csv | |
| import concurrent.futures | |
| import numpy | |
| import requests | |
| import pycountry | |
| def get_aws_sms_prices(): | |
| r = requests.get('https://s3.amazonaws.com/aws-sms-pricing-info-prod-us-east-1/smsPricesAndDeliverability-latest.json') | |
| r.raise_for_status() | |
| data = r.json() | |
| result = dict() | |
| for country_code, cellular_providers in data.items(): | |
| rates_map = defaultdict(list) | |
| for cellular_provider in cellular_providers: | |
| name = cellular_provider['name'] | |
| rates_map[name].append(cellular_provider['transPrice']) | |
| rates = rates_map.values() | |
| rates = map(numpy.mean, rates) | |
| rates = list(rates) | |
| result[country_code] = numpy.mean(rates) | |
| return result | |
| def get_twilio_sms_prices(): | |
| r = requests.get('https://www.twilio.com/pricing/csv/messaging') | |
| r.raise_for_status() | |
| lines = r.text.splitlines() | |
| csv_reader = csv.reader(lines, delimiter=',') | |
| next(csv_reader, None) # skip the headers | |
| result = dict() | |
| countries_map = defaultdict(lambda: defaultdict(list)) | |
| for row in csv_reader: | |
| name = row[2] | |
| rates_map = countries_map[row[0]] | |
| rates_map[name].append(float(row[3])) | |
| for country_code, rates_map in countries_map.items(): | |
| rates = rates_map.values() | |
| rates = map(numpy.mean, rates) | |
| rates = list(rates) | |
| result[country_code] = numpy.mean(rates) | |
| return result | |
| def main(): | |
| with concurrent.futures.ThreadPoolExecutor() as executor: | |
| aws_prices_executor = executor.submit(get_aws_sms_prices) | |
| twilio_prices_executor = executor.submit(get_twilio_sms_prices) | |
| aws_prices = aws_prices_executor.result() | |
| twilio_prices = twilio_prices_executor.result() | |
| for country in pycountry.countries: | |
| country_code = country.alpha_2 | |
| print(f'{country_code}\t{country.name}\t{twilio_prices.get(country_code, "")}\t{aws_prices.get(country_code, "")}') | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment