Usage examples

Sample report download script

The sample Python script below shows how you could use our airline data API to download the latest Pricing report. It's not meant to be used as-is, so be sure to adjust it to your specific needs.

import requests
import sys


class SelfieInterface(object):
    def __init__(self, airline, username, password):
        self.base_url = f"https://{airline}.useselfie.com/api/selfie/v1"
        self.login_url = f"https://{airline}.useselfie.com/api/selfie/rest_auth/login/"
        self.username = username
        self.password = password
        self.cookies = None

    def login(self):
        resp = requests.post(
            self.login_url, json={"username": self.username, "password": self.password}
        )

        if resp.status_code == 200:
            self.cookies = resp.cookies
            print("logged in successfully")
        else:
            print(f"Failed to login - {resp.status_code}: {resp.text}")

    def get_available_pricing_weekly_reports(self):
        resp = requests.get(f"{self.base_url}/reports/pricing", cookies=self.cookies)
        reports = resp.json()
        return reports

    def download_dataset(self, dataset):
        filename = f"report_{dataset['fromDate']}_{dataset['toDate']}.zip"
        resp = requests.get(
            f"{self.base_url}/report/{dataset['id']}?decrypt=true",
            cookies=self.cookies,
        )

        if resp.status_code == 200:
            with open(filename, "wb") as outfile:
                outfile.write(resp.content)
                print(f"Report downloaded to {filename}")
        else:
            print(f"Failed to download report - {resp.status_code}: {resp.text}")


if __name__ == "__main__":
    if len(sys.argv) != 4:
        print("Usage: python <name_of_the_file>.py <airline> <username> <password")
        exit()

    _, airline, username, password = sys.argv
    selfie = SelfieInterface(airline, username, password)
    selfie.login()

    reports = selfie.get_available_pricing_weekly_reports()
    print(reports)

    selfie.download_dataset(reports[0])

Power BI integration script

The basic script below shows how you could integrate the data from our airline API into Power BI. Feel free to use it as a starting point and modify it as you see fit.

import io
import pandas as pd
import requests
import sys
import zipfile

SELFIE_URL = ""
SELFIE_USERNAME = ""
SELFIE_PASSWORD = ""

class SelfieInterface():
    def login(self, username, password):
        resp = requests.post(f"{SELFIE_URL}/rest_auth/login/", json={"username": username, "password": password})
        if resp.status_code == 200:
            self.cookies = resp.cookies
            print("logged in successfully")
            print(self.cookies)
        else:
            print(f"Failed to login - {resp.status_code}: {resp.text}")

    def get_available_pricing_weekly_reports(self):
        resp = requests.get(f"{SELFIE_URL}/v1/reports/pricing", cookies=self.cookies) 
        reports = resp.json()
        return reports

    def download_report(self, dataset):
        filename = f"reports_{dataset['fromDate']}_{dataset['toDate']}.zip"
        resp = requests.get(f"{SELFIE_URL}/v1/report/{dataset['id']}?decrypt=true", cookies=self.cookies) 
        if resp.status_code == 200:
            with zipfile.ZipFile(io.BytesIO(resp.content)) as z:
                csv_filename = z.namelist()[0]
                with z.open(csv_filename) as csv_file:
                    df = pd.read_csv(csv_file)
                    return df
        else:
            print(f"Failed to download report - {resp.status_code}: {resp.text}")


def main():
    iface = SelfieInterface()
    iface.login(SELFIE_USERNAME, SELFIE_PASSWORD)
    reports = iface.get_available_pricing_weekly_reports()

    df = iface.download_report(reports[0])
    return df


df = main()
print(df)
Xeneta Footer - Fixed