0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ServiceNow - KnowledgeBase記事の添付ファイルをダウンロードするPythonスクリプト

Posted at

概要

ServiceNowのAPIを利用して指定したKnowledeBase記事のすべての添付ファイルをダウンロードするPython3.7スクリプトです。

import requests

from servicenowapis.constants import ATTACHMENT_API, DOWNLOAD_DIR, REQUEST_HEADER,\
    SERVICENOW_PWD, SERVICENOW_URL,SERVICENOW_USER, TABLE_API


def get_table_data(table_name, param):
    url = SERVICENOW_URL + '/' + TABLE_API + '/' + table_name + '?' + param
    response = requests.get(url, auth=(SERVICENOW_USER, SERVICENOW_PWD), headers=REQUEST_HEADER.get('json'))
    if response.status_code != 200: # if error, then exit
        print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:', response.json())
        exit()
    return response.json()


def get_attachment_info(sys_id):
    url = SERVICENOW_URL + ATTACHMENT_API + '/' + sys_id
    response = requests.get(url, auth=(SERVICENOW_USER, SERVICENOW_PWD), headers=REQUEST_HEADER.get('json'))
    if response.status_code != 200:  # if error, then exit
        print(f'Status:  {response.status_code}, Headers:{response.headers}')
        exit()
    return response.json()


def get_attachment(att_sys_id, file_type, download_dir):
    url = SERVICENOW_URL + '/' + ATTACHMENT_API + '/' + att_sys_id + '/file'
    response = requests.get(url, auth=(SERVICENOW_USER, SERVICENOW_PWD), headers=REQUEST_HEADER.get(file_type))
    if response.status_code != 200:  # if error, then exit
        print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:', response.json())
        exit()
    with open(download_dir, 'wb') as f:
        for chunk in response:
            f.write(chunk)


def get_kb_attachment(kb_number):
    param = 'sysparm_query=number=' + kb_number + '&sysparam_limit=1'
    kb_info = get_table_data('kb_knowledge', param)
    kb_sys_id = kb_info.get('result')[0].get('sys_id')

    param = 'sysparm_query=table_sys_id=' + kb_sys_id + '&sysparam_limit=1'
    kb_attachments = get_table_data('sys_attachment', param)
    result = kb_attachments.get('result')
    for attach_file in result:
        attach_sys_id = attach_file.get('sys_id')
        content_type = attach_file.get('content_type').split('/')
        file_ext = content_type[1]
        file_name = attach_file.get('file_name')
        get_attachment(attach_sys_id, file_ext, DOWNLOAD_DIR + file_name)


if __name__ == '__main__':
    get_kb_attachment('<kb article number>')  # knowledge base number e.g.KB0010062

Constants.py

SERVICENOW_URL = 'https://<instance>.service-now.com'
SERVICENOW_USER = 'username'
SERVICENOW_PWD = 'password'

DOWNLOAD_DIR = 'C:/Users/hozawa/downloads/'

REQUEST_HEADER = {"json": {"Content-Type": "application/xml", "Accept": "application/json"},
                  "png": {"Content-Type": "application/xml", "Accept": "application/png"},
                  "xml": {"Content-Type": "application/xml", "Accept": "application/xml"},
                  }

ATTACHMENT_API = '/api/now/attachment'
TABLE_API = '/api/now/table'
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?