当前位置: 首页>>代码示例>>Python>>正文


Python RequestObject.URL方法代码示例

本文整理汇总了Python中RequestObject.RequestObject.URL方法的典型用法代码示例。如果您正苦于以下问题:Python RequestObject.URL方法的具体用法?Python RequestObject.URL怎么用?Python RequestObject.URL使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在RequestObject.RequestObject的用法示例。


在下文中一共展示了RequestObject.URL方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from RequestObject import RequestObject [as 别名]
# 或者: from RequestObject.RequestObject import URL [as 别名]
def main(argv):
    """Main method that takes in user input, creates a RequestObject, and
    then creates/calls the AnalyticAgent to retrieve and save the page of
    data from thre report. 
    """

    # grab info to see how the input is coming in. If by pipe or file
    # redirect, we will echo the inputted values to the screen
    mode = os.fstat(0).st_mode
    echoInput = stat.S_ISFIFO(mode) or stat.S_ISREG(mode)
    
    if len(argv) != 1: # command
        print_help()
        sys.exit(2)

    print(u"Grab a Page of Data from an Analytic Report\n")

    # input the parameters
    url = raw_input(u"Alma resource URL:\n> ").strip()
    if echoInput: print(url)

    path = raw_input(u"Analytic path:\n> ").strip()
    if echoInput: print(path)

    apikey = raw_input(u"API key:\n> ").strip()
    if echoInput: print(apikey)

    limit = 0
    while not (AnalyticAgent.LIMIT_LOWER <= limit <= AnalyticAgent.LIMIT_UPPER):
        limit = raw_input(u"Number of results to return (25 - 1000):\n> ").strip()
        if echoInput: print(limit)
        limit = int(limit)
    # round up to nearest multiple of 25
    old_limit = limit
    limit = int(math.ceil(limit/25.0)) * 25
    if limit != old_limit:
        print("Limit rounded up to nearest multiple of 25: %d --> %d" \
              % (old_limit, limit))
              


    filename = raw_input(u"Save data to file:\n> ")
    if echoInput: print(filename)

    # create a simple Request
    request = RequestObject()
    request.Simple = True
    request.URL = url
    request.Paths.append(path)
    request.Keys.append(apikey)

    # unneeded check, but just to show that this is enough for a simple request
    assert request.validate(simpleRequest=True)

    # Create and run the agent
    print()
    print(u"Grabbing Analytic data...")
    try:
        agt = AnalyticAgent.loadRequest(request)

        # writer: where the data will be saved
        writer = codecs.open(filename, 'w', encoding='utf-8')

        # output any progress/log messages to standard out
        logger = [sys.stdout]

        # Tell the agent to run with a name of Agent and perform a
        # query of type PAGE.
        # Note that we leave out the path and apikey parameters since
        # the agent will pull them from the request object.
        n = agt.run(jobName="Agent", writer=writer, logger=logger,
                    limit=limit, queryType=QueryType.PAGE)

        print(u"Page collected. " + unicode(n) + u" records collected.")
        sys.exit(0)
        
    except AnalyticServerError as e:
        # Exception if the query is malformed or server cannot be reached
        print(u"Agent> failed due to server error:\n"
              + unicode(e) )
        writer.close()
        # clean up the incomplete outfile
        if os.path.isfile(filename):
            os.remove(filename)
        sys.exit(1)
        
    except ZeroResultsError as e:
        # See CustomExceptions for description of this error
        print(u"Agent> failed due to zero results error" )
        outfile.close()                    
        # clean up the incomplete outfile
        if os.path.isfile(filename):
            os.remove(filename)
        sys.exit(1)
开发者ID:NEU-Libraries,项目名称:alma-analytic-tools,代码行数:96,代码来源:page_download.py


注:本文中的RequestObject.RequestObject.URL方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。