1. 程式人生 > >python3 通過api獲取oracle REST API for Oracle Eloqua Marketing Cloud Service 資料

python3 通過api獲取oracle REST API for Oracle Eloqua Marketing Cloud Service 資料

通過REST API for Oracle Eloqua Marketing Cloud Service 獲取資料,
驗證方式是Authenticate using HTTP Basic Authentication驗證方式

import base64
import requests
import json
import psycopg2

# 用來操作資料庫的類
class GPCommand(object):
    # 類的初始化
    def __init__(self):
      self.hostname = 'xxxxxx'
      self.username = 'xxxx'
      self.password = 'xxxxx'
      self.database = 'xxxx'
    def connectGp(self):
      try:
        #連結資料庫
        #讀取配置利用connect連結資料庫
        self.connect = psycopg2.connect( host=self.hostname, user=self.username, password=self.password, dbname=self.database )
        #建立一個新的cursor
        self.cursor = self.connect.cursor()
        print("connect gp successful."+'\n' + '資料庫連線成功')
        return ('con_successful')
      except psycopg2.Error:
          error = 'Failed to setup Postgres environment.\n{0}'.format(sys.exc_info())
          print('connect gp error.'+'\n' + '資料庫連線失敗')
          return 'con_error'+ error
#關閉資料庫
    def closeMysql(self):
        self.cursor.close()
        self.connect.close()
        print("資料庫已關閉")

    def insert_data(self,table_name,dict): #_key,dict_value
        try:
            dicts_values = []
            # print(dict)
            dict_key = list(dict.keys())
            table_key = ",".join(dict_key)
            ROWstr = ''  # 行欄位
            for key in dict.keys():
                ROWstr = (ROWstr + "'%s'" + ',') % (escape_character(dict[key]))
            insert_sql = "insert into %s (%s) values (%s)"\
                         %(table_name,table_key,ROWstr[:-1])
            # print(insert_sql)
            self.cursor.execute(insert_sql)
            self.connect.commit()
        except Exception as e:
            print(e)


def escape_character(string):
    script = ''
    if type(string) == type([]):
        script_tmp = ",".join(string)
    elif type(string) == type({}):
        script_tmp = str(string)
    else:
        script_tmp = string
    if "'" in script_tmp:
        nops = []
        new_loop = []
        itemplist = list(script_tmp)
        for i in range(len(itemplist)):
            if itemplist[i] == "'":
                nops.append(i)
        for item in nops:
            new_loop.append(item + nops.index(item))
        for i in new_loop:
            itemplist.insert(i, "'")
        script = "".join(itemplist)
    else:
        script = script_tmp
    return  script

