sistem_robotik_old/SOURCE/migration_gdrive_link.py

65 lines
1.9 KiB
Python

# coding: utf-8
from __future__ import unicode_literals
import sys, os
import requests
import importlib
# to run separatly from soffice
# $ soffice --calc --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"
sys.path.append("/lib64/libreoffice/program/")
from scriptforge import ScriptForge, CreateScriptService
from unohelper import fileUrlToSystemPath
sys.path.insert(0, '{}/{}'.format(os.getcwd(),'Library'))
ScriptForge(hostname='localhost', port=2002)
from gquiz import gquiz
ui = CreateScriptService("UI")
doc = CreateScriptService("Calc")
bas = CreateScriptService("Basic")
q = gquiz()
q.generateService()
def get_file_id_from_link(link):
# Find the starting index of the file ID
start_index = -1
if "/d/" in link:
start_index = link.index("/d/") + 3
elif "id=" in link:
start_index = link.index("id=") + 3
elif "rs/" in link:
start_index = link.index("rs/") + 3
if start_index != -1:
# Find the ending index of the file ID
end_index = link.index("/", start_index)
if end_index == -1:
# If there is no trailing "/", use the end of the string
end_index = len(link)
# Extract the file ID
file_id = link[start_index:end_index]
return file_id
else:
# Return None if no file ID is found
return None
cells = doc.getValue("")
for col in range(0, len(cells)):
for row in range(0, len(cells[0])):
if(str(cells[col][row]).startswith("https://")):
print("found the culprit!!")
url = cells[col][row]
print(url)
Id=get_file_id_from_link(url)
file = q.drive_service.files().get(fileId=Id).execute()
newCell = "/asset/{newname}".format(newname=file["name"])
print("{new} <- {old}".format(new=newCell, old=cells[col][row]))
doc.setValue(doc.Offset("A1",col,row),newCell)