import csv
import requests
# Replace these with your actual credentials and API endpoint
API_TOKEN = 'API-Toekn'
API_URL = 'https://mysite.atlassian.net/wiki/rest/api/space'
HEADERS = {
'Authorization': f'Bearer {API_TOKEN}',
'Content-Type': 'application/json'
}
def get_spaces():
spaces = []
next_page = API_URL
try:
while next_page:
response = requests.get(next_page, headers=HEADERS)
response.raise_for_status() # Raise an exception for HTTP errors
data = response.json()
# Check if 'results' key exists in the response
if 'results' in data:
for space in data['results']:
spaces.append({
'key': space['key'],
'name': space['name'],
'total_pages': 0
})
next_page = data.get('next')
else:
# If 'results' key is not found, print the response for debugging
print("No 'results' key found in response:")
print(data)
# Break the loop to avoid infinite loop
break
except requests.exceptions.HTTPError as e:
print(f"HTTP error occurred: {e}")
# Return an empty list of spaces to indicate failure
return []
except Exception as e:
print(f"An error occurred: {e}")
# Return None to indicate failure
return None
return spaces
def get_space_pages(space_key):
total_pages = 0
next_page = f'{API_URL}/{space_key}/content'
while next_page:
response = requests.get(next_page, headers=HEADERS)
data = response.json()
# Decide which pages to count (e.g., pages with a certain label)
for page in data['results']:
# Check if 'desired_label' is not numeric
if not page['metadata'].get('desired_label', '').isdigit():
# Increment total_pages
total_pages += 1
next_page = data.get('next')
return total_pages
def main():
spaces = get_spaces()
if spaces is None:
print("Failed to retrieve spaces. Exiting...")
return
for space in spaces:
space['total_pages'] = get_space_pages(space['key'])
print(f"Space Key: {space['key']}, Space Name: {space['name']}, TotalPagesPerSpace: {space['total_pages']}")
if __name__ == '__main__':
main()
I have a site admin permission can any one please help
Typo in the API Token?
API_TOKEN = 'API-Toekn'
'Authorization': f'Bearer {API_TOKEN}',
Not really but i was able to fix it. instead f'Bearer {API_TOKEN}', i have used f'basic {API_TOKEN}', Thank you for your reply
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.