Skip to content

Instantly share code, notes, and snippets.

@arraytools
Created February 9, 2025 20:15
Show Gist options
  • Save arraytools/f71507e3867eea513fb3742fd1f1e9c2 to your computer and use it in GitHub Desktop.
Save arraytools/f71507e3867eea513fb3742fd1f1e9c2 to your computer and use it in GitHub Desktop.
Return the name and the size of each bitbucket repositories. Step 1: Change username and app_password, Step 2: Run "python3 bitbucket_download.py". The app_password can be created from Avatar -> Personal settings from the dropdown menu on RHS. Navigate to App Passwords and then Access management -> App passwords.
import requests
from requests.auth import HTTPBasicAuth
username = "USERNAME"
app_password = "API_PASSWORD"
base_url = f"https://api.bitbucket.org/2.0/repositories/{username}"
def fetch_repositories(url):
response = requests.get(url, auth=HTTPBasicAuth(username, app_password))
response.raise_for_status() # Raises an error for bad status codes
return response.json()
def main():
repositories = []
total_size_bytes = 0
next_url = base_url
while next_url:
data = fetch_repositories(next_url)
repositories.extend(data.get("values", []))
next_url = data.get("next") # Get the URL for the next page, if any
# Sort repositories by size in descending order
sorted_repos = sorted(repositories, key=lambda repo: repo["size"], reverse=True)
for repo in sorted_repos:
repo_name = repo["name"]
repo_size_bytes = repo["size"]
repo_size_mb = repo_size_bytes / (1024 * 1024) # Convert bytes to megabytes
total_size_bytes += repo_size_bytes
print(f"Repository: {repo_name}, Size: {repo_size_mb:.2f} MB")
# Calculate total size in megabytes
total_size_mb = total_size_bytes / (1024 * 1024)
print(f"\nTotal size of all repositories: {total_size_mb:.2f} MB")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment