forked from kaminetzky/grade-uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrappers.py
62 lines (51 loc) · 2.13 KB
/
wrappers.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
import gspread
import requests
from oauth2client.service_account import ServiceAccountCredentials
class Github:
def __init__(self, user, repo, token):
self.user = user
self.repo = repo
self.auth = (user, token)
self.api_url = 'https://api.github.com/repos/{}/{}'.format(
self.user, self.repo)
self.raw_url = 'https://raw.githubusercontent.com/{}/{}'.format(
self.user, self.repo)
def get_file_text(self, path):
url = '{}/contents/{}'.format(self.api_url, path)
response = requests.get(url, auth=self.auth)
response_json = response.json()
download_url = response_json['download_url']
download_response = requests.get(download_url, auth=self.auth)
return download_response.text
def get_file(self, path):
url = '{}/contents/{}'.format(self.api_url, path)
response = requests.get(url, auth=self.auth)
response_json = response.json()
return response_json
def get_directories(self, path):
response_json = self.get_file(path)
if isinstance(response_json, dict):
raise ValueError('Path entered is not a directory')
directories = [
directory['name'] for directory in response_json
if '.' not in directory['name']
]
return directories
def download_text(self, path, branch='master'):
url = '{}/{}/{}'.format(self.raw_url, branch, path)
response = requests.get(url, auth=self.auth)
return response.text
class Google:
def __init__(self, key_path):
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name(
key_path, scope)
self.gc = gspread.authorize(credentials)
def get_worksheet(self, spreadsheet_name, index):
return self.gc.open(spreadsheet_name).get_worksheet(index)
@staticmethod
def update_cells(worksheet, data):
cells = [worksheet.cell(*element[0]) for element in data]
for i in range(len(data)):
cells[i].value = data[i][1]
worksheet.update_cells(cells)