Last active
February 11, 2024 22:23
-
-
Save heukirne/ded385438491b6f3ef6758ddc18ca2de to your computer and use it in GitHub Desktop.
Script to migrate data from Octadesk to Odoo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xmlrpc.client | |
import requests, json, os, time | |
## Octadesk API: https://api-docs.octadesk.services/docs/ | |
payload = {'username': '[email protected]', 'password': 'password'} | |
headers = {'subDomain': 'company', 'content-type': 'application/json', 'accept': 'application/json'} | |
r = requests.post('https://api.octadesk.services/login', data=json.dumps(payload), headers=headers) | |
token = r.json()['token'] | |
headers['Authorization'] = 'Bearer ' + token | |
## Odoo API: https://www.odoo.com/documentation/17.0/developer/reference/external_api.html | |
apiUrl = 'https://company.odoo.com/xmlrpc/2/' | |
apiDB = 'company' | |
apiUser = '[email protected]' | |
apiKey = 'apikey' | |
common = xmlrpc.client.ServerProxy(apiUrl+'common') | |
uid = common.authenticate(apiDB, apiUser, apiKey, {}) | |
models = xmlrpc.client.ServerProxy(apiUrl+'object') | |
## Add Organization | |
for p in range(1,10): | |
tReq = requests.get('https://api.octadesk.services/organization?page='+str(p), headers=headers) | |
for c in tReq.json(): | |
company = models.execute_kw(apiDB, uid, apiKey, 'res.partner', 'web_search_read', [[['name','=',r['name']]]], {'specification': {}}) | |
if company['length'] == 1: continue | |
print(c['name'], c['domains'][0]) | |
company = { | |
'name': c['name'], | |
'type': 'contact', | |
'company_type': 'company', | |
'website': c['domains'][0], | |
'lang': 'pt_BR' | |
} | |
models.execute_kw(apiDB, uid, apiKey, 'res.partner', 'web_save', [[], company], {'specification': {}}) | |
## Add Requesters | |
for p in range(2,50): | |
tReq = requests.get('https://api.octadesk.services/persons/requesters?page='+str(p), headers=headers) | |
if len(tReq.json()) == 0: break | |
print('########## Page',p) | |
for r in tReq.json(): | |
time.sleep(1) | |
print('Search', r['name']) | |
person = models.execute_kw(apiDB, uid, apiKey, 'res.partner', 'web_search_read', [[['email','=',r['email']]]], {'specification': {}}) | |
if person['length'] == 1: continue | |
parent_id = False | |
if 'organization' in r: | |
company = models.execute_kw(apiDB, uid, apiKey, 'res.partner', 'web_search_read', [[['name','=',r['organization']['name']]]], {'specification': {}}) | |
if company['length'] == 1: | |
parent_id = company['records'][0]['id'] | |
person = { | |
'name': r['name'], | |
'type': 'contact', | |
'company_type': 'person', | |
'email': r['email'], | |
'lang': 'pt_BR', | |
'parent_id': parent_id | |
} | |
models.execute_kw(apiDB, uid, apiKey, 'res.partner', 'web_save', [[], person], {'specification': {}}) | |
## Add Tickets | |
for p in range(2,50): | |
tReq = requests.get('https://api.octadesk.services/tickets/search?status=Resolvido&sortBy=open_date&sortDirection=asc&page='+str(p), headers=headers) | |
for t in tReq.json(): | |
if t['number'] < 100: continue | |
if not 'lastPersonUpdate' in t or not 'organizationName' in t \ | |
or t['organizationName'] == 'Company' or t['totalHumanInteractions'] == 0 \ | |
or t['totalHumanInteractions'] > 40: continue | |
lastEmail = t['lastPersonUpdate']['email'] | |
eTicket = models.execute_kw(apiDB, uid, apiKey, 'helpdesk.ticket', 'web_search_read', [[['x_studio_nmero_octa','=',t['number']]]], {'specification': {}}) | |
if eTicket['length'] == 1: continue | |
partner = models.execute_kw(apiDB, uid, apiKey, 'res.partner', 'web_search_read', [[['email','=',t['requesterMail']]]], {'specification': {'name':{},'email':{}}}) | |
if partner['length'] != 1: continue | |
print('Solicitante: ', partner) | |
partner_id = partner['records'][0]['id'] | |
user = models.execute_kw(apiDB, uid, apiKey, 'res.users', 'web_search_read', [[['email','=',lastEmail]]], {'specification': {'name':{},'email':{}}}) | |
if user['length'] != 1: continue | |
print('Responsável: ', user) | |
user_id = user['records'][0]['id'] | |
team_id = 1 | |
print('Buscando interações de ',t['number']) | |
iReq = requests.get('https://api.octadesk.services/tickets/'+ str(t['number']) +'/interactions', headers=headers) | |
summary = summarizeTicket(tReq, iReq) | |
ticket = { | |
'name': t['summary'] + ' #' + str(t['number']), | |
'user_id': user_id, # temporaty turn off email notification to avoid spam | |
'partner_id': partner_id, | |
'stage_id': 4, | |
'description': t['descriptionAsText'].replace('\n','<br>') | |
'team_id': team_id, | |
'x_studio_nmero_octa': t['number'], | |
'x_studio_data_octa': t['openDate'].translate({84:32,90:None}).split('.')[0] | |
} | |
print('Inserindo Ticket ',t['number'],': ',t['summary']) | |
odooT = models.execute_kw(apiDB, uid, apiKey, 'helpdesk.ticket', 'web_save', [[], ticket], {'specification': {}}) | |
if odooT[0] and odooT[0]['id']: | |
for i in iReq.json(): | |
if i['isHumanInteraction'] == True and len(i['comments']) > 0: | |
iPartner = models.execute_kw(apiDB, uid, apiKey, 'res.partner', 'web_search_read', [[['email','=',i['person']['email']]]], {'specification': {'email':{}}}) | |
if iPartner['length'] != 1: continue | |
print('Inserindo comentário de ', iPartner['records'][0]['email']) | |
iData = i['dateCreation'].translate({84:32,90:None}).split('.')[0] | |
models.execute_kw(apiDB, uid, apiKey,'mail.message','create', | |
[{ | |
'message_type': 'comment', | |
'body': '<strong>' + iPartner['records'][0]['email'] + ' disse</strong> em ' + iData + ':<br>' + i['comments'][0]['content'], | |
'model': 'helpdesk.ticket', | |
'res_id': odooT[0]['id'], | |
'subtype_id': 2 | |
}] | |
) | |
## give a break for Oddo Api to avoid too many requests | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment