通过Python创建Zendesk文章

时间:2018-06-28 12:53:47

标签: python-3.x zendesk zendesk-api

我是Zendesk管理员,我正在尝试从我们的一个帮助中心克隆所有帮助中心文章,然后将其移至另一个。如果我通过复制/粘贴手动进行操作,这将是一个非常丑陋的过程,因此我正在尝试使用Zendesk API。我一直按照https://develop.zendesk.com/hc/en-us/articles/360001074288-Zendesk-REST-API-tutorial-Backing-up-your-knowledge-base-with-Python的步骤进行操作,这使我能够删除所有文章,将它们备份到一组不错的html文件中,但是最终我要做的是数据,然后将它们上传到新的Zendesk实例中。

所以我知道这些步骤需要一个不同的端点,即:https://developer.zendesk.com/rest_api/docs/help_center/articles#create-article,然后是:/api/v2/help_center/en-us/articles.json

如果我要旁载,我很确定我需要做,因为我需要添加部分和作者来创建文章,我认为它看起来像这样: /api/v2/help_center/{locale}/sections/{id}/articles.jsoninclude=sections,用户,附件'.format(locale = language.lower())

我的麻烦是,我不确定如何或在何处添加文章create语句。它应该进入提取数据的循环吗?还是应该将其添加为单独的语句?另外,我打算在提取数据时执行此操作,因此应该将json数据提取到内存中,组织成变量,使用内存中的数据创建新文章,然后将数据备份到html中文件,然后循环到下一个。

到目前为止,我的代码已登录并检查了内容。我是个新手,所以如果这看起来很明显,我很抱歉。

# Begin imports
import requests
import os
import datetime
import csv
# Resolve imports

# Login variables
credentialskey = 'emailgoeshere', 'apikey'
zendesk = 'zendeskurl'
language = 'en-us'
# End Login variables

# Setup for backup folder structure. Set date, backup_path variable to folders. If no folders detected, create new ones
date = datetime.date.today()
backup_path = os.path.join(str(date), language)
if not os.path.exists(backup_path):
    os.makedirs(backup_path)

# Create log tuple for CSV log creation
log =[]

# Set endpoint for extraction.
# https://develop.zendesk.com/hc/en-us/articles/360001074288
# This only calls for /articles.json but we need to sideload for users/sections to import later
endpoint = zendesk + '/api/v2/help_center/en-us/articles.json?include=sections,users,attachments'.format(locale=language.lower())
while endpoint:
    response = requests.get(endpoint, auth=credentials)
    if response.status_code != 200:
        print('Failed to retrieve articles with error {}'.format(response.status_code))
        exit()
    data = response.json()
# Data extraction complete, data is in json form. Will error if we get anything other than response 200

# Extraction Loop. For article in json data, if article has a body, continue. Title variable is setting the title
# Takes the title of the article, sets that as h1 in the html file it creates, sets the filename as the article id
# Then opens the folder for each, creates the file with the body.
    for article in data['articles']:
        if article['body'] is None:
            continue

            locale=language.lower())
        title = '<h1>' + article['title'] + '</h1>'
        filename = '{id}.html'.format(id=article['id'])
        with open(os.path.join(backup_path, filename), mode='w', encoding='utf-8') as f:
            f.write(title + '\n' +article['body'])
        print('{id} copied!'.format(id=article['id']))

# Not sure what log.append is doing. Adding it to the html file?
        log.append((filename, article['title'], article['author_id'], article['section_id'], article['body']))


# Next line iterates through the pages of json file
    endpoint = data['next_page']

# This section opens the csv _log and writes to it
with open(os.path.join(backup_path, '_log.csv'), mode='wt', encoding='utf-8') as f:
    writer = csv.writer(f)
    writer.writerow( ('File', 'Title', 'Author Id', 'Section Id', 'Body') )
    for article in log:
        writer.writerow(article)


# Need a section that opens the correct endpoint
# Also needs to connect to other help center
# Change response = requests.get to requests.put
# Should look like
# secondhelpcentercreds = 'emailcreds', 'apikey'
# secondhelpcenter_zendesk = 'zendeskurl'
# locale = en-us
# id = article['id']
# id = article['id']
# locale = en-us
# endpoint_put = secondhelpcenter_zendesk + '/api/v2/help_center/{locale}/sections/{id}/articles.jsoninclude=sections,users,attachments'.format(locale=language.lower())

# while # endpoint_put:
#     response = requests.put(endpoint, auth=credentials)
#     if response.status_code != 200:
#         print('Failed to retrieve articles with error {}'.format(response.status_code))
#         exit()

# Unsolved questions - How do I connect to both of these? Where in the loop do I need to put the, er, put

0 个答案:

没有答案