1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
import requests
from django.conf import settings
from django.shortcuts import render
from django.utils import timezone
from django.views import View
from pydis_site.apps.main.models import RepositoryMetadata
GITHUB_API = "https://api.github.com/users/python-discord/repos"
class HomeView(View):
@staticmethod
def _get_api_data():
"""Call the GitHub API and get information about our repos."""
repo_dict = {repo_name: {} for repo_name in settings.HOMEPAGE_REPOS}
# Fetch the data from the GitHub API
api_data = requests.get(GITHUB_API)
api_data = api_data.json()
# Process the API data into our dict
for repo in api_data:
full_name = repo["full_name"]
if full_name in settings.HOMEPAGE_REPOS:
repo_dict[full_name] = {
"full_name": repo["full_name"],
"description": repo["description"],
"language": repo["language"],
"forks_count": repo["forks_count"],
"stargazers_count": repo["stargazers_count"],
}
return repo_dict
def _get_repo_data(self):
"""Build a list of RepositoryMetadata objects that we can use to populate the front page."""
# Try to get site data from the cache
try:
repo_data = RepositoryMetadata.objects.get(repo_name="python-discord/site")
# If the data is older than 2 minutes, we should refresh it.
if (timezone.now() - repo_data.last_updated).seconds > 120:
# Get new data from API
api_data_container = self._get_api_data()
repo_data_container = []
# Update or create all RepoData objects in settings.HOMEPAGE_REPOS
for repo_name, api_data in api_data_container.items():
try:
repo_data = RepositoryMetadata.objects.get(repo_name=repo_name)
repo_data.description = api_data["description"]
repo_data.language = api_data["language"]
repo_data.forks = api_data["forks_count"]
repo_data.stargazers = api_data["stargazers_count"]
except RepositoryMetadata.DoesNotExist:
repo_data = RepositoryMetadata(
repo_name=api_data["full_name"],
description=api_data["description"],
forks=api_data["forks_count"],
stargazers=api_data["stargazers_count"],
language=api_data["language"],
)
repo_data.save()
repo_data_container.append(repo_data)
return repo_data_container
# Otherwise, if the data is fresher than 2 minutes old, we should just return it.
else:
return RepositoryMetadata.objects.all()
# If this is raised, the database has no repodata at all, we will create them all.
except RepositoryMetadata.DoesNotExist:
# Get new data from API
api_data_container = self._get_api_data()
repo_data_container = []
# Create all the repodata records in the database.
for api_data in api_data_container.values():
repo_data = RepositoryMetadata(
repo_name=api_data["full_name"],
description=api_data["description"],
forks=api_data["forks_count"],
stargazers=api_data["stargazers_count"],
language=api_data["language"],
)
repo_data.save()
repo_data_container.append(repo_data)
return repo_data_container
def get(self, request):
"""Collect repo data and render the homepage view"""
repo_data = self._get_repo_data()
return render(request, "home/index.html", {"repo_data": repo_data})
|