I have code in Python 3 where I fetch data from the GitHub API, and I want some advice on the architecture of this code.
import urllib.request, urllib.parse, urllib.error
import json
import sys
from collections import Counter
from datetime import datetime, timedelta, date
from tabulate import tabulate
from LanguagesRepoCounter import LanguagesCounter
from RepoListLanguages import RepoListLanguages
#getting the date of one mounth ago from today
One_Mouth_ago_date = datetime.date(datetime.now()) - timedelta(days=30)
url = 'https://api.github.com/search/repositories?q=created:%3E'+One_Mouth_ago_date.isoformat()+'&sort=stars&order=desc&page=1&per_page=100'
data_connection_request = urllib.request.urlopen(url)
unicoded_data = data_connection_request.read().decode()
try:
json_data = json.loads(unicoded_data)
except:
json_data = None
# testing retrieaved data
if not json_data or json_data["total_count"]<1:
print(json_data)
sys.exit('######## Failure To Retrieve ########')
#Counting the number of repo using every language
Lang_Dic_Count = LanguagesCounter(json_data)
# Getting the list of repo using every language
DicDataList = RepoListLanguages(json_data, Lang_Dic_Count)
# Displaye the repo's urls for every language
for key in DicDataList:
print(key, ' language used in')
for i in range(len(DicDataList[key])):
print(" ", DicDataList[key][i])
# Displaye the number of repos using every language in a table
table = []
for key in Lang_Dic_Count:
under_table=[]
under_table.append(key)
under_table.append(Lang_Dic_Count[key])
table.append(under_table)
print(tabulate(table, headers=('Language', 'number of Repo using it'), tablefmt="grid"))
and the first function is :
def LanguagesCounter(Data):
''' Count how many times every programming language is used in the 100 most starred repos created in the last 30 days given in a parameter as json object, return a dict with languages as a keys and number of repo using this language as a value
'''
Languages_liste = []
for i in range(len(Data["items"])):
Languages_liste.append(Data["items"][i]["language"])
Lang_Dic_Count ={i:Languages_liste.count(i) for i in Languages_liste}
return Lang_Dic_Count
the third function is:
def RepoListLanguages(Data, list):
'''
return the list of repositories from Data parameter for every programming language in the list parameter as a dictionary with the programming language as key and a list of repositories as value
'''
languagesRepo = {}
for key in list:
languagesRepo[key]= []
for repo in range(len(Data["items"])):
if (key == Data["items"][repo]["language"]):
languagesRepo[key].append(Data["items"][repo]["html_url"])
return languagesRepo
The code fetches the most starred repos from GitHub API that were created in the last 30 days, counts how many repos use every programming language, and lists the repo links for every language.
'''Python
will run. \$\endgroup\$