4
4
Author: Cody Winkler (twitter: c2thewinkler | github: m0rph-1)
5
5
Date: 2/12/2020
6
6
"""
7
+ import os
7
8
import time
8
9
import requests
9
10
import json
10
11
import sys
12
+ from dotenv import load_dotenv
11
13
from termcolor import colored
12
14
from base64 import b64decode
13
15
from indicators import *
@@ -27,9 +29,13 @@ def parse_options():
27
29
args = parser .parse_args ()
28
30
return args
29
31
30
- def get_code_snippet (git_url_key ):
32
+ def get_code_snippet (git_url_key , authorization ):
33
+ authorization
34
+ headers = {
35
+ "Authorization" :"Bearer {authorization}"
36
+ }
31
37
32
- this_req = requests .get (git_url_key , verify = True )
38
+ this_req = requests .get (git_url_key , verify = True , headers = headers )
33
39
json_data = json .loads (this_req .content )
34
40
if this_req .status_code == 200 :
35
41
repo_code = str (b64decode (json_data ["content" ]).decode ('utf-8' )).split ('\n ' )
@@ -39,14 +45,25 @@ def get_code_snippet(git_url_key):
39
45
print (str (repo_code [i ]))
40
46
41
47
def check_rate_limit ():
42
-
43
- this_req = requests .get ("https://api.github.com/rate_limit" , verify = True )
48
+ this_req = requests .get ("https://api.github.com/rate_limit" , verify = True , headers = headers )
44
49
json_data = json .loads (this_req .content )
45
50
pprint (json_data ["rate" ])
46
51
return
47
52
48
53
def main (args ):
49
54
55
+ load_dotenv ()
56
+
57
+ api_key = os .getenv ("API_KEY" )
58
+
59
+ if api_key == None or api_key == '' :
60
+ print (colored ("[!] API key is not set. Specify it in .env." , "red" ))
61
+ exit (1 )
62
+
63
+ headers = {
64
+ "Authorization" : f"Bearer { api_key } "
65
+ }
66
+
50
67
separator = "-" * 100
51
68
52
69
if args .rate :
@@ -60,7 +77,7 @@ def main(args):
60
77
try :
61
78
62
79
github_api = ("https://api.github.com/search/code?q=user:{} {}" ).format (args .org , args .query )
63
- this_req = requests .get (github_api , verify = True )
80
+ this_req = requests .get (github_api , verify = True , headers = headers )
64
81
json_data = json .loads (this_req .content )
65
82
66
83
if this_req .status_code == 200 :
@@ -78,7 +95,7 @@ def main(args):
78
95
79
96
print (colored ("This should be printed if -g is specified" , "yellow" ))
80
97
git_url_key = key ["git_url" ]
81
- get_code_snippet (git_url_key )
98
+ get_code_snippet (git_url_key , )
82
99
83
100
except KeyError :
84
101
print (colored ("[-] Didn't find anything. Moving on!" , "red" ))
0 commit comments