refactoring, support google docs work with list, using image that store in git
parent
7233034920
commit
b608a41b55
|
@ -1,3 +1,3 @@
|
||||||
client_secret.json
|
client_secret.json
|
||||||
token.json
|
token.*
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
class bedc:
|
||||||
|
def __init__(self):
|
||||||
|
self.infoPrint = self._defaultPrint
|
||||||
|
self.savePath = './'
|
||||||
|
|
||||||
|
def _defaultPrint(self, text : str, progress: int):
|
||||||
|
print("{}% {}".format(progress, text))
|
||||||
|
|
||||||
|
def AttachProcessInfo(self, callback):
|
||||||
|
self.infoPrint = callback
|
||||||
|
|
||||||
|
def setProgress(self, i : int):
|
||||||
|
self.progress = i
|
||||||
|
|
||||||
|
def setSavePath(self, path):
|
||||||
|
self.savePath = path
|
||||||
|
|
||||||
|
def createOption(self, index, value, image=None):
|
||||||
|
self.infoPrint("creating option", self.progress)
|
||||||
|
|
||||||
|
def createQuestion(self, title, description, options, indexAnswer, itemImage=None):
|
||||||
|
self.infoPrint("creating create question", self.progress)
|
||||||
|
|
||||||
|
def submitItem(self):
|
||||||
|
self.infoPrint("submiting question", self.progress)
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
self.infoPrint("updating remote", self.progress)
|
|
@ -1,125 +1,50 @@
|
||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import os.path, time
|
import time, json
|
||||||
from google.auth.transport.requests import Request
|
from gservice import gservice
|
||||||
from google.oauth2.credentials import Credentials
|
|
||||||
from google_auth_oauthlib.flow import InstalledAppFlow
|
|
||||||
from googleapiclient.discovery import build
|
|
||||||
from googleapiclient.errors import HttpError
|
|
||||||
from pprint import pprint
|
|
||||||
import requests
|
|
||||||
from urllib.parse import urlparse
|
|
||||||
|
|
||||||
class gdoc:
|
class gdoc(gservice):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
'''
|
super(gdoc, self).__init__()
|
||||||
Args :
|
self.image_temp_service_url = "https://git.manakin-gentoo.ts.net/a2nr/boring_edu_doc/raw/branch/master"
|
||||||
templateId : get the id from link GoogleForm
|
# self.image_temp_service_url = "https://tmpfiles.org/api/v1/upload"
|
||||||
'''
|
# self.image_temp_service_url = "https://tmpfiles.org/api/v1/upload"
|
||||||
self.image_temp_service_url = "https://tmpfiles.org/api/v1/upload"
|
|
||||||
# self.image_temp_service_url = "https://uguu.se/upload.php"
|
# self.image_temp_service_url = "https://uguu.se/upload.php"
|
||||||
self.submition = {"requests":[]}
|
self.submition = {"requests":[]}
|
||||||
self.docs_service = None
|
|
||||||
self.main_docs = None
|
self.main_docs = None
|
||||||
self.infoPrint = self._defaultPrint
|
|
||||||
self.progress = -1
|
self.progress = -1
|
||||||
self.resultUri = ""
|
self.resultUri = ""
|
||||||
self.savePath = './'
|
|
||||||
self.questionKey = []
|
self.questionKey = []
|
||||||
|
|
||||||
def setProgress(self, i : int):
|
def generateService(self, name):
|
||||||
self.progress = i
|
|
||||||
|
|
||||||
def setSavePath(self, path):
|
|
||||||
self.savePath = path
|
|
||||||
|
|
||||||
def AttachProcessInfo(self, callback):
|
|
||||||
self.infoPrint = callback
|
|
||||||
|
|
||||||
def _defaultPrint(self, text : str, progress: int):
|
|
||||||
print("{}% {}".format(progress, text))
|
|
||||||
|
|
||||||
def generateService(self):
|
|
||||||
''' Start Tokenizing
|
|
||||||
here is the way to get token
|
|
||||||
link : https://developers.google.com/docs/api/quickstart/python
|
|
||||||
'''
|
|
||||||
SCOPES = ["https://www.googleapis.com/auth/documents",]
|
SCOPES = ["https://www.googleapis.com/auth/documents",]
|
||||||
|
self.generateToken('docs', SCOPES)
|
||||||
|
self.buildDocsService()
|
||||||
|
self.main_docs = self.service.documents().create(body={"title":name}).execute()
|
||||||
|
|
||||||
creds = None
|
def createOption(self, index, value, image=None):
|
||||||
# The file token.json stores the user's access and refresh tokens, and is
|
super().createOption(index, value, image)
|
||||||
# created automatically when the authorization flow completes for the first
|
|
||||||
# time.
|
|
||||||
if os.path.exists('token.json'):
|
|
||||||
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
|
|
||||||
self.infoPrint("token already exist",self.progress)
|
|
||||||
# If there are no (valid) credentials available, let the user log in.
|
|
||||||
if not creds or not creds.valid:
|
|
||||||
if creds and creds.expired and creds.refresh_token:
|
|
||||||
creds.refresh(Request())
|
|
||||||
self.infoPrint("refresh token",self.progress)
|
|
||||||
else:
|
|
||||||
flow = InstalledAppFlow.from_client_secrets_file(
|
|
||||||
self.savePath+'/secret/client_secret.json', SCOPES)
|
|
||||||
creds = flow.run_local_server(port=0)
|
|
||||||
self.infoPrint("generate token",self.progress)
|
|
||||||
# Save the credentials for the next run
|
|
||||||
with open('token.json', 'w') as token:
|
|
||||||
token.write(creds.to_json())
|
|
||||||
|
|
||||||
try:
|
|
||||||
self.infoPrint("creating service",self.progress)
|
|
||||||
self.docs_service = build('docs', 'v1', credentials=creds)
|
|
||||||
|
|
||||||
except HttpError as err:
|
|
||||||
print(err)
|
|
||||||
''' End Tokenizing
|
|
||||||
'''
|
|
||||||
|
|
||||||
def createDocs(self, name):
|
|
||||||
try:
|
|
||||||
self.infoPrint("creating document",self.progress)
|
|
||||||
self.main_docs = self.docs_service.documents().create(body={"title":name}).execute()
|
|
||||||
except HttpError as error:
|
|
||||||
print('An error occurred: %s' % error)
|
|
||||||
|
|
||||||
def createOption(self, value, image=None):
|
|
||||||
self.infoPrint("creating option",self.progress)
|
|
||||||
opttxt = "\t{}\r\n".format(value)
|
opttxt = "\t{}\r\n".format(value)
|
||||||
opt = [{'insertText' : {
|
opt = [{'insertText' : {
|
||||||
'location':{ 'index': 1, },
|
'location':{ 'index': 1, },
|
||||||
'text': opttxt }}]
|
'text': opttxt }}]
|
||||||
if(image != None):
|
if(image != None):
|
||||||
self.infoPrint("uploading option image... ",self.progress)
|
|
||||||
req = requests.post(self.image_temp_service_url,files={"file": open(image,'rb')})
|
|
||||||
if(req.json()['status'] == 'error'):
|
|
||||||
raise Exception("upload failed : {}".format(req.json()))
|
|
||||||
time.sleep(3)
|
|
||||||
self.infoPrint("uploading option image... ok",self.progress)
|
|
||||||
u = urlparse(req.json()['data']['url'])
|
|
||||||
opt.append( { 'insertInlineImage' : {
|
opt.append( { 'insertInlineImage' : {
|
||||||
'uri' : u._replace(path="/dl"+u.path).geturl(),
|
'uri' : self.image_temp_service_url + image,
|
||||||
'location' : { 'index' : len(opttxt)-1}}
|
'location' : { 'index' : len(opttxt)-1}}
|
||||||
})
|
})
|
||||||
return opt
|
return opt
|
||||||
|
|
||||||
def createQuestion(self, title, description, options, indexAnswer, itemImage=None):
|
def createQuestion(self, title, description, options, indexAnswer, itemImage=None):
|
||||||
self.infoPrint("create question...",self.progress)
|
super().createQuestion(title, description, options, indexAnswer, itemImage)
|
||||||
itemtxt = "{}\r\n".format(description)
|
itemtxt = "{}\r\n".format(description)
|
||||||
item = [{'insertText' : {
|
item = [{'insertText' : {
|
||||||
'text': itemtxt,
|
'text': itemtxt,
|
||||||
'location':{'index': 1,}}}]
|
'location':{'index': 1,}}}]
|
||||||
if (itemImage != None):
|
if (itemImage != None):
|
||||||
self.infoPrint("uploading question image...",self.progress)
|
|
||||||
req = requests.post(self.image_temp_service_url,files={"file": open(itemImage,'rb')})
|
|
||||||
if(req.json()['status'] == 'error'):
|
|
||||||
raise Exception("upload failed : {}".format(req.json()))
|
|
||||||
time.sleep(3)
|
|
||||||
self.infoPrint("uploading question image... ok",self.progress)
|
|
||||||
u = urlparse(req.json()['data']['url'])
|
|
||||||
item.append( { 'insertInlineImage' : {
|
item.append( { 'insertInlineImage' : {
|
||||||
'uri' : u._replace(path="/dl"+u.path).geturl(),
|
'uri' : self.image_temp_service_url + itemImage,
|
||||||
'location' : { 'index' : len(itemtxt)-1 }}
|
'location' : { 'index' : len(itemtxt)-1 }}
|
||||||
})
|
})
|
||||||
item = list(reversed(options)) + item
|
item = list(reversed(options)) + item
|
||||||
|
@ -127,17 +52,29 @@ class gdoc:
|
||||||
return item
|
return item
|
||||||
|
|
||||||
def submitItem(self, index, item):
|
def submitItem(self, index, item):
|
||||||
self.infoPrint("submit question",self.progress)
|
super().submitItem()
|
||||||
for _item in item :
|
for _item in item :
|
||||||
submition = {}
|
submition = {}
|
||||||
submition["requests"] = _item
|
submition["requests"] = _item
|
||||||
print(submition)
|
print(submition)
|
||||||
r = self.docs_service.documents().batchUpdate(documentId=self.main_docs["documentId"], body=submition).execute()
|
r = self.service.documents().batchUpdate(documentId=self.main_docs["documentId"], body=submition).execute()
|
||||||
print(r)
|
print(r)
|
||||||
|
time.sleep(.8)
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self.infoPrint("Updating Docs...",self.progress)
|
super().update()
|
||||||
# Prints the result to show the question has been added
|
# Prints the result to show the question has been added
|
||||||
# get_result = self.docs_service.documents().get(documentId=self.main_docs["documentId"]).execute()
|
state = self.service.documents().get(documentId=self.main_docs["documentId"]).execute()
|
||||||
self.resultUri = self.main_docs["documentId"]
|
content = state['body']['content']
|
||||||
|
endIndex = content[len(content)-1]['endIndex']
|
||||||
|
requests = [
|
||||||
|
{'insertText': { 'text' : "\r\n\r\n"+"\r\n".join(["{}".format(chr(64+key)) for key in self.questionKey]), 'location': {'index': endIndex-1}}},
|
||||||
|
{'insertPageBreak': {'location': {'index': endIndex}}},
|
||||||
|
{'createParagraphBullets': {'range': {'startIndex': 1, 'endIndex': endIndex }, 'bulletPreset': 'NUMBERED_DECIMAL_ALPHA_ROMAN',}}
|
||||||
|
]
|
||||||
|
print(requests)
|
||||||
|
r = self.service.documents().batchUpdate(documentId=self.main_docs["documentId"], body={'requests' : requests}).execute()
|
||||||
|
print(r)
|
||||||
|
|
||||||
|
self.resultUri = "https://docs.google.com/document/d/" + self.main_docs["documentId"] + "/edit"
|
||||||
self.infoPrint("Updating Form... Done",self.progress)
|
self.infoPrint("Updating Form... Done",self.progress)
|
||||||
|
|
|
@ -1,31 +1,23 @@
|
||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import os.path, time
|
import time
|
||||||
from google.auth.transport.requests import Request
|
from gservice import gservice
|
||||||
from google.oauth2.credentials import Credentials
|
|
||||||
from google_auth_oauthlib.flow import InstalledAppFlow
|
|
||||||
from googleapiclient.discovery import build
|
|
||||||
from googleapiclient.errors import HttpError
|
|
||||||
from pprint import pprint
|
|
||||||
import requests
|
|
||||||
from urllib.parse import urlparse
|
|
||||||
|
|
||||||
class gquiz:
|
class gquiz(gservice):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
'''
|
'''
|
||||||
Args :
|
Args :
|
||||||
templateId : get the id from link GoogleForm
|
templateId : get the id from link GoogleForm
|
||||||
'''
|
'''
|
||||||
self.image_temp_service_url = "https://tmpfiles.org/api/v1/upload"
|
super(gquiz, self).__init__()
|
||||||
|
self.image_temp_service_url = "https://git.manakin-gentoo.ts.net/a2nr/boring_edu_doc/raw/branch/master"
|
||||||
|
# self.image_temp_service_url = "https://tmpfiles.org/api/v1/upload"
|
||||||
# self.image_temp_service_url = "https://uguu.se/upload.php"
|
# self.image_temp_service_url = "https://uguu.se/upload.php"
|
||||||
self.submition = {"requests":[]}
|
self.submition = {"requests":[]}
|
||||||
self.form_service = None
|
|
||||||
self.main_form = None
|
self.main_form = None
|
||||||
self.infoPrint = self._defaultPrint
|
|
||||||
self.progress = -1
|
self.progress = -1
|
||||||
self.resultUri = ""
|
self.resultUri = ""
|
||||||
self.savePath = './'
|
|
||||||
|
|
||||||
self.submition["requests"].append({
|
self.submition["requests"].append({
|
||||||
"updateSettings": {
|
"updateSettings": {
|
||||||
|
@ -38,68 +30,14 @@ class gquiz:
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
def setProgress(self, i : int):
|
def generateService(self, name):
|
||||||
self.progress = i
|
|
||||||
|
|
||||||
def setSavePath(self, path):
|
|
||||||
self.savePath = path
|
|
||||||
|
|
||||||
def AttachProcessInfo(self, callback):
|
|
||||||
self.infoPrint = callback
|
|
||||||
|
|
||||||
def _defaultPrint(self, text : str, progress: int):
|
|
||||||
print("{}% {}".format(progress, text))
|
|
||||||
|
|
||||||
def generateService(self):
|
|
||||||
''' Start Tokenizing
|
|
||||||
here is the way to get token
|
|
||||||
link : https://developers.google.com/docs/api/quickstart/python
|
|
||||||
'''
|
|
||||||
SCOPES = ["https://www.googleapis.com/auth/forms.body",]
|
SCOPES = ["https://www.googleapis.com/auth/forms.body",]
|
||||||
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1"
|
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1"
|
||||||
|
self.generateToken('form', SCOPES)
|
||||||
|
self.buildFormService(DISCOVERY_DOC)
|
||||||
|
self.main_form = self.service.forms().create(body={"info":{"title":name}}).execute()
|
||||||
|
|
||||||
creds = None
|
def createOption(self, index, value, image=None):
|
||||||
# The file token.json stores the user's access and refresh tokens, and is
|
|
||||||
# created automatically when the authorization flow completes for the first
|
|
||||||
# time.
|
|
||||||
if os.path.exists('token.json'):
|
|
||||||
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
|
|
||||||
self.infoPrint("token already exist",self.progress)
|
|
||||||
# If there are no (valid) credentials available, let the user log in.
|
|
||||||
if not creds or not creds.valid:
|
|
||||||
if creds and creds.expired and creds.refresh_token:
|
|
||||||
creds.refresh(Request())
|
|
||||||
self.infoPrint("refresh token",self.progress)
|
|
||||||
else:
|
|
||||||
flow = InstalledAppFlow.from_client_secrets_file(
|
|
||||||
self.savePath+'/secret/client_secret.json', SCOPES)
|
|
||||||
creds = flow.run_local_server(port=0)
|
|
||||||
self.infoPrint("generate token",self.progress)
|
|
||||||
# Save the credentials for the next run
|
|
||||||
with open('token.json', 'w') as token:
|
|
||||||
token.write(creds.to_json())
|
|
||||||
|
|
||||||
try:
|
|
||||||
self.infoPrint("creating service",self.progress)
|
|
||||||
self.form_service = build('forms', 'v1', credentials=creds, discoveryServiceUrl=DISCOVERY_DOC, static_discovery=False)
|
|
||||||
|
|
||||||
except HttpError as err:
|
|
||||||
print(err)
|
|
||||||
''' End Tokenizing
|
|
||||||
'''
|
|
||||||
|
|
||||||
def createForm(self, name):
|
|
||||||
""" Create Form
|
|
||||||
Args:
|
|
||||||
name : name form
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
self.infoPrint("creating form",self.progress)
|
|
||||||
self.main_form = self.form_service.forms().create(body={"info":{"title":name}}).execute()
|
|
||||||
except HttpError as error:
|
|
||||||
print('An error occurred: %s' % error)
|
|
||||||
|
|
||||||
def createOption(self, value, image=None):
|
|
||||||
'''
|
'''
|
||||||
return {"value" : "A. option 1"}
|
return {"value" : "A. option 1"}
|
||||||
return {"value": "Option",
|
return {"value": "Option",
|
||||||
|
@ -110,23 +48,13 @@ class gquiz:
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
'''
|
'''
|
||||||
self.infoPrint("creating option",self.progress)
|
super().createOption(index, value, image)
|
||||||
opt = {"value" : "{}".format(value)}
|
opt = {"value" : "{}. {}".format(chr(64+index), value)}
|
||||||
if(image != None):
|
if(image != None):
|
||||||
self.infoPrint("uploading option image... ",self.progress)
|
|
||||||
req = requests.post(self.image_temp_service_url,files={"file": open(image,'rb')})
|
|
||||||
if(req.json()['status'] == 'error'):
|
|
||||||
raise Exception("upload failed : {}".format(req.json()))
|
|
||||||
time.sleep(3)
|
|
||||||
self.infoPrint("uploading option image... ok",self.progress)
|
|
||||||
u = urlparse(req.json()['data']['url'])
|
|
||||||
opt.update({"image" : {
|
opt.update({"image" : {
|
||||||
"sourceUri": u._replace(path="/dl"+u.path).geturl(),
|
"sourceUri": self.image_temp_service_url + image,
|
||||||
"properties": {
|
"properties": {"alignment": "LEFT"}
|
||||||
"alignment": "LEFT"
|
|
||||||
}
|
|
||||||
}})
|
}})
|
||||||
time.sleep(3)
|
|
||||||
return opt
|
return opt
|
||||||
|
|
||||||
def createQuestion(self, title, description, options, indexAnswer, itemImage=None):
|
def createQuestion(self, title, description, options, indexAnswer, itemImage=None):
|
||||||
|
@ -142,6 +70,7 @@ class gquiz:
|
||||||
{"value" : "D. option 4"},
|
{"value" : "D. option 4"},
|
||||||
{"value" : "E. option 5"},]
|
{"value" : "E. option 5"},]
|
||||||
'''
|
'''
|
||||||
|
super().createQuestion(title, description, options, indexAnswer, itemImage)
|
||||||
item = {
|
item = {
|
||||||
"title" : title,
|
"title" : title,
|
||||||
"description" : "{}".format(description),
|
"description" : "{}".format(description),
|
||||||
|
@ -160,18 +89,10 @@ class gquiz:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.infoPrint("create question...",self.progress)
|
|
||||||
if (itemImage != None):
|
if (itemImage != None):
|
||||||
self.infoPrint("uploading question image...",self.progress)
|
|
||||||
req = requests.post(self.image_temp_service_url,files={"file": open(itemImage,'rb')})
|
|
||||||
if(req.json()['status'] == 'error'):
|
|
||||||
raise Exception("upload failed : {}".format(req.json()))
|
|
||||||
time.sleep(3)
|
|
||||||
self.infoPrint("uploading question image... ok",self.progress)
|
|
||||||
u = urlparse(req.json()['data']['url'])
|
|
||||||
item['questionItem'].update(\
|
item['questionItem'].update(\
|
||||||
{"image": {
|
{"image": {
|
||||||
"sourceUri": u._replace(path="/dl"+u.path).geturl(),
|
"sourceUri": self.image_temp_service_url + itemImage,
|
||||||
"properties": {
|
"properties": {
|
||||||
"alignment": "CENTER"
|
"alignment": "CENTER"
|
||||||
}
|
}
|
||||||
|
@ -180,29 +101,22 @@ class gquiz:
|
||||||
return item
|
return item
|
||||||
|
|
||||||
def submitItem(self, index, item):
|
def submitItem(self, index, item):
|
||||||
""" Submit item to submition
|
super().submitItem()
|
||||||
Args:
|
|
||||||
index : location item in form
|
|
||||||
item : object item
|
|
||||||
"""
|
|
||||||
self.submition['requests'].append({
|
self.submition['requests'].append({
|
||||||
"createItem" : {
|
"createItem" : {
|
||||||
"location": {"index": index},
|
"location": {"index": index},
|
||||||
"item": item
|
"item": item
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
self.infoPrint("submit question",self.progress)
|
|
||||||
print(self.submition)
|
print(self.submition)
|
||||||
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self.infoPrint("Updating Form...",self.progress)
|
super().update()
|
||||||
# Adds the question to the form
|
# Adds the question to the form
|
||||||
question_setting = self.form_service.forms().batchUpdate(formId=self.main_form["formId"], body=self.submition).execute()
|
|
||||||
print(question_setting)
|
print(question_setting)
|
||||||
|
|
||||||
# Prints the result to show the question has been added
|
# Prints the result to show the question has been added
|
||||||
get_result = self.form_service.forms().get(formId=self.main_form["formId"]).execute()
|
get_result = self.service.forms().get(formId=self.main_form["formId"]).execute()
|
||||||
self.resultUri = get_result['responderUri']
|
self.resultUri = get_result['responderUri']
|
||||||
print(get_result)
|
print(get_result)
|
||||||
self.infoPrint("Updating Form... Done",self.progress)
|
self.infoPrint("Updating Form... Done",self.progress)
|
||||||
|
|
|
@ -1 +1,62 @@
|
||||||
# TODO jadikan satu semua code yang berhubungan dengan autentikasi dan generate service
|
import os.path
|
||||||
|
from google.auth.transport.requests import Request
|
||||||
|
from google.oauth2.credentials import Credentials
|
||||||
|
from google_auth_oauthlib.flow import InstalledAppFlow
|
||||||
|
from googleapiclient.discovery import build
|
||||||
|
from googleapiclient.errors import HttpError
|
||||||
|
from bedc import bedc
|
||||||
|
|
||||||
|
class gservice(bedc):
|
||||||
|
"""docstring for gservice."""
|
||||||
|
def __init__(self):
|
||||||
|
super(gservice, self).__init__()
|
||||||
|
self.service = None
|
||||||
|
self.creds = None
|
||||||
|
|
||||||
|
def fetchToken(self, SCOPES):
|
||||||
|
self.infoPrint("generate token",self.progress)
|
||||||
|
flow = InstalledAppFlow.from_client_secrets_file(
|
||||||
|
self.savePath+'/secret/client_secret.json', SCOPES)
|
||||||
|
self.creds = flow.run_local_server(port=0)
|
||||||
|
|
||||||
|
def generateToken(self,serviceType, SCOPES):
|
||||||
|
''' Start Tokenizing
|
||||||
|
here is the way to get token
|
||||||
|
link : https://developers.google.com/docs/api/quickstart/python
|
||||||
|
'''
|
||||||
|
# The file token.json stores the user's access and refresh tokens, and is
|
||||||
|
# created automatically when the authorization flow completes for the first
|
||||||
|
# time.
|
||||||
|
if os.path.exists('token.json.{}'.format(serviceType)):
|
||||||
|
self.creds = Credentials.from_authorized_user_file('token.json.{}'.format(serviceType), SCOPES)
|
||||||
|
self.infoPrint("token already exist",self.progress)
|
||||||
|
# If there are no (valid) credentials available, let the user log in.
|
||||||
|
if not self.creds or not self.creds.valid:
|
||||||
|
if self.creds and self.creds.expired and self.creds.refresh_token:
|
||||||
|
try:
|
||||||
|
self.creds.refresh(Request())
|
||||||
|
except:
|
||||||
|
self.fetchToken(SCOPES)
|
||||||
|
self.infoPrint("refresh token",self.progress)
|
||||||
|
else:
|
||||||
|
self.fetchToken(SCOPES)
|
||||||
|
# Save the credentials for the next run
|
||||||
|
with open('token.json.{}'.format(serviceType), 'w') as token:
|
||||||
|
token.write(self.creds.to_json())
|
||||||
|
|
||||||
|
def buildFormService(self, DISCOVERY_DOC):
|
||||||
|
try:
|
||||||
|
self.infoPrint("creating form service",self.progress)
|
||||||
|
self.service = build('forms', 'v1', credentials=self.creds, discoveryServiceUrl=DISCOVERY_DOC, static_discovery=False)
|
||||||
|
except HttpError as err:
|
||||||
|
self.infoPrint("Error create service : {}".format(err),self.progress)
|
||||||
|
print(err)
|
||||||
|
pass
|
||||||
|
|
||||||
|
def buildDocsService(self):
|
||||||
|
try:
|
||||||
|
self.infoPrint("creating service",self.progress)
|
||||||
|
self.service = build('docs', 'v1', credentials=self.creds)
|
||||||
|
except HttpError as err:
|
||||||
|
print(err)
|
||||||
|
pass
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
import requests
|
|
||||||
|
|
||||||
class ImageProvides:
|
|
||||||
def __init__(self):
|
|
||||||
self.service_url = [ \
|
|
||||||
"https://tmpfiles.org/api/v1/upload" ]
|
|
||||||
self.service_use = 0
|
|
||||||
self.successStatus = False
|
|
||||||
self.urlImage = ""
|
|
||||||
|
|
||||||
def upload(self, imagePath):
|
|
||||||
try:
|
|
||||||
urlService = self.service_url[self.service_use]
|
|
||||||
req = requests.post(urlService,files={"file": open(image,'rb')})
|
|
||||||
if(req.json()['status'] == 'error'):
|
|
||||||
self.successStatus = False
|
|
||||||
self.successStatus = True
|
|
||||||
|
|
||||||
|
|
||||||
def isSuccess(self):
|
|
||||||
return self.successStatus
|
|
||||||
|
|
||||||
def getUrl(self):
|
|
|
@ -2,27 +2,14 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import base64
|
import base64
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
|
from bedc import bedc
|
||||||
|
|
||||||
class moodleQuiz:
|
class moodleQuiz(bedc):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.infoPrint = self._defaultPrint
|
super(moodleQuiz, self).__init__()
|
||||||
self.progress = -1
|
self.progress = -1
|
||||||
self.resultUri = ""
|
self.resultUri = ""
|
||||||
self.data = ET.Element('quiz')
|
self.data = ET.Element('quiz')
|
||||||
self.savePath = '.'
|
|
||||||
|
|
||||||
def setSavePath(self, path):
|
|
||||||
self.savePath = path
|
|
||||||
|
|
||||||
def setProgress(self, i : int):
|
|
||||||
self.progress = i
|
|
||||||
self.infoPrint("generating...", self.progress)
|
|
||||||
|
|
||||||
def AttachProcessInfo(self, callback):
|
|
||||||
self.infoPrint = callback
|
|
||||||
|
|
||||||
def _defaultPrint(self, text : str, progress: int):
|
|
||||||
print("{}% {}".format(progress, text))
|
|
||||||
|
|
||||||
def _subElementFile(self, parent, file):
|
def _subElementFile(self, parent, file):
|
||||||
Efile = ET.SubElement(parent, "file")
|
Efile = ET.SubElement(parent, "file")
|
||||||
|
@ -31,12 +18,13 @@ class moodleQuiz:
|
||||||
Efile.set('encoding','base64')
|
Efile.set('encoding','base64')
|
||||||
Efile.text = base64.b64encode(file.read()).decode('utf-8')
|
Efile.text = base64.b64encode(file.read()).decode('utf-8')
|
||||||
|
|
||||||
def createOption(self, value, image=None):
|
def createOption(self, index, value, image=None):
|
||||||
|
super().createOption(index, value, image)
|
||||||
ans = ET.Element('answer')
|
ans = ET.Element('answer')
|
||||||
ans.set('fraction', '0')
|
ans.set('fraction', '0')
|
||||||
ans.set('format', 'html')
|
ans.set('format', 'html')
|
||||||
if(image == None):
|
if(image == None):
|
||||||
ET.SubElement(ans, 'text').text = "<p dir=\"ltr\" style=\"text-align: left;\">{}</p>".format(value)
|
ET.SubElement(ans, 'text').text = "<p dir=\"ltr\" style=\"text-align: left;\">{}. {}</p>".format(index, value)
|
||||||
else :
|
else :
|
||||||
f = open(image,"rb")
|
f = open(image,"rb")
|
||||||
ET.SubElement(ans, 'text').text = "<p dir=\"ltr\" style=\"text-align: left;\">" + \
|
ET.SubElement(ans, 'text').text = "<p dir=\"ltr\" style=\"text-align: left;\">" + \
|
||||||
|
@ -48,6 +36,7 @@ class moodleQuiz:
|
||||||
|
|
||||||
|
|
||||||
def createQuestion(self, title, description, options, indexAnswer, itemImage=None):
|
def createQuestion(self, title, description, options, indexAnswer, itemImage=None):
|
||||||
|
super().createQuestion(title, description, options, indexAnswer, itemImage)
|
||||||
question = ET.Element('question')
|
question = ET.Element('question')
|
||||||
question.set('type','multichoice')
|
question.set('type','multichoice')
|
||||||
|
|
||||||
|
@ -77,13 +66,12 @@ class moodleQuiz:
|
||||||
ET.SubElement(question, 'answernumbering').text = 'abc'
|
ET.SubElement(question, 'answernumbering').text = 'abc'
|
||||||
return question
|
return question
|
||||||
|
|
||||||
def submitQuestion(self, index, item):
|
def submitItem(self, index, item):
|
||||||
|
super().submitItem()
|
||||||
self.data.append(ET.fromstring(ET.tostring(item)))
|
self.data.append(ET.fromstring(ET.tostring(item)))
|
||||||
|
|
||||||
def copyFile(self,origin_file_id, copy_title):
|
|
||||||
print("noting to copy, it for moodle")
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
|
super().update()
|
||||||
ET.indent(self.data)
|
ET.indent(self.data)
|
||||||
print(ET.tostring(self.data))
|
print(ET.tostring(self.data))
|
||||||
ET.ElementTree(self.data).write("{}/{}".format(self.savePath, "moodleXMLMultichoiceQuestion.xml"))
|
ET.ElementTree(self.data).write("{}/{}".format(self.savePath, "moodleXMLMultichoiceQuestion.xml"))
|
||||||
|
|
|
@ -46,13 +46,14 @@ def MakeTemplate():
|
||||||
def _statusBarInfoUpdate(text : str, progress : int):
|
def _statusBarInfoUpdate(text : str, progress : int):
|
||||||
ui.ShowProgressBar("loading...", "{}% {}".format(progress,text), progress)
|
ui.ShowProgressBar("loading...", "{}% {}".format(progress,text), progress)
|
||||||
|
|
||||||
def _updateQuestion(q, isReverse = False):
|
def _updateQuestion(q, isReverse = False, needPath = False):
|
||||||
selctedCell = doc.CurrentSelection #
|
selctedCell = doc.CurrentSelection #
|
||||||
cwd = curpath
|
cwd = curpath
|
||||||
maxRow = doc.LastRow(selctedCell)+1-doc.FirstRow(selctedCell)
|
maxRow = doc.LastRow(selctedCell)+1-doc.FirstRow(selctedCell)
|
||||||
rnge = reversed(range(0, maxRow)) if isReverse else range(0, maxRow)
|
rnge = reversed(range(0, maxRow)) if isReverse else range(0, maxRow)
|
||||||
for nrow in rnge:
|
for nrow in rnge:
|
||||||
q.setProgress(int(((nrow+1)/maxRow)*100))
|
xrow = (nrow - (maxRow-1)) if isReverse else nrow
|
||||||
|
q.setProgress(int(((xrow+1)/maxRow)*100))
|
||||||
item = doc.getValue(doc.Offset(doc.FirstCell(selctedCell),nrow,0,1,17))
|
item = doc.getValue(doc.Offset(doc.FirstCell(selctedCell),nrow,0,1,17))
|
||||||
# item[0] <Text question>
|
# item[0] <Text question>
|
||||||
# item[1] <IMG question image>
|
# item[1] <IMG question image>
|
||||||
|
@ -73,23 +74,30 @@ def _updateQuestion(q, isReverse = False):
|
||||||
# item[16] <Text Option5>
|
# item[16] <Text Option5>
|
||||||
opt, theAnswer, c = [], -1, 1
|
opt, theAnswer, c = [], -1, 1
|
||||||
for o in range(2, 17, 3): # mengambil kolim jawaban saja
|
for o in range(2, 17, 3): # mengambil kolim jawaban saja
|
||||||
if (item[o] == 1)
|
if (item[o] == 1):
|
||||||
theAnswer = c
|
theAnswer = c
|
||||||
opt.append(\
|
if (item[o+1] != ""):
|
||||||
q.createOption(\
|
optImg = cwd+item[o+1] if needPath else item[o+1]
|
||||||
"{}. {}".format(chr(64+c),item[o+2]),\
|
else:
|
||||||
cwd+item[o+1] if (item[o+1] != "") else None))
|
optImg = None
|
||||||
|
|
||||||
|
opt.append(q.createOption(c, item[o+2], optImg))
|
||||||
c = c+1
|
c = c+1
|
||||||
|
|
||||||
if (theAnswer == -1):
|
if (theAnswer == -1):
|
||||||
raise Exception("Chose the correct answer")
|
raise Exception("Chose the correct answer")
|
||||||
|
|
||||||
img = cwd+item[1] if ( item[1] != "") else None
|
if (item[1] != ""):
|
||||||
|
itmImg = cwd+item[1] if needPath else item[1]
|
||||||
|
else:
|
||||||
|
itmImg = None
|
||||||
|
|
||||||
qq = q.createQuestion(\
|
qq = q.createQuestion(\
|
||||||
title = "Soal No {}".format(nrow+1),\
|
title = "Soal No {}".format(nrow+1),\
|
||||||
description = item[0],\
|
description = item[0],\
|
||||||
indexAnswer = theAnswer, \
|
indexAnswer = theAnswer, \
|
||||||
options = opt, itemImage=img)
|
options = opt,\
|
||||||
|
itemImage = itmImg)
|
||||||
q.submitItem(nrow,qq)
|
q.submitItem(nrow,qq)
|
||||||
q.update()
|
q.update()
|
||||||
|
|
||||||
|
@ -100,8 +108,7 @@ def GoogleQuiz():
|
||||||
q.setSavePath(curpath)
|
q.setSavePath(curpath)
|
||||||
q.AttachProcessInfo(_statusBarInfoUpdate)
|
q.AttachProcessInfo(_statusBarInfoUpdate)
|
||||||
q.setProgress(0)
|
q.setProgress(0)
|
||||||
q.generateService()
|
q.generateService("form generated using macro")
|
||||||
q.createForm("Demo Soal") #TODO : put this mundane inside generateService()
|
|
||||||
_updateQuestion(q)
|
_updateQuestion(q)
|
||||||
ui.SetStatusbar("creating google form quiz done!")
|
ui.SetStatusbar("creating google form quiz done!")
|
||||||
_statusBarInfoUpdate("Finish!!",100)
|
_statusBarInfoUpdate("Finish!!",100)
|
||||||
|
@ -114,8 +121,7 @@ def GoogleDocs():
|
||||||
q.setSavePath(curpath)
|
q.setSavePath(curpath)
|
||||||
q.AttachProcessInfo(_statusBarInfoUpdate)
|
q.AttachProcessInfo(_statusBarInfoUpdate)
|
||||||
q.setProgress(0)
|
q.setProgress(0)
|
||||||
q.generateService()
|
q.generateService("docs generated using macro")
|
||||||
q.createDocs("Demo Soal") #TODO : put this mundane inside generateService()
|
|
||||||
_updateQuestion(q, True)
|
_updateQuestion(q, True)
|
||||||
ui.SetStatusbar("creating google form quiz done!")
|
ui.SetStatusbar("creating google form quiz done!")
|
||||||
_statusBarInfoUpdate("Finish!!",100)
|
_statusBarInfoUpdate("Finish!!",100)
|
||||||
|
@ -128,7 +134,7 @@ def MoodleQuiz():
|
||||||
q.setSavePath(curpath)
|
q.setSavePath(curpath)
|
||||||
q.AttachProcessInfo(_statusBarInfoUpdate)
|
q.AttachProcessInfo(_statusBarInfoUpdate)
|
||||||
q.setProgress(0)
|
q.setProgress(0)
|
||||||
_updateQuestion(q)
|
_updateQuestion(q, needPath=True)
|
||||||
ui.SetStatusbar("Done!")
|
ui.SetStatusbar("Done!")
|
||||||
_statusBarInfoUpdate("Check *.xml file in curent folder!",100)
|
_statusBarInfoUpdate("Check *.xml file in curent folder!",100)
|
||||||
|
|
||||||
|
|
|
@ -1,75 +0,0 @@
|
||||||
# coding: utf-8
|
|
||||||
# run it under boring_edu_doc folder
|
|
||||||
# $ python Scripts/python/test_gquiz.py
|
|
||||||
from __future__ import unicode_literals
|
|
||||||
import os, sys, requests
|
|
||||||
if (len(sys.argv) < 2):
|
|
||||||
print("arg[1] : form, docs, moodle")
|
|
||||||
quit()
|
|
||||||
|
|
||||||
def gquiz_form_test():
|
|
||||||
'''
|
|
||||||
arg[1] = form
|
|
||||||
'''
|
|
||||||
if not 'gquiz' in sys.modules:
|
|
||||||
sys.path.insert(0, os.getcwd()+"/Scripts/python/Library/")
|
|
||||||
from gquiz import gquiz
|
|
||||||
|
|
||||||
q = gquiz()
|
|
||||||
q.setSavePath(".")
|
|
||||||
q.setProgress(0)
|
|
||||||
q.generateService()
|
|
||||||
q.setProgress(20)
|
|
||||||
q.createForm("test quiz")
|
|
||||||
q.setProgress(50)
|
|
||||||
opt = [ q.createOption("A.","./asset/option1.png"),
|
|
||||||
q.createOption("B.","./asset/option2.png"),
|
|
||||||
q.createOption("C.","./asset/option3.png"),
|
|
||||||
q.createOption("D.","./asset/option4.png"),
|
|
||||||
q.createOption("E.","./asset/option5.png") ]
|
|
||||||
q.setProgress(80)
|
|
||||||
qq = q.createQuestion(title = "Soal No 1",\
|
|
||||||
description = "Dari gambar dibawah ini, ada bagian gambar yang hilang. Dari pilihan dibawah, manakah gambar yang benar?",\
|
|
||||||
indexAnswer = 4, options = opt, itemImage='./asset/test_image.png')
|
|
||||||
q.setProgress(85)
|
|
||||||
q.submitItem(0,qq)
|
|
||||||
q.setProgress(90)
|
|
||||||
q.update()
|
|
||||||
q.setProgress(100)
|
|
||||||
print("Open link : {}".format(q.resultUri))
|
|
||||||
|
|
||||||
def gquiz_docs_test():
|
|
||||||
'''
|
|
||||||
arg[1] = docs
|
|
||||||
'''
|
|
||||||
if not 'gdoc' in sys.modules:
|
|
||||||
sys.path.insert(0, os.getcwd()+"/Scripts/python/Library/")
|
|
||||||
from gdoc import gdoc
|
|
||||||
|
|
||||||
q = gdoc()
|
|
||||||
q.setSavePath(".")
|
|
||||||
q.setProgress(0)
|
|
||||||
q.generateService()
|
|
||||||
q.setProgress(20)
|
|
||||||
q.createDocs("test quiz")
|
|
||||||
q.setProgress(50)
|
|
||||||
opt = [ q.createOption("A.","./asset/option1.png"),
|
|
||||||
q.createOption("B.","./asset/option2.png"),
|
|
||||||
q.createOption("C.","./asset/option3.png"),
|
|
||||||
q.createOption("D.","./asset/option4.png"),
|
|
||||||
q.createOption("E.","./asset/option5.png") ]
|
|
||||||
q.setProgress(80)
|
|
||||||
qq = q.createQuestion(title = "Soal No 1",\
|
|
||||||
description = "Dari gambar dibawah ini, ada bagian gambar yang hilang. Dari pilihan dibawah, manakah gambar yang benar?",\
|
|
||||||
indexAnswer = 4, options = opt, itemImage='./asset/test_image.png')
|
|
||||||
q.setProgress(85)
|
|
||||||
q.submitItem(0,qq)
|
|
||||||
q.setProgress(90)
|
|
||||||
q.update()
|
|
||||||
q.setProgress(100)
|
|
||||||
print("Open link : {}".format(q.resultUri))
|
|
||||||
|
|
||||||
if (sys.argv[1] == 'form'):
|
|
||||||
gquiz_form_test()
|
|
||||||
elif (sys.argv[1] == 'docs'):
|
|
||||||
gquiz_docs_test()
|
|
|
@ -0,0 +1,95 @@
|
||||||
|
# coding: utf-8
|
||||||
|
# run it under boring_edu_doc folder
|
||||||
|
# $ python Scripts/python/test_gquiz.py
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
import os, sys, requests
|
||||||
|
if (len(sys.argv) < 2):
|
||||||
|
print("arg[1] : form, docs, moodle")
|
||||||
|
quit()
|
||||||
|
|
||||||
|
def gquiz_form_test():
|
||||||
|
'''
|
||||||
|
arg[1] = form
|
||||||
|
'''
|
||||||
|
if not 'gquiz' in sys.modules:
|
||||||
|
sys.path.insert(0, os.getcwd()+"/Scripts/python/Library/")
|
||||||
|
from gquiz import gquiz
|
||||||
|
|
||||||
|
q = gquiz()
|
||||||
|
q.setSavePath(".")
|
||||||
|
q.setProgress(0)
|
||||||
|
q.generateService("unit test form")
|
||||||
|
q.setProgress(50)
|
||||||
|
opt = [ q.createOption(1, "option1","/asset/option1.png"),
|
||||||
|
q.createOption(2, "option2","/asset/option2.png"),
|
||||||
|
q.createOption(3, "option3","/asset/option3.png"),
|
||||||
|
q.createOption(4, "option4","/asset/option4.png"),
|
||||||
|
q.createOption(5, "option5","/asset/option5.png") ]
|
||||||
|
q.setProgress(80)
|
||||||
|
qq = q.createQuestion(title = "Soal No 1",\
|
||||||
|
description = "Dari gambar dibawah ini, ada bagian gambar yang hilang. Dari pilihan dibawah, manakah gambar yang benar?",\
|
||||||
|
indexAnswer = 4, options = opt, itemImage='/asset/test_image.png')
|
||||||
|
q.setProgress(85)
|
||||||
|
q.submitItem(0,qq)
|
||||||
|
q.setProgress(90)
|
||||||
|
q.update()
|
||||||
|
q.setProgress(100)
|
||||||
|
print("Open link : {}".format(q.resultUri))
|
||||||
|
|
||||||
|
def gquiz_docs_test():
|
||||||
|
'''
|
||||||
|
arg[1] = docs
|
||||||
|
'''
|
||||||
|
if not 'gdoc' in sys.modules:
|
||||||
|
sys.path.insert(0, os.getcwd()+"/Scripts/python/Library/")
|
||||||
|
from gdoc import gdoc
|
||||||
|
|
||||||
|
q = gdoc()
|
||||||
|
q.setSavePath(".")
|
||||||
|
q.setProgress(0)
|
||||||
|
q.generateService("unit test docs")
|
||||||
|
q.setProgress(50)
|
||||||
|
opt = [ q.createOption(1, "option1","/asset/option1.png"),
|
||||||
|
q.createOption(2, "option2","/asset/option2.png"),
|
||||||
|
q.createOption(3, "option3","/asset/option3.png"),
|
||||||
|
q.createOption(4, "option4","/asset/option4.png"),
|
||||||
|
q.createOption(5, "option5","/asset/option5.png") ]
|
||||||
|
q.setProgress(80)
|
||||||
|
qq = q.createQuestion(title = "Soal No 1",\
|
||||||
|
description = "Dari gambar dibawah ini, ada bagian gambar yang hilang. Dari pilihan dibawah, manakah gambar yang benar?",\
|
||||||
|
indexAnswer = 4, options = opt, itemImage='/asset/test_image.png')
|
||||||
|
q.setProgress(85)
|
||||||
|
q.submitItem(0,qq)
|
||||||
|
q.setProgress(90)
|
||||||
|
q.update()
|
||||||
|
q.setProgress(100)
|
||||||
|
print("Open link : {}".format(q.resultUri))
|
||||||
|
|
||||||
|
def moodle_test():
|
||||||
|
if not 'moodleQuiz' in sys.modules:
|
||||||
|
sys.path.insert(0, os.getcwd()+"/Scripts/python/Library/")
|
||||||
|
from moodleQuiz import moodleQuiz
|
||||||
|
|
||||||
|
q = moodleQuiz()
|
||||||
|
q.setSavePath(".")
|
||||||
|
q.setProgress(0)
|
||||||
|
opt = [ q.createOption(1, "option1","./asset/option1.png"),
|
||||||
|
q.createOption(2, "option2","./asset/option2.png"),
|
||||||
|
q.createOption(3, "option3","./asset/option3.png"),
|
||||||
|
q.createOption(4, "option4","./asset/option4.png"),
|
||||||
|
q.createOption(5, "option5","./asset/option5.png") ]
|
||||||
|
qq = q.createQuestion(title = "Soal No 1",\
|
||||||
|
description = "Dari gambar dibawah ini, ada bagian gambar yang hilang. Dari pilihan dibawah, manakah gambar yang benar?",\
|
||||||
|
indexAnswer = 4, options = opt, itemImage='./asset/test_image.png')
|
||||||
|
q.setProgress(85)
|
||||||
|
q.submitItem(0,qq)
|
||||||
|
q.setProgress(80)
|
||||||
|
q.update()
|
||||||
|
q.setProgress(100)
|
||||||
|
|
||||||
|
if (sys.argv[1] == 'form'):
|
||||||
|
gquiz_form_test()
|
||||||
|
elif (sys.argv[1] == 'docs'):
|
||||||
|
gquiz_docs_test()
|
||||||
|
elif (sys.argv[1] == 'moodle'):
|
||||||
|
moodle_test()
|
Loading…
Reference in New Issue