config_data = [
    #{'table_name':'public.accounts_test10','get_url':'/api/REST/1.0/data/accounts','modify_date':'lastUpdatedAt'},
    {'table_name':'public.campaigns_test20','get_url':'/api/REST/2.0/assets/campaigns','modify_date':'lastUpdatedAt'},
    {'table_name':'public.contact_fields_test10','get_url':'/api/REST/1.0/assets/contact/fields','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.contact_lists_test10','get_url':'/api/REST/1.0/assets/contact/lists','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.contact_segments_test10','get_url':'/api/REST/1.0/assets/contact/segments','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.contact_test10','get_url':'/api/REST/1.0/data/contacts','modify_date':'lastUpdatedAt'} --42w 未全匯入
    #{'table_name': 'public.content_sections_test10', 'get_url': '/api/REST/1.0/assets/contentSections','modify_date': 'lastUpdatedAt'}
    #{'table_name':'public.custom_object_test10','get_url':'/api/REST/1.0/assets/customObjects','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.custom_object_data_test10','get_url':'/api/REST/1.0/data/customObject/8','modify_date':'lastUpdatedAt'} -- 需要id去獲取,但是id是多少目前不知道custom object data2.0相同
    #{'table_name':'public.email_folders_test10','get_url':'/api/REST/1.0/assets/email/folders','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.email_footers_test10','get_url':'/api/REST/1.0/assets/email/footers','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.email_groups_test10','get_url':'/api/REST/1.0/assets/email/groups','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.email_headers_test10','get_url':'/api/REST/1.0/assets/email/headers','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.emails_test10','get_url':'/api/REST/1.0/assets/emails','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.forms_test10','get_url':'/api/REST/1.0/assets/forms','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.images_test10','get_url':'/api/REST/1.0/assets/images','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.landing_pages_test10','get_url':'/api/REST/1.0/assets/landingPages','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.microsites_test10','get_url':'/api/REST/1.0/assets/microsites','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.option_lists_test10','get_url':'/api/REST/1.0/assets/optionLists','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.users_test10','get_url':'/api/REST/1.0/system/users','modify_date':'lastUpdatedAt'}

    #{'table_name':'public.account_groups_test20','get_url':'/api/REST/2.0/assets/account/groups','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.account_test20','get_url':'/api/REST/2.0/assets/account/groups','modify_date':'lastUpdatedAt'} -- 採用的是post,必須提供ids 故先不提取 contact20相同
    #{'table_name':'public.campaigns_test20','get_url':'/api/REST/2.0/assets/campaigns','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.contact_segments_test20','get_url':'/api/REST/2.0/assets/contact/segments','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.custom_objects_test20','get_url':'/api/REST/2.0/assets/customObjects','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.emails_test20','get_url':'/api/REST/2.0/assets/emails','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.events_test20','get_url':'/api/REST/2.0/assets/eventRegistrations','modify_date':'lastUpdatedAt'}
    #external activity需要id
    #{'table_name':'public.external_asset_types_test20','get_url':'/api/REST/2.0/assets/external/types','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.external_assets_test20','get_url':'/api/REST/2.0/assets/externals','modify_date':'lastUpdatedAt'} -- 沒有資料
    #{'table_name':'public.programs_test20','get_url':'/api/REST/2.0/assets/programs','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.visitor_profile_fields_test20','get_url':'/api/REST/2.0/assets/visitor/fields','modify_date':'lastUpdatedAt'}
    #{'table_name':'public.visitor_data_test20','get_url':'/api/REST/2.0/data/visitors','modify_date':'lastUpdatedAt'}
]


for i in range(0 ,len(config_data)):
    gpCommand = GPCommand()
    gpCommand.connectGp()
    result_data=[]
    url = 'https://login.eloqua.com/id'
    str_encrypt= 'xxxxx\xxxxx:xxxx'
    base64_encrypt = str(base64.b64encode(str_encrypt.encode('utf-8')),'utf-8')
    Authorization = "Basic %s" % base64_encrypt
    headers = { 'Authorization' : Authorization }
    r = requests.get(url,headers=headers)
    base_url = 'https://secure.p04.eloqua.com'+config_data[i]['get_url']+'?page=1&'+config_data[i]['modify_date']+'>=1540194812'
    baseheaders = { 'Authorization' : Authorization,'Accept':'application/json','Content-Type':'application/json'}
    br = requests.get(base_url,headers=baseheaders)
    br_result = json.loads(br.content.decode())
    result_data.append(br_result['elements'])
    print(br_result['total'])
    page_size = 3 #br_result['total']//1000 + 2


    for item in range(2,page_size):
        page_url = 'https://secure.p04.eloqua.com'+config_data[i]['get_url']+'?page=' + str(item) +'&'+config_data[i]['modify_date']+'>=1540194812'
        br_page = requests.get(page_url, headers=baseheaders)
        br_page_result = json.loads(br_page.content.decode())
        result_data.append(br_page_result['elements'])
    result_data_temp = []
    for item in result_data:
        for it in item:
            result_data_temp.append(it)

    print(result_data_temp)
    #
    for item in result_data_temp:
        gpCommand.insert_data(config_data[i]['table_name'],item)



    gpCommand.closeMysql()