Skip to content
Raydo Matthee edited this page May 23, 2024 · 5 revisions

Fetching and Displaying IBM Badges from Credly

Introduction

This tutorial explains how to fetch IBM-specific badges from Credly, filter them based on IBM courses, and generate .html and .md files. These files will be committed to a GitHub repository.

Prerequisites

  • Python 3.x
  • requests library
  • A GitHub repository to store the output files
  • An IBM course feed JSON file
  • Credly API authorization token

Steps

1. Clone the GitHub Repository

Clone your GitHub repository to your local machine.

git clone https://github.com/skunkworksza/credly.git
cd credly

2. Install Necessary Dependencies

Install the requests library.

pip install requests

3. Fetch IBM Course Data

Download the IBM course feed JSON and save it as ibm_course_feed.json.

# fetch_ibm_courses.py
import json

def fetch_ibm_courses():
    with open('ibm_course_feed.json', 'r') as file:
        return json.load(file)['courses']

4. Authenticate and Fetch Credly Data

Use your Credly authorization token to fetch badges data. Here are the steps for authentication:

  1. Create the Authorization Header:

    • Your Credly authorization token will be used as the HTTP username with a blank password.
    • The username and password should be base64 encoded.
  2. Example Python Code for Authentication:

# fetch_badges.py
import base64
import requests

API_URL = "https://api.credly.com/v1/badges"
AUTH_TOKEN = "YOUR_AUTH_TOKEN"

def fetch_badges():
    headers = {
        "Accept": "application/json",
        "Authorization": f"Basic {base64.b64encode(f'{AUTH_TOKEN}:'.encode()).decode()}",
        "Content-Type": "application/json"
    }
    response = requests.get(API_URL, headers=headers)
    response.raise_for_status()
    return response.json()

Replace YOUR_AUTH_TOKEN with your actual authorization token.

5. Filter Badges to Match IBM Courses

Filter the fetched badges to match the IBM courses.

# filter_badges.py
def filter_ibm_badges(badges, courses):
    ibm_badges = []
    course_names = {course['title'] for course in courses}
    for badge in badges['data']:
        if badge['name'] in course_names:
            ibm_badges.append(badge)
    return ibm_badges

6. Generate HTML and Markdown Content

Create functions to generate HTML and Markdown content from the filtered badges.

# generate_content.py
def generate_html(badges):
    html_content = """
    <html>
    <head>
        <title>IBM Credly Badges</title>
        <style>
            body { font-family: Arial, sans-serif; margin: 20px; }
            h1 { color: #0033A0; }
            h2 { color: #0033A0; }
            .badge { border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; }
            .badge img { max-width: 100px; vertical-align: middle; }
            .badge p { display: inline-block; margin-left: 20px; vertical-align: middle; }
        </style>
    </head>
    <body>
        <h1>IBM Credly Badges</h1>
        <table>
            <tr>
                <th>Badge</th>
                <th>Description</th>
            </tr>
    """
    for badge in badges:
        html_content += f"""
        <tr class="badge">
            <td><img src="{badge['image_url']}" alt="{badge['name']}"><p>{badge['name']}</p></td>
            <td>{badge['description']}</td>
        </tr>
        """
    html_content += """
        </table>
    </body>
    </html>
    """
    return html_content

def generate_markdown(badges):
    md_content = "# IBM Credly Badges\n"
    for badge in badges:
        md_content += f"## {badge['name']}\n![{badge['name']}]({badge['image_url']})\n{badge['description']}\n"
    return md_content

7. Save Content to Files

Save the generated HTML and Markdown content to files.

# save_files.py
import os

def save_files(html_content, md_content, repo_path):
    with open(os.path.join(repo_path, "badges.html"), "w") as html_file:
        html_file.write(html_content)
    with open(os.path.join(repo_path, "badges.md"), "w") as md_file:
        md_file.write(md_content)

8. Commit and Push Changes to GitHub

Commit the generated files to your GitHub repository.

# commit_and_push.py
import os

def commit_and_push(repo_path):
    os.system(f"cd {repo_path} && git add badges.html badges.md && git commit -m 'Add IBM badges info and images' && git push")

9. Main Script

Combine all steps into a main script.

# main.py
from fetch_ibm_courses import fetch_ibm_courses
from fetch_badges import fetch_badges
from filter_badges import filter_ibm_badges
from generate_content import generate_html, generate_markdown
from save_files import save_files
from commit_and_push import commit_and_push

def main():
    courses = fetch_ibm_courses()
    badges = fetch_badges()
    ibm_badges = filter_ibm_badges(badges, courses)
    html_content = generate_html(ibm_badges)
    md_content = generate_markdown(ibm_badges)
    repo_path = "/path/to/your/local/repo"
    save_files(html_content, md_content, repo_path)
    commit_and_push(repo_path)

if __name__ == "__main__":
    main()

Replace /path/to/your/local/repo and YOUR_AUTH_TOKEN with your actual values.

Conclusion

This tutorial guides you through fetching IBM badges from Credly, filtering them, and generating HTML and Markdown files, which are then committed to a GitHub repository.