-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
128 lines (101 loc) · 3.34 KB
/
main.py
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import argparse
import urllib.request
import json as js
from pathlib import Path
from datetime import datetime
# Handle Arguments
# --------------------------
parser = argparse.ArgumentParser()
parser.add_argument(
"--lua", action="store_const", const=1, help="save file as lua file"
)
parser.add_argument(
"--json", action="store_const", const=1, help="save file as json file"
)
parser.add_argument(
"--out", "-o", type=str, default="commits", help="file name you want to save as"
)
parser.add_argument(
"--rname",
"-r",
type=bool,
default=True,
help="True/False, change '.' and '-' to '_' in plugin names ex: plugin.nivm -> plugin_nvim",
)
parser.add_argument(
"--urlfile",
"-u",
type=str,
default="./urls/put_urls_here.url",
help="file containing urls",
)
lua = parser.parse_args().lua
json = parser.parse_args().json
rname = parser.parse_args().rname
output = parser.parse_args().out
urls_file = parser.parse_args().urlfile
# --------------------------
def make_api(filename, branch="main"):
api_urls = []
with open(filename) as url_list:
urls = url_list.read().splitlines()
for url in urls:
# ignore comment line
if url.split("#")[0] == "":
continue
if len(url.split(" ")) > 1:
branch = url.split("/")[-1].split(" ")[-1]
url = url.split("/")
plugin_name = url[-1].split(" ")[0]
api = f"https://api.github.com/repos/{url[-2]}/{plugin_name}/branches/{branch}"
api_urls.append(api)
return api_urls
def get_plugin_name(api_url):
return api_url.split("/")[-3]
def get_commit(url):
data = urllib.request.urlopen(url).read()
commit = js.loads(data)["commit"]["sha"]
return commit
def save_as(filetype, commits):
"""example: filetype="json" commits = "{key: value}" """
if rname:
repair_commits = {}
# fix file naming
for plugin_name, commit in commits.items():
plugin_name = plugin_name.replace("-", "_")
plugin_name = plugin_name.replace(".", "_")
repair_commits[plugin_name] = commit
commits = repair_commits
current_date = datetime.today().strftime("%Y-%m-%d_%T")
# save as lua
if filetype == "lua":
stamp = f"-- Generated by: https://github.com/pullape/LCommit\n-- on\t{current_date}\n"
filename = f"./output/{output}.lua"
with open(filename, "w") as file:
file.write(stamp+ "\nreturn {")
for plugin_name, commit in commits.items():
with open(filename, "a") as file:
file.write(f"\n\t{plugin_name} = '{commit}',")
with open(filename, "a") as file:
file.write("\n}\n")
# save as json
if filetype == "json":
filename = f"./output/{output}.json"
with open(filename, "w") as file:
file.write(js.dumps(commits, sort_keys=True, indent=4))
def main():
# create dir to save our result
save_location = "./output"
if not Path(save_location).exists():
Path(save_location).mkdir(parents=True)
commits = {}
for url in make_api(urls_file):
commits[get_plugin_name(url)] = get_commit(url)
if lua:
save_as("lua", commits)
if json:
save_as("json", commits)
if not lua and not json:
save_as("json", commits)
if __name__ == "__main__":
main()