-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetJSON.py
More file actions
44 lines (34 loc) · 1.55 KB
/
GetJSON.py
File metadata and controls
44 lines (34 loc) · 1.55 KB
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
from CodeData import Utils
from LanguagesAndReadme import GetLanguagesAndReadme
from CodeData import GetImports
from CodeData import GetIdentifiers
from CodeData import GetDocstrings
import json
import os
DEFAULT_GIT_INFO = {'stargazers_count': '0', 'commit_sha': '0', 'repo_id': '0'}
def get_json(url: str, git_info: dict = DEFAULT_GIT_INFO):
"""
:param path: 1) path to file ( ~ doesn't work with os ) or 2) url to github repository
:param as_url: False (default) - path is treated as 1), True - path is treated as 2)
:param git_info: Meta info about repository
:return: json dump
"""
if url.endswith(".git"):
url = url[:-4]
name = url.split("/")[-1].strip('\n')
owner = url.split("/")[-2]
repo_name = owner + '_' + name
path = os.path.abspath(os.getcwd()) + "/repos/" + repo_name
Utils.download_repo(owner, name, path)
if not os.path.exists(path):
raise FileNotFoundError(path, "is incorrect path")
languages, percentages, readme = GetLanguagesAndReadme.get_languages_and_readme(path, 20)
imports = GetImports.get_imports(path)
identifiers, splitted_identifiers = GetIdentifiers.get_identifiers(path)
docstrings = GetDocstrings.get_docstrings(path)
Utils.remove_dir(path)
data = {"owner": owner, "name": name, "readme": readme, "languages": languages,
"percentages": percentages, "imports": imports, "identifiers": identifiers,
"splitted_identifiers": splitted_identifiers, "docstrings": docstrings}
data.update(git_info)
return json.dumps(data)