diff --git a/Scripts/Python/Library/gquiz.py b/Scripts/Python/Library/gquiz.py index 3d5af35..5be7b38 100644 --- a/Scripts/Python/Library/gquiz.py +++ b/Scripts/Python/Library/gquiz.py @@ -1,7 +1,7 @@ # coding: utf-8 from __future__ import unicode_literals -import os.path +import os.path, time from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow @@ -18,6 +18,7 @@ class gquiz: templateId : get the id from link GoogleForm ''' self.image_temp_service_url = "https://tmpfiles.org/api/v1/upload" + # self.image_temp_service_url = "https://uguu.se/upload.php" self.submition = {"requests":[]} self.form_service = None self.drive_service = None @@ -74,6 +75,16 @@ class gquiz: ''' End Tokenizing ''' + def createForm(self, name): + """ Create Form + Args: + name : name form + """ + try: + self.main_form = self.form_service.forms().create(body={"info":{"title":name}}).execute() + except HttpError as error: + print('An error occurred: %s' % error) + def copyFile(self,origin_file_id, copy_title): """Copy an existing file. Args: @@ -97,7 +108,6 @@ class gquiz: except HttpError as error: print('An error occurred: %s' % error) - return None def createOption(self, value, image=None): ''' @@ -124,6 +134,7 @@ class gquiz: "alignment": "LEFT" } }}) + time.sleep(3) return opt def createQuestion(self, title, description, options, indexAnswer, itemImage=None): @@ -172,6 +183,7 @@ class gquiz: } } }) + time.sleep(3) return item def submitQuestion(self, index, item): diff --git a/Scripts/Python/Library/imageprovider.py b/Scripts/Python/Library/imageprovider.py new file mode 100644 index 0000000..7f55ff4 --- /dev/null +++ b/Scripts/Python/Library/imageprovider.py @@ -0,0 +1,23 @@ +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): diff --git a/Scripts/Python/quiz_generator.py b/Scripts/Python/quiz_generator.py index 268a5c9..58228e0 100644 --- a/Scripts/Python/quiz_generator.py +++ b/Scripts/Python/quiz_generator.py @@ -7,50 +7,59 @@ def generateTemplate(): doc = CreateScriptService("Calc") basic = CreateScriptService("Basic") doc.SetArray(doc.CurrentSelection, \ - (("","", \ - "", "", "", \ - "", "", "", \ - "", "", "", \ - "", "", "", \ - "", "", "", ),)) + (("","", \ + "", "", "", \ + "", "", "", \ + "", "", "", \ + "", "", "", \ + "", "", "", ),)) def updateQuestion(): - import sys, os - from unohelper import fileUrlToSystemPath - if not 'gquiz' in sys.modules: - doc = XSCRIPTCONTEXT.getDocument() - url = fileUrlToSystemPath('{}/{}'.format(doc.URL,'Scripts/python/Library')) - sys.path.insert(0, url) - from gquiz import gquiz - import requests - + import sys, os + from unohelper import fileUrlToSystemPath + if not 'gquiz' in sys.modules: + doc = XSCRIPTCONTEXT.getDocument() + url = fileUrlToSystemPath('{}/{}'.format(doc.URL,'Scripts/python/Library')) + sys.path.insert(0, url) + + from gquiz import gquiz + import requests - doc = CreateScriptService("Calc") - item = doc.getValue(doc.Offset(doc.CurrentSelection,0,0,1,17)) - - q = gquiz() + doc = CreateScriptService("Calc") + selctedCell = doc.CurrentSelection + + q = gquiz() + q.generateService() + q.createForm("Demo Soal") + cwd = os.getcwd() + for nrow in range(0, doc.LastRow(selctedCell)+1-doc.FirstRow(selctedCell)): + item = doc.getValue(doc.Offset(doc.FirstCell(selctedCell),nrow,0,1,17)) + opt = [] + theAnswer = -1 + c = 1 + + for o in range(2,17,3): + if (item[o] == 1): + theAnswer = c + c = c+1 + if (item[o+1] != ""): + opt.append(q.createOption(item[o+2],cwd+item[o+1])) + else: + opt.append(q.createOption(item[o+2])) + + if (theAnswer == -1): + raise Exception("Chose the correct answer") - q.generateService() - - q.copyFile("16-rh3W-NwYzdKVBZJmi574sTWe_rMIdE-FQSw_33qXI", "Demo Soal") - - opt = [] - - theAnswer = -1 - c = 1 - for o in range(2,17,3): - if (item[o] == 1): - theAnswer = c - c = c+1 - opt.append(q.createOption(item[o+2],os.getcwd()+item[o+1])) - - if (theAnswer == -1): - raise Exception("Chose the correct answer") - - qq = q.createQuestion(title = "Soal No 1",\ - description = item[0],\ - indexAnswer = theAnswer, options = opt, itemImage=os.getcwd()+item[1]) - q.submitQuestion(0,qq) - q.update() + if( item[1] != ""): + img = cwd+item[1] + else: + img = None + + qq = q.createQuestion(title = "Soal No 1",\ + description = item[0],\ + indexAnswer = theAnswer, \ + options = opt, itemImage=img) + q.submitQuestion(nrow,qq) + q.update() g_exportedScripts = (generateTemplate, updateQuestion, ) diff --git a/myedu.ods b/myedu.ods index 4e8a941..42a04ec 100644 Binary files a/myedu.ods and b/myedu.ods differ