Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Violeta-Tejera committed Dec 25, 2023
0 parents commit 6bddb0b
Show file tree
Hide file tree
Showing 18 changed files with 907 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

Empty file added README.md
Empty file.
Empty file added api/__init__.py
Empty file.
Binary file added api/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file added api/__pycache__/repo.cpython-311.pyc
Binary file not shown.
Binary file added api/__pycache__/user.cpython-311.pyc
Binary file not shown.
10 changes: 10 additions & 0 deletions api/repo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from github import Github

def get_statistics_repo(github_instance, repo, year): # commits, pull requests, issues,...
pass

def get_descr_repo(github_instance, repo, year):
pass

def get_contributors_repo(github_instance, repo, year):
pass
67 changes: 67 additions & 0 deletions api/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from github import Github, Repository
from utils.github_helpers import get_github_languages
from datetime import datetime
from collections import defaultdict

def get_created_repos(github_instance: Github, username: str, year: int, showPrivate=False):
"""
Returns a list of the repos created by a user in a certain year. You can toggle
whether or not you want to display the private repositories.
"""

if showPrivate == True:
user = github_instance.get_user() # authenticated user can get private repos too
user_repos = user.get_repos(visibility='all')
else:
user = github_instance.get_user(username)
user_repos = user.get_repos()

repos = [r for r in user_repos if r.fork is False and r.created_at.year == year and r.owner == github_instance.get_user(username)]

return repos

def get_contributed_repos(github_instance: Github, username: str, year: int):
"""
Returns a list of the repos contributed to by a user in a certain year.
"""

user = github_instance.get_user(username)
repos = []

repos += get_created_repos(github_instance, username, year)

for event in user.get_events():
if event.type == "PushEvent" and event.created_at.year == year:
repo = event.repo
if repo not in repos:
repos.append(repo)

return repos

def get_languages_user(github_instance: Github, username: str, year: int, showPrivate=False):
"""
Returns a dictionary that maps every language (with at least 1 line of code) with the
quantity of lines of code written on year "year" by user "username".
"""
user = github_instance.get_user(username)
try:
github_languages = get_github_languages()
except:
print("Error: Unable to get languages. Aborting now")

language_stats = defaultdict(int)

date_since = datetime(year, 1, 1, 0, 0, 0)
date_until = datetime(year + 1, 1, 1, 0, 0, 0)

for repo in user.get_repos():
for commit in repo.get_commits(since=date_since, until=date_until, author=user):
for file in commit.files:
extension = '.' + file.filename.split('.')[-1]
language = github_languages.get(extension, 'Unknown')
language_stats[language] += file.changes

return dict(sorted(language_stats.items(), key=lambda x:x[1], reverse=True))

def get_statistics_user(github_instance: Github, username, year):
pass
6 changes: 6 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"username": "Violeta-Tejera",
"token": "YOUR TOKEN HERE",
"year": 2023,
"showPrivate": true
}
7 changes: 7 additions & 0 deletions funciones
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
repositorios:

get_collaborators
get_contributors
get_stargazers

atributos de repo:
67 changes: 67 additions & 0 deletions githubwrapped.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from utils.helpers import *
from api.repo import *
from api.user import *

def repositories_details(github, username, year, showPrivate):
print("------------------------------------------------")
print("Repositories and code")
print("------------------------------------------------")
repos = get_created_repos(github, username, year, showPrivate=showPrivate)
print("Repositories created this year: ")
for r in repos:
print(r.full_name, " Top Language: ", r.language)
print("------------------------------------------------")
repos = get_contributed_repos(github, username, year)
print("Repositories contributed to this year: ")
for r in repos:
print(r.full_name, " Top Language: ", r.language)
print("------------------------------------------------")
languages = get_languages_user(github, username, year)
print(f"You coded in more than {len(languages)} languages this year")
languages_list = list(languages.items())
if languages_list[0][0] != 'Unknown' or len(languages_list) == 0:
top_language = languages_list[0]
else:
top_language = languages_list[1]
print(f"But your favourite was without a doubt {top_language[0]}")

def commits_details(github, username, year, showPrivate):
### Commits
print("Commits")

# Num. of commits of the year

# Max streak of commits (dates)

# Max streak of days without commits (dates)

# Histogram of commits per month

def social_details(github, username, year, showPrivate):
### Social
print("Social")
# Stars (Given and received)

# Top 2 collaborators

# Num de nuevos seguidores

# Num de nuevas cuentas seguidas

def main():
filename = "config.json"
jsonfile = load_json(filename)

username = jsonfile.get('username')
token = jsonfile.get('token')
year = jsonfile.get('year')
showPrivate = jsonfile.get('showPrivate')

github = connect(token)

if github:
repositories_details(github, username, year, showPrivate)

disconnect(github)

main()
Empty file added utils/__init__.py
Empty file.
Binary file added utils/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file added utils/__pycache__/github_helpers.cpython-311.pyc
Binary file not shown.
Binary file added utils/__pycache__/helpers.cpython-311.pyc
Binary file not shown.
34 changes: 34 additions & 0 deletions utils/github_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import requests
import yaml
from collections import defaultdict

def get_github_languages():
"""
Returns a dictionary that maps each extension to the name of a language known to Github.
"""
url = 'https://raw.githubusercontent.com/github/linguist/master/lib/linguist/languages.yml'
response = requests.get(url)

if response.status_code == 200:
data = response.text
else:
raise Exception("Unable to get github languages info")

github_languages = yaml.safe_load(data)

extension_mapping = defaultdict(str)

for language, properties in github_languages.items():
extensions = properties.get('extensions', '')
type = properties.get("type", "")

if type == "programming":
if isinstance(extensions, str):
extensions_list = extensions.split()
elif isinstance(extensions, list):
extensions_list = extensions

for extension in extensions_list:
extension_mapping[extension] = language

return extension_mapping
40 changes: 40 additions & 0 deletions utils/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from github import Github, Auth
import json

def connect(token:str):
"""
Autentifies with the Github API using the token
"""
try:
auth = Auth.Token(token)
g = Github(auth=auth)

username = g.get_user()
print(f"You have been connected to Github as user {username.login}")
return g
except Exception as e:
print(f"Error autentifying Github: {e}")
return None

def disconnect(github:Github):
"""
Closes github connection
"""
if github:
print("Disconnecting")
github.close()

def load_json(filepath:str):
"""
Returns, if successful, a Python object containing a decoded JSON document
"""
try:
with open(filepath, 'r') as file:
config = json.load(file)
return config
except FileNotFoundError:
print(f"Error: Unable to find path in {filepath}")
return None
except json.JSONDecodeError:
print(f"Error: Unable to decode the content of file in {filepath}")
return None

0 comments on commit 6bddb0b

Please sign in to comment.