mirror of
https://github.com/JonasunderscoreJones/turbo-octo-potato.git
synced 2025-10-28 03:29:18 +01:00
Added features
- dont-download flag - dont-update-github flag - added timestamp
This commit is contained in:
parent
959b119501
commit
7fb15d92b8
1 changed files with 42 additions and 23 deletions
|
|
@ -1,11 +1,14 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
|
import time
|
||||||
import requests
|
import requests
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
VERBOSEPRINT = "-v" in sys.argv or "--verbose" in sys.argv
|
VERBOSEPRINT = "-v" in sys.argv or "--verbose" in sys.argv
|
||||||
|
DONTDOWNLOAD = "-dd" in sys.argv or "--dont-download" in sys.argv
|
||||||
|
DONTUPDATEGH = "-du" in sys.argv or "--dont-update-github" in sys.argv
|
||||||
|
|
||||||
def verboseprint(string="", end="\n"):
|
def verboseprint(string="", end="\n"):
|
||||||
if VERBOSEPRINT:
|
if VERBOSEPRINT:
|
||||||
|
|
@ -102,44 +105,60 @@ def get_languagages(repo, access_token):
|
||||||
projects_json_path = os.path.expanduser("~/.cache/gh-projects/projects.json")
|
projects_json_path = os.path.expanduser("~/.cache/gh-projects/projects.json")
|
||||||
# create the directory if it doesn't exist
|
# create the directory if it doesn't exist
|
||||||
os.makedirs(os.path.dirname(projects_json_path), exist_ok=True)
|
os.makedirs(os.path.dirname(projects_json_path), exist_ok=True)
|
||||||
# fetch the projects.json file from https://cdn.jonasjones.dev/api/projects/projects.json
|
|
||||||
projects_json_url = "https://cdn.jonasjones.dev/api/projects/projects.json"
|
|
||||||
projects_json = requests.get(projects_json_url)
|
|
||||||
with open(projects_json_path, "wb") as file:
|
|
||||||
file.write(projects_json.content)
|
|
||||||
|
|
||||||
verboseprint(f"Fetched projects.json file")
|
if not DONTDOWNLOAD:
|
||||||
|
# fetch the projects.json file from https://cdn.jonasjones.dev/api/projects/projects.json
|
||||||
|
projects_json_url = "https://cdn.jonasjones.dev/api/projects/projects.json"
|
||||||
|
projects_json = requests.get(projects_json_url)
|
||||||
|
with open(projects_json_path, "wb") as file:
|
||||||
|
file.write(projects_json.content)
|
||||||
|
|
||||||
|
verboseprint(f"Fetched projects.json file")
|
||||||
|
elif not os.path.exists("~/.cache/gh-projects/projects.json"):
|
||||||
|
FileNotFoundError("File 'projects.json' not found. Cannot proceed without \
|
||||||
|
downloading it. Remove '-dd' or '--dontdownload' from the launch arguments.")
|
||||||
|
else:
|
||||||
|
print("Skipping download of 'projects.json'")
|
||||||
|
|
||||||
# Load the existing projects.json file
|
# Load the existing projects.json file
|
||||||
with open(projects_json_path, "r") as file:
|
with open(projects_json_path, "r") as file:
|
||||||
projects_data = json.load(file)
|
projects_data = json.load(file)
|
||||||
|
|
||||||
print("Fetching Repo data...")
|
if not DONTUPDATEGH:
|
||||||
|
print("Fetching Repo data...")
|
||||||
|
|
||||||
# Update the last_update (Unix timestamp) for each project
|
# Update the last_update (Unix timestamp) for each project
|
||||||
for project in projects_data:
|
for project in projects_data:
|
||||||
gh_api = project.get("gh_api")
|
gh_api = project.get("gh_api")
|
||||||
if gh_api:
|
if gh_api:
|
||||||
last_commit_timestamp = get_last_commit_timestamp(gh_api, GITHUB_API_TOKEN)
|
last_commit_timestamp = get_last_commit_timestamp(gh_api, GITHUB_API_TOKEN)
|
||||||
last_release_version = get_last_release_version(gh_api, GITHUB_API_TOKEN)
|
last_release_version = get_last_release_version(gh_api, GITHUB_API_TOKEN)
|
||||||
if last_commit_timestamp:
|
if last_commit_timestamp:
|
||||||
project["last_update"] = last_commit_timestamp
|
project["last_update"] = last_commit_timestamp
|
||||||
else:
|
else:
|
||||||
project["last_update"] = 0
|
project["last_update"] = 0
|
||||||
if last_release_version:
|
if last_release_version:
|
||||||
project["version"] = last_release_version.replace("v", "")
|
project["version"] = last_release_version.replace("v", "")
|
||||||
languages = get_languagages(gh_api, GITHUB_API_TOKEN)
|
languages = get_languagages(gh_api, GITHUB_API_TOKEN)
|
||||||
if languages:
|
if languages:
|
||||||
project["languages"] = languages
|
project["languages"] = languages
|
||||||
|
else:
|
||||||
|
print("Skipping Github updates...")
|
||||||
|
|
||||||
|
# remove first element
|
||||||
|
projects_data.pop(0)
|
||||||
|
|
||||||
# sort projects alphabetically
|
# sort projects alphabetically
|
||||||
projects_data = sorted(projects_data, key=lambda x: x['last_update'], reverse=True)
|
projects_data = sorted(projects_data, key=lambda x: x['last_update'], reverse=True)
|
||||||
|
|
||||||
|
# add a first element to the list that holds the date of the last update
|
||||||
|
projects_data.insert(0, {"last_update": time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) + " UTC"})
|
||||||
|
|
||||||
# Save the updated data back to the projects.json file
|
# Save the updated data back to the projects.json file
|
||||||
with open(projects_json_path, "w") as file:
|
with open(projects_json_path, "w") as file:
|
||||||
json.dump(projects_data, file, indent=2)
|
json.dump(projects_data, file, indent=2)
|
||||||
|
|
||||||
print("Updated projects.json")
|
print("Updated projects.json\nUploading to cdn...")
|
||||||
|
|
||||||
os.system(f"rclone copy {projects_json_path} cdn:cdn/api/projects/")
|
os.system(f"rclone copy {projects_json_path} cdn:cdn/api/projects/")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue