Last active
July 5, 2017 14:01
-
-
Save cbirdsong/4166b6c79cbc7f92553aae109c8b50c1 to your computer and use it in GitHub Desktop.
Bitium export to CSV/1Password
This file contains 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 | |
import os, sys, stat, string | |
import json | |
import csv | |
import requests | |
# global utility variables | |
bitium_api_url = 'https://www.bitium.com/api/v2/organizations/' | |
current_directory = os.path.dirname(sys.argv[0]) | |
# load Bitium auth info from json | |
auth_info = open(current_directory + '/auth.json') | |
auth_info = json.load(auth_info) | |
org_id = auth_info.get('org_id') | |
token = auth_info.get('token') | |
unique_id = auth_info.get('unique_id') | |
# Set Bitium API authorization headers | |
auth_headers = {'Authorization': 'token ' + token, 'X-Device-Id': unique_id} | |
# Get list of items from Bitium | |
bitium = requests.Session() | |
subscriptions = bitium.get(bitium_api_url+org_id+'/subscriptions', headers=auth_headers) | |
subscriptions = json.loads(subscriptions.content) | |
# Set up CSV file | |
outputFile = open(current_directory + '/bitium-' + org_id + '.csv', 'w+', newline='') | |
outputWriter = csv.writer(outputFile) | |
outputWriter.writerow(['name', 'url', 'username', 'password', 'bitium app type', 'error']) | |
success_count = 0 | |
error_count = 0 | |
for item in subscriptions: | |
#get the name, URL and login type | |
name = item.get('name') | |
url = item.get('installation').get('configuration').get('url') | |
provider = item.get('installation').get('provider').get('name') | |
# prepend bitium app type to the name for cleaner 1password formatting | |
if name != provider: | |
name = name + ' - ' + provider | |
# get username and password using the internal Bitium ID | |
item_id = str(item.get('id')) | |
credentials = bitium.post(bitium_api_url+org_id+'/subscriptions/'+item_id+'/credentials', headers=auth_headers) | |
login = json.loads(credentials.content) | |
if credentials.reason != 'Created': | |
error = str(credentials.status_code) + ' ' + credentials.reason | |
error_count += 1 | |
else: | |
error = '' | |
success_count += 1 | |
username = login.get('login') | |
password = login.get('password') | |
# write to CSV | |
print('scraping: ' + name) | |
outputWriter.writerow([name, url, username, password, provider, error]) | |
print('Successes: ' + str(success_count)) | |
print('Errors: ' + str(error_count)) |
This file contains 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
{ | |
"org_id": "bitium.com", | |
"token": "8AR4YDvxLSzvcCFdVyZ_", | |
"unique_id": "f566018b-d443-4f74-a177-46ed092ec3b2" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment