Rewrote the token handler to add manual input
This commit is contained in:
49
copilot.py
49
copilot.py
@@ -4,6 +4,7 @@ import os
|
||||
import sys
|
||||
import time
|
||||
from os.path import expanduser
|
||||
from AuthToken import AuthToken
|
||||
import click
|
||||
from rich import print
|
||||
from rich.panel import Panel
|
||||
@@ -14,50 +15,6 @@ try:
|
||||
except ImportError:
|
||||
import readline
|
||||
|
||||
|
||||
|
||||
# get a new github copilot token
|
||||
def get_token():
|
||||
|
||||
valid = False
|
||||
token = {}
|
||||
|
||||
# check if a token exists in the file
|
||||
if os.path.exists("token.json"):
|
||||
with open("token.json", "r") as f:
|
||||
token = json.load(f)
|
||||
|
||||
# check if the token is valid
|
||||
if "token" in token and "expires_at" in token:
|
||||
if token["expires_at"] > time.time():
|
||||
valid = True
|
||||
|
||||
if not valid:
|
||||
# read token from file
|
||||
|
||||
# read file from home directory
|
||||
home = expanduser("~")
|
||||
with open(f"{home}/.config/github-copilot/hosts.json", "r") as f:
|
||||
terms = json.load(f)
|
||||
auth_token = terms["github.com"]["oauth_token"]
|
||||
|
||||
|
||||
headers = {
|
||||
'Authorization': f"Bearer {auth_token}",
|
||||
}
|
||||
response = requests.get('https://api.github.com/copilot_internal/v2/token', headers=headers)
|
||||
|
||||
if response.status_code != 200:
|
||||
return None
|
||||
|
||||
token = response.json()
|
||||
|
||||
# save the token in a json file with the expiration time
|
||||
with open("token.json", "w") as f:
|
||||
json.dump(token, f)
|
||||
|
||||
return token["token"]
|
||||
|
||||
# get a suggestion using github copilot
|
||||
def get_suggestion(prompt, token):
|
||||
headers = {
|
||||
@@ -125,7 +82,9 @@ def main():
|
||||
def generate_suggestion(prompt):
|
||||
prompt = " ".join(prompt)
|
||||
prompt = "!/bin/bash\n\n" + prompt + ":\n"
|
||||
token = get_token()
|
||||
|
||||
authToken = AuthToken()
|
||||
token = authToken.get_github_api_token()
|
||||
|
||||
suggestion = get_suggestion(prompt, token)
|
||||
panel = Panel(Text(suggestion, justify="center"), title_align="center", title="Execute? \[y/n/e/c]", expand=False)
|
||||
|
||||
Reference in New Issue
Block a user