ServiceNowでレコード情報を取得し、XMLに出力する方法(Not RestAPI)

## したいこと
RestAPI Explolerによらず、自由に拡張できるテーブル情報取得のスクリプトやバッチが欲しい。
Pythonで書いてみた。


## ソースコード

```python:GetXML.py
import datetime, os, json, time, difflib
import argparse, textfile
from selenium import webdriver

#webdriverの設定
options = webdriver.ChromeOptions()
driver_path = 'D:/Users/chromedriver.exe'
driver = webdriver.Chrome(executable_path = driver_path, chrome_options = options)

#ID/PasswordをJSONの読込
conf_open = open('config.json', 'r')
conf_load = json.load(conf_open)

#Table/Queryを指定したJSONの読込
json_open = open('query.json', 'r',encoding="utf-8")
json_load = json.load(json_open)

#Basicなuriを生成
base1 = 'https://' + conf_load['userid'] + ':' + conf_load['password'] + '@' + conf_load['dev']
base2 = '_list.do?XML&'


#XMLを一括して出力
def getXML():
    now = datetime.datetime.now()
    dirname = os.path.dirname(__file__)
    if os.path.exists("output") == False:
        os.mkdir("output")
    path = os.path.join(dirname,'output/')
    filename =  now.strftime('%Y%m%d_%H%M%S') + 'GetRecord.xml'
    file = path + filename
    f =  open(file, 'a',encoding="utf-8")
    for v in json_load.values():
        table = v['table']
        query = v['query']
        description = v['description']
        XML_path = base1 + table + base2 + query
        driver.get(XML_path)
        text1 = driver.find_element_by_class_name("folder").get_attribute("textContent")
        f.write('====TableName  : ' + table + ' Start====\n')
        f.write('====Query      : ' + query + ' ====\n')
        f.write('====Description: ' + description + ' ====\n')
        f.write('\n')
        f.write(text1)
        f.write('\n')
        f.write('==== End ====\n')
        f.write('\n')
        time.sleep(1)
    f.close()
    textfile.replace(file, '...<', '<')    
    

#指定テーブルのDiff出力        
def getTableDiff():
    pass
    #if (v['table'] == args.diff):みたいにしぼってdifflibを使えばよい
#指定テーブルのみ出力
def getSiteiTable():
    now = datetime.datetime.now()
    dirname = os.path.dirname(__file__)
    if os.path.exists("output") == False:
        os.mkdir("output")
    path = os.path.join(dirname,'output/')
    for v in json_load.values():    
        if (v['table'] == args.sitei):
            table = v['table']
            query = v['query']
            filename =  now.strftime('%Y%M%d_%H%M%S') + v['table'] +'.xml'
            file = path + filename
            f =  open(file, 'a',encoding="utf-8")
            XML_path = base1 + table + base2 + query
            driver.get(XML_path)
            text1 = driver.find_element_by_class_name("folder").get_attribute("textContent")
            f.write('====TableName: ' + table + ' Start====\n')
            f.write('====Query: ' + query + ' Start====\n')
            f.write('\n')
            f.write(text1)
            f.write('\n')
            f.write('==== End ====\n')
            f.write('\n')
            f.close()
            textfile.replace(file, '...<', '<')  
            break

#引数で動作を変更
#全レコード取得/特定テーブル取得/Diff取得
parser = argparse.ArgumentParser(description='パーサー')
parser.add_argument('-s','--sitei',help='指定取得')
parser.add_argument('-d','--diff',help='Diff取得')
args = parser.parse_args()

if args.diff:
    print('Diff出力')
    getTableDiff()
elif args.sitei:
    print('指定テーブル出力')
    getSiteiTable()
else:
    print('XML出力')
    getXML()
```