Skip to content

Instantly share code, notes, and snippets.

@t0mk
Last active January 20, 2020 15:40
Show Gist options
  • Save t0mk/510dbcd0cb195d71db0a64bdfa007183 to your computer and use it in GitHub Desktop.
Save t0mk/510dbcd0cb195d71db0a64bdfa007183 to your computer and use it in GitHub Desktop.
Prints data about Spot Maket prices in Packet API: facility, plan, price per hour, number of CPU, price per CPU hour
#!/usr/bin/env python3
import os
import sys
import requests
apiToken = os.environ["PACKET_API_TOKEN"]
assert(len(apiToken))
apiUrl = "https://api.packet.net"
headers = {"X-Auth-Token": apiToken}
print("Getting spot prices, it takes time...", file=sys.stderr)
r = requests.get(apiUrl + "/market/spot/prices", headers=headers)
spotPrices = r.json()
print("Getting plans, should be quick...", file=sys.stderr)
r = requests.get(apiUrl + "/plans", headers=headers)
plans = r.json()
class PlanNotFound(Exception):
pass
def getCpuCount(plan, plans):
p = [i for i in plans['plans'] if i['slug'] == plan]
if len(p) == 0:
raise PlanNotFound(plan)
c = p[0]['specs']['cpus'][0]['count']
return c
for fac, availplans in spotPrices['spot_market_prices'].items():
for plan, pri in availplans.items():
cpuCount = getCpuCount(plan, plans)
priNum = pri['price']
print(fac, plan, priNum, cpuCount, priNum/float(cpuCount))
# to sort the listing by price per spot CPU, you can pipe the stdout to `sort -k 5`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment