-
Notifications
You must be signed in to change notification settings - Fork 1
/
github.py
129 lines (119 loc) · 4.09 KB
/
github.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
129
import os
import time
from datetime import datetime
from github import Github
from . import *
GIT_TEMP_DIR = "./userbot/temp/"
@bot.on(d3vil_cmd(pattern=r"commit"))
@bot.on(sudo_cmd(pattern=r"commit"))
async def download(event):
if event.fwd_from:
return
if Config.GITHUB_ACCESS_TOKEN is None:
await eod(event, "`Please ADD Proper Access Token from github.com`")
return
if Config.GIT_REPO_NAME is None:
await eod(event, "`Please ADD Proper Github Repo Name of D3vilBot`")
return
d3vilbot = await eor(event, "Processing ...")
if not os.path.isdir(GIT_TEMP_DIR):
os.makedirs(GIT_TEMP_DIR)
start = datetime.now()
reply_message = await event.get_reply_message()
try:
time.time()
print("Downloading to TEMP directory")
downloaded_file_name = await bot.download_media(
reply_message.media, GIT_TEMP_DIR
)
except Exception as e:
await eod(d3vilbot, str(e))
else:
end = datetime.now()
ms = (end - start).seconds
await event.delete()
await d3vilbot.edit(
"Downloaded to `{}` in {} seconds.".format(downloaded_file_name, ms)
)
await d3vilbot.edit("Committing to Github....")
await git_commit(downloaded_file_name, d3vilbot)
async def git_commit(file_name, d3vilbot):
content_list = []
access_token = Config.GITHUB_ACCESS_TOKEN
g = Github(access_token)
file = open(file_name, "r", encoding="utf-8")
commit_data = file.read()
repo = g.get_repo(Config.GIT_REPO_NAME)
print(repo.name)
create_file = True
contents = repo.get_contents("")
for content_file in contents:
content_list.append(str(content_file))
print(content_file)
for i in content_list:
create_file = True
if i == 'ContentFile(path="' + file_name + '")':
return await hellbot.edit("`File Already Exists`")
create_file = False
file_name = "d3vilbot/plugins/" + file_name
if create_file == True:
file_name = file_name.replace("./userbot/temp/", "")
print(file_name)
try:
repo.create_file(
file_name, "Uploaded New Plugin", commit_data, branch="master"
)
print("Committed File")
ccess = Config.GIT_REPO_NAME
ccess = ccess.strip()
await d3vilbot.edit(
f"`Commited On Your Github Repo`\n\n[Your STDPLUGINS](https://github.com/{ccess}/tree/master/userbot/plugins/)"
)
except:
print("Cannot Create Plugin")
await eod(d3vilbot, "Cannot Upload Plugin")
else:
return await eod(d3vilbot, "`Committed Suicide`")
@bot.on(d3vil_cmd(pattern="github (.*)", outgoing=True))
@bot.on(sudo_cmd(pattern="github (.*)", allow_sudo=True))
async def _(event):
if event.fwd_from:
return
input_str = event.pattern_match.group(1)
url = "https://api.github.com/users/{}".format(input_str)
r = requests.get(url)
if r.status_code != 404:
b = r.json()
avatar_url = b["avatar_url"]
html_url = b["html_url"]
gh_type = b["type"]
name = b["name"]
company = b["company"]
blog = b["blog"]
location = b["location"]
bio = b["bio"]
created_at = b["created_at"]
await bot.send_file(
event.chat_id,
caption="""Name: [{}]({})
Type: {}
Company: {}
Blog: {}
Location: {}
Bio: {}
Profile Created: {}""".format(
name, html_url, gh_type, company, blog, location, bio, created_at
),
file=avatar_url,
force_document=False,
allow_cache=False,
reply_to=event,
)
await event.delete()
else:
await eor(event, "`{}`: {}".format(input_str, r.text))
CmdHelp("github").add_command(
"commit", "<reply to a file>", "Uploads the file on github repo as provided in Heroku Config GIT_REPO_NAME. In short makes a commit to git repo from Userbot"
).add_command(
"github", "<git username>", "Fetches the details of the given git username"
).add()