-
Notifications
You must be signed in to change notification settings - Fork 0
Home
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.
- Python 3.x
-
requests
library - A GitHub repository to store the output files
- An IBM course feed JSON file
- Credly API authorization token
Clone your GitHub repository to your local machine.
git clone https://github.com/skunkworksza/credly.git
cd credly
Install the requests
library.
pip install requests
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']
Use your Credly authorization token to fetch badges data. Here are the steps for authentication:
-
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.
-
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.
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
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
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)
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")
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.
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.
- Disclaimer:** This tutorial is for educational purposes only. Ensure you follow best practices for handling and storing sensitive data like API tokens.
Connect with us on social media: - [Twitter](https://twitter.com/skunkworksza) - [LinkedIn](https://www.linkedin.com/company/skunkworksza) - [Facebook](https://www.facebook.com/skunkworksza)