Skip to content

Commit 5e9536b

Browse files
author
scottstraughan
committed
Add script used to generate new project files.
1 parent dc82abf commit 5e9536b

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

scripts/get_github_latest_projects.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import os
2+
import urllib.request
3+
import json
4+
from datetime import datetime
5+
6+
from pathlib import Path
7+
8+
import yaml
9+
from markdownfeeds.MarkdownFile import MarkdownFile
10+
from slugify import slugify
11+
12+
projects_path = os.path.join(os.getcwd(), '../content/projects')
13+
url = 'https://api.github.com/search/repositories?q=topic:sycl&per_page=100'
14+
15+
16+
def create_project_file(repo):
17+
date = datetime.fromisoformat(repo['created_at'])
18+
title = slugify(repo['name'])
19+
file_path = os.path.join(
20+
projects_path,
21+
str(date.year),
22+
f'{date.year}-{date.strftime("%m")}-{date.strftime("%d")}-{title}.md')
23+
24+
if not os.path.exists(os.path.dirname(file_path)):
25+
os.makedirs(os.path.dirname(file_path))
26+
27+
license = 'Unspecified'
28+
29+
if 'license' in repo and repo['license'] is not None and 'name' in repo['license']:
30+
license = repo['license']['name']
31+
32+
with open(file_path, mode='w') as file:
33+
front_matter = {
34+
'date': datetime.today().strftime('%Y-%m-%dT%H:%M:%S'),
35+
'creation_date': repo['created_at'],
36+
'title': repo['name'],
37+
'license': license,
38+
'contributor': 'anonymous',
39+
'external_url': repo['html_url'],
40+
'tags': repo['topics']
41+
}
42+
43+
contents = '---\n'
44+
contents += yaml.dump(front_matter)
45+
contents += '---\n\n'
46+
contents += repo['description'] if repo['description'] is not None else repo['name']
47+
contents += '\n'
48+
49+
print(f'Wrote new project file to {file_path}')
50+
file.write(contents)
51+
52+
53+
# Fetch and load JSON data
54+
page = 1
55+
total_repos_to_fetch = 0
56+
repos = []
57+
58+
while len(repos) < total_repos_to_fetch or total_repos_to_fetch == 0:
59+
with urllib.request.urlopen(url + f'&page={page}') as response:
60+
result = json.loads(response.read().decode())
61+
total_repos_to_fetch = result['total_count']
62+
repos = repos + result['items']
63+
page += 1
64+
65+
# Get all the project URLs we currently have
66+
project_urls = []
67+
for markdown_file in list(Path(projects_path).rglob('*.md')):
68+
contents = MarkdownFile.load(str(markdown_file))
69+
project_urls.append(str(contents.front_matter.get('external_url')).lower())
70+
71+
# Loop all the repos we have found from API and ensure we have a project file for them
72+
for repo in repos:
73+
needle = str(repo['html_url']).lower()
74+
75+
if needle not in project_urls:
76+
print(f'Project not found: ' + repo['html_url'])
77+
create_project_file(repo)

0 commit comments

Comments
 (0)