boring_edu_doc/Scripts/python/Library/gdoc.py

81 lines
3.4 KiB
Python

# coding: utf-8
from __future__ import unicode_literals
import time, json
from gservice import gservice
class gdoc(gservice):
def __init__(self):
super(gdoc, self).__init__()
self.image_temp_service_url = "https://git.manakin-gentoo.ts.net/a2nr/boring_edu_doc/raw/branch/mapel_data_science"
# 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.submition = {"requests":[]}
self.main_docs = None
self.progress = -1
self.resultUri = ""
self.questionKey = []
def generateService(self, name):
SCOPES = ["https://www.googleapis.com/auth/documents",]
self.generateToken('docs', SCOPES)
self.buildDocsService()
self.main_docs = self.service.documents().create(body={"title":name}).execute()
def createOption(self, index, value, image=None):
super().createOption(index, value, image)
opttxt = "\t{}\r\n".format(value)
opt = [{'insertText' : {
'location':{ 'index': 1, },
'text': opttxt }}]
if(image != None):
opt.append( { 'insertInlineImage' : {
'uri' : self.image_temp_service_url + image,
'location' : { 'index' : len(opttxt)-1}}
})
return opt
def createQuestion(self, title, description, options, indexAnswer, itemImage=None):
super().createQuestion(title, description, options, indexAnswer, itemImage)
itemtxt = "{}\r\n".format(description)
item = [{'insertText' : {
'text': itemtxt,
'location':{'index': 1,}}}]
if (itemImage != None):
item.append( { 'insertInlineImage' : {
'uri' : self.image_temp_service_url + itemImage,
'location' : { 'index' : len(itemtxt)-1 }}
})
item = list(reversed(options)) + item
self.questionKey.append(indexAnswer)
return item
def submitItem(self, index, item):
super().submitItem()
for _item in item :
submition = {}
submition["requests"] = _item
print(submition)
r = self.service.documents().batchUpdate(documentId=self.main_docs["documentId"], body=submition).execute()
print(r)
time.sleep(.8)
def update(self):
super().update()
# Prints the result to show the question has been added
state = self.service.documents().get(documentId=self.main_docs["documentId"]).execute()
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)