Beispiel #1
0
def execute(args, options):
    login = args['--login']

    file_path = check_file(args)

    csv_reader = CsvReader(file_path)
    csv_reader.open()

    game_count = csv_reader.rowCount

    if not args['--force']:
        sys.stdout.write(
            "You are about to delete %s games in you collection (%s), "
            "please enter the number of games displayed here to confirm you want to continue: "
            % (game_count, login))

        if raw_input() != game_count.__str__():
            Logger.error(
                'Operation canceled, number does not match (should be %s).' %
                game_count,
                sysexit=True)
            return

    Logger.info("Deleting games for '%s' account..." % login)

    with WebDriver('collection-delete', args, options) as web_driver:
        if not LoginPage(web_driver.driver).authenticate(
                login, args['--password']):
            sys.exit(1)

        Logger.info("Deleting %s games..." % game_count)
        game_page = GamePage(web_driver.driver)
        csv_reader.iterate(lambda row: game_page.delete(row))
        Logger.info("Deletion has finished.")
Beispiel #2
0
def execute(args, options):
    login = args['--login']
    dest_path = args['<file>']

    Logger.info("Exporting collection for '%s' account..." % login)

    # 1. Authentication
    with WebDriver('collection-export', args, options) as web_driver:
        if not LoginPage(web_driver.driver).authenticate(
                login, args['--password']):
            sys.exit(1)
        auth_cookie = web_driver.driver.get_cookie(
            BGG_SESSION_COOKIE_NAME)['value']

    # 2. Export
    # Easier to rely on a client HTTP call rather than Selenium to download a file
    # Just need to pass the session cookie to get the full export with private information

    # Use XML2 API, see https://www.boardgamegeek.com/wiki/page/BGG_XML_API2#Collection
    # Default CSV export doesn't provide version info!
    url = '%s/xmlapi2/collection?username=%s&version=1&showprivate=1&stats=1' \
          % (BGG_BASE_URL, login)
    req = urllib2.Request(
        url, None,
        {'Cookie': '%s=%s' % (BGG_SESSION_COOKIE_NAME, auth_cookie)})

    # Get a BadStatusLine error most of times without this delay!
    # Related to Selenium, but in some conditions that I have not identified
    time.sleep(8)
    try:
        Logger.info('Launching export...')
        response = default_export(req)
    except Exception as e:
        Logger.error('Error while fetching export file!', e, sysexit=True)
        return

    # 3. Store XML file if requested
    xml_file = options.get('save-xml-file')
    if xml_file == 'true':
        xml_file_path = write_xml_file(response, dest_path)
        Logger.info("XML file save as %s" % xml_file_path)
        source = open(xml_file_path, 'rU')
    else:
        source = response

    # 4. Write CSV file
    try:
        write_csv(source, dest_path)
    except Exception as e:
        Logger.error('Error while writing export file in file system!',
                     e,
                     sysexit=True)
        return
    finally:
        source.close()

    # End
    Logger.info("Collection has been exported as %s" % dest_path)
Beispiel #3
0
def test_collection(tmpdir):
    """
    End-2-end test to verify collection-delete/export/import services
    """

    debug_test()
    debug_test("End-to-end test is executed in %s" % tmpdir)

    #
    debug_test()
    debug_test("1. Ensure the collection is empty")
    with WebDriver('test_collection-empty') as web_driver:
        if not CollectionPage(web_driver.driver).is_empty(LOGIN):
            try:
                _main([
                    '-v', '--login', LOGIN, '--password', PASSWORD,
                    'collection-delete', '--force', COLLECTION_CSV_PATH
                ])
            except BaseException as e:
                assert False, "Delete command should not fail: %s" % e

            assert CollectionPage(web_driver.driver).is_empty(
                LOGIN), 'Collection should be empty!'
            debug_test("-> [ok (collection was not empty)]")
        else:
            debug_test("-> [ok (collection was empty)]")

    debug_test()
    debug_test("2. Import collection")
    _main([
        '-v', '--login', LOGIN, '--password', PASSWORD, 'collection-import',
        COLLECTION_CSV_PATH
    ])
    debug_test("-> [ok]")

    #
    debug_test()
    debug_test("3. Export collection as CSV")
    actual_file = tmpdir.join('actual-collection.csv').strpath
    assert not os.path.exists(actual_file)
    _main([
        '-v', '--login', LOGIN, '--password', PASSWORD, 'collection-export',
        actual_file
    ])
    assert os.path.isfile(actual_file)
    debug_test("-> [ok]")

    #
    debug_test()
    debug_test("4. Compare CSV files")
    compare_csv_files(actual_file, COLLECTION_CSV_PATH)
    debug_test("Comparison OK!")

    debug_test()
    debug_test("End-to-end test on collection commands is successful!")
Beispiel #4
0
def execute(args, options):
    login = args['--login']

    file_path = check_file(args)

    csv_reader = CsvReader(file_path)
    csv_reader.open()

    Logger.info("Importing games for '%s' account..." % login)

    with WebDriver('collection-import', args, options) as web_driver:
        if not LoginPage(web_driver.driver).authenticate(
                login, args['--password']):
            sys.exit(1)

        Logger.info("Importing %s games..." % csv_reader.rowCount)
        game_page = GamePage(web_driver.driver)
        csv_reader.iterate(lambda row: game_page.update(row))
        Logger.info("Import has finished.")
def execute(args, options):
    print('Executing!')
    login = args['--login']

    file_path = check_file(args)

    csv_reader = CsvReader(file_path)
    csv_reader.open()
    rows = []
    #    try:
    Logger.info("Parsing input file '{}'...".format(file_path))
    csv_reader.iterate(lambda row: rows.append(row))
    #Logger.info("Found %s games to put in collection..." % csv_reader.rowCount)
    rows.reverse()
    firstrow = rows[0]
    loop = 0

    Logger.info("Importing {} games to collection of '{}' ...".format(
        csv_reader.rowCount, login))
    while rows:
        try:
            with WebDriver('collection-import', args, options) as web_driver:
                if not LoginPage(web_driver.driver).authenticate(
                        login, args['--password']):
                    sys.exit(1)
                #input("Kill Firefox, then Press Enter to continue...")
                game_page = GamePage(web_driver.driver)
                while rows:
                    row = rows.pop()
                    if firstrow is None or firstrow == row:
                        loop += 1
                        if loop >= LOOPLIMIT:
                            Logger.info(
                                "Loop limit of {} reached.".format(loop))
                            return
                        Logger.info('Loop {} (maximum {})'.format(
                            loop, LOOPLIMIT))
                        if rows:
                            firstrow = rows[0]
                            Logger.info('First assigned {}'.format(
                                firstrow['objectname']))
                        else:
                            firstrow = None
                            Logger.info('First assigned None')

                    Logger.info('(BGGID {}) Name: {} ({} game left)'.format(
                        row['objectid'], row['objectname'],
                        len(rows) + 1))

                    try:
                        val = game_page.update(row)
                        Logger.info('update returned {}'.format(val))

                        if val:
                            #Logger.info('Updated (BGGID {0}) "{1}"'.format(row['objectid'],row['objectname']))
                            Logger.info('(BGGID {}) Name: {} UPDATED!'.format(
                                row['objectid'], row['objectname'], len(rows)))
                            #  ({} game left)
                        else:
                            rows.insert(0, row)
                            Logger.info(
                                'returned False??, back in queue.'.format(
                                    len(rows)))  #  ({} game left)

                    except WebDriverException:
                        rows.insert(0, row)
                        Logger.info(
                            'Exception occurred, back in queue.'.format(
                                len(rows)))  # ({} left)
                        Logger.info(
                            'WebDriverException occurred, restarting browser.')
                        raise

                    except Exception as e:
                        traceback.print_exc(limit=2, file=sys.stdout)

                        rows.insert(0, row)
                        Logger.info(
                            'Exception occurred, back in queue.'.format(
                                len(rows)))  #  ({} left)

                        #badrows.append(row)
                # for row in rows:
                # try:
                # game_page.update(row)
                # except:
                # badrows.append(row)
                # print
        except WebDriverException:
            pass
    Logger.info("Import has finished.")