def test_album_tracks(HttpMock): client = init_client() album_id = '1c5f47572d9152f3' album_xml = ET.fromstring(textwrap.dedent("""<album featured="false" code="HM001" detail="Razor-sharp pop& rock bristling with spiky guitars & infectious, feelgood inspiration … and tons of attitude." name="HM 001 Sample Album" displaytitle="HM 001 Sample Album " id="%s"/> """ % (album_id))) return_values = [ (200, """<responsetracks> <tracks> <track tracknumber="1" time="02:50" lengthseconds="170" comment="Make sure you’re down the front for this fiery Post Punk workout." composer=""S. Milton, J. Wygens"" publisher="" name="Guerilla Pop" albumid="%(album_id)s" id="17376d36f309f18d" keywords="" displaytitle="Guerilla Pop" genre="Pop / Rock" tempo="" instrumentation="" bpm="" mixout="" frequency="44100" bitrate="1411" dateingested="2008-05-15 06:08:18"/> <track tracknumber="2" time="02:46" lengthseconds="166" comment="Poignant electric guitars. Brooding acoustic strums." composer=""S. Milton, J. Wygens"" publisher="" name="Hat And Feather" albumid="%(album_id)s" id="635f90a4db673855" keywords="" displaytitle="Hat And Feather" genre="Pop / Rock" tempo="" instrumentation="" bpm="" mixout="" frequency="44100" bitrate="1411" dateingested="2008-05-15 06:08:18"/> </tracks> </responsetracks>""" % locals()), ] http = build_http_mock(HttpMock, responses=return_values) album = Album._from_xml(album_xml, client) tracks = album.get_tracks(get_full_detail=False) assert tracks[0].albumid == album_id
def test_get_download_url(HttpMock): client = init_client() member_id = 'bacba2b288328238bcbac' track_format = 'mp3' track_id = '17376d36f309f18d' track_name = 'Guerilla Pop' track_xml = ET.fromstring( textwrap.dedent( """<track tracknumber="1" time="02:50" lengthseconds="170" comment="Make sure you’re down the front for this fiery Post Punk workout." composer=""S. Milton, J. Wygens"" publisher="" name="%(track_name)s" albumid="1c5f47572d9152f3" id="%(track_id)s" keywords="" displaytitle="Guerilla Pop" genre="Pop / Rock" tempo="" instrumentation="" bpm="" mixout="" frequency="44100" bitrate="1411" dateingested="2008-05-15 06:08:18"/>""" ) % locals()) track = Track._from_xml(track_xml, client) download_url = track.get_download_url(track_format, member_id) download_url_template = client.config.download_url format_identifier = client.config.get_format_identifier(track_format) expected_url = download_url_template.replace('{memberaccountid}', member_id).\ replace('{id}', track_id).\ replace('{trackformat}', format_identifier) assert download_url == expected_url, 'url: %s != expected: %s' % ( download_url, expected_url)
def test_get_library_albums(HttpMock): client = init_client() test_album_id = get_random_md5() return_values = [ (200, """<ResponseLibraries> <libraries> <library id="abc123" name="VIDEOHELPER" detail="Library description" /> <library id="abc125" name="MODULES" detail="Library description" /> </libraries> </ResponseLibraries>"""), (200, """<responsealbums> <albums> <album libraryid="abc123" featured="false" code="HM001" detail="Razor-sharp pop& rock bristling with spiky guitars & infectious, feelgood inspiration … and tons of attitude." name="HM 001 Sample Album" displaytitle="HM 001 Sample Album " id="%(test_album_id)s"/> <album libraryid="19b8f5935503adde" featured="false" code="HM002" detail=" Contemporary beats fused with orchestral elements." name="HM 002 Sample Album " displaytitle="HM 002 Sample Album " id="67a6aed83a741a06"/> </albums> </responsealbums>""" % {'test_album_id': test_album_id}), ] http = build_http_mock(HttpMock, responses=return_values) libraries = Library.query.get_libraries(client) assert isinstance(libraries, list) library = libraries[0] albums = library.get_albums() assert isinstance(albums, list) assert albums[0].id == test_album_id
def test_register_no_client(HttpMock): client = init_client() test_member_id = get_random_md5() username = '******' first_name = 'Test' last_name = 'User' email = '*****@*****.**' xml_response = """<?xml version="1.0" encoding="utf-8"?> <ResponseMember> <memberaccount id="%(test_member_id)s"> <username>%(username)s</username> <first_name>%(first_name)s</first_name> <last_name>%(last_name)s</last_name> <email>%(email)s</email> </memberaccount> </ResponseMember>""" % {'test_member_id': test_member_id, 'username': username, 'first_name': first_name, 'last_name': last_name, 'email': email} username = username first_name = first_name last_name = last_name email = email termsaccept = 'true' fileformat = 'MP3' http = build_http_mock(HttpMock, content=xml_response) member = Member.register(username=username, first_name=first_name, last_name=last_name, email=email, termsaccept=termsaccept, fileformat=fileformat)
def test_remove_track(HttpMock): client = init_client() test_member_id = get_random_md5() username = '******' firstname = 'Test' lastname = 'User' email = '*****@*****.**' xml_str = """<memberaccount id="%(test_member_id)s"> <username>%(username)s</username> <firstname>%(firstname)s</firstname> <lastname>%(lastname)s</lastname> <email>%(email)s</email> </memberaccount>""" % \ {'test_member_id': test_member_id, 'username': username, 'firstname': firstname, 'lastname': lastname, 'email': email} member_xml = ET.fromstring(xml_str) member = Member._from_xml(member_xml, client) xml_response = """<?xml version="1.0" encoding="utf-8"?> <responsecode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <code>OK</code> </responsecode>""" http = build_http_mock(HttpMock, content=xml_response) test_track_id = get_random_md5() member.remove_favourite(test_track_id)
def test_remove(HttpMock): client = init_client() test_member_id = get_random_md5() test_playlist_id = get_random_md5() test_playlist_name = 'test playlist' test_track_id = get_random_md5() return_values = [ (200, """<?xml version="1.0" encoding="utf-8"?> <ResponsePlaylists> <playlists> <playlist id="%(test_playlist_id)s" name="%(test_playlist_name)s"> <tracks> <track tracknumber="001" time="02:50" lengthseconds="170" comment="Make sure you’re down the front for this fiery Post Punk workout." composer=""S. Milton, J. Wygens"" publisher="HM" name="Guerilla Pop" id="%(test_track_id)s" keywords="" lyrics="" displaytitle="Guerilla Pop" genre="Pop / Rock" tempo="" instrumentation="" bpm="" mixout="" frequency="44100" bitrate="1411" /> </tracks> </playlist> </playlists> </ResponsePlaylists>""" % locals()), (200, """<?xml version="1.0" encoding="utf-8"?> <responsecode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <code>OK</code> </responsecode>""",), ] http = build_http_mock(HttpMock, responses=return_values) member = Member(_client=client) member.id = test_member_id playlists = member.get_playlists() playlists[0].remove()
def test_album_tracks(HttpMock): client = init_client() album_id = '1c5f47572d9152f3' album_xml = ET.fromstring( textwrap.dedent( """<album featured="false" code="HM001" detail="Razor-sharp pop& rock bristling with spiky guitars & infectious, feelgood inspiration … and tons of attitude." name="HM 001 Sample Album" displaytitle="HM 001 Sample Album " id="%s"/> """ % (album_id))) return_values = [ (200, """<responsetracks> <tracks> <track tracknumber="1" time="02:50" lengthseconds="170" comment="Make sure you’re down the front for this fiery Post Punk workout." composer=""S. Milton, J. Wygens"" publisher="" name="Guerilla Pop" albumid="%(album_id)s" id="17376d36f309f18d" keywords="" displaytitle="Guerilla Pop" genre="Pop / Rock" tempo="" instrumentation="" bpm="" mixout="" frequency="44100" bitrate="1411" dateingested="2008-05-15 06:08:18"/> <track tracknumber="2" time="02:46" lengthseconds="166" comment="Poignant electric guitars. Brooding acoustic strums." composer=""S. Milton, J. Wygens"" publisher="" name="Hat And Feather" albumid="%(album_id)s" id="635f90a4db673855" keywords="" displaytitle="Hat And Feather" genre="Pop / Rock" tempo="" instrumentation="" bpm="" mixout="" frequency="44100" bitrate="1411" dateingested="2008-05-15 06:08:18"/> </tracks> </responsetracks>""" % locals()), ] http = build_http_mock(HttpMock, responses=return_values) album = Album._from_xml(album_xml, client) tracks = album.get_tracks(get_full_detail=False) assert tracks[0].albumid == album_id
def test_album_notracks(HttpMock): client = init_client() album_id = '1c5f47572d9152f3' album_xml = ET.fromstring( textwrap.dedent( """<album featured="false" code="HM001" detail="Razor-sharp pop& rock bristling with spiky guitars & infectious, feelgood inspiration … and tons of attitude." name="HM 001 Sample Album" displaytitle="HM 001 Sample Album " id="%s"/> """ % (album_id))) return_values = [ ( 200, """<responsetracks> <tracks> </tracks> </responsetracks>""" % locals(), ), ] http = build_http_mock(HttpMock, responses=return_values) album = Album._from_xml(album_xml, client) tracks = album.get_tracks(get_full_detail=False) assert len(tracks) == 0
def test_get_download_url(HttpMock): client = init_client() member_id = 'bacba2b288328238bcbac' track_format = 'mp3' playlist_id = "908098a8a0ba8b065" playlist_xml = ET.fromstring(""" <playlist id="%(playlist_id)s" name="sample playlist"> <tracks> <track tracknumber="1" time="02:50" lengthseconds="170" comment="Track Comment" composer="JJ Jayjay" publisher="PP Peepee" name="Epic Track" albumid="1abcbacbac33" id="11bacbcbabcb3b2823" displaytitle="Epic Track" genre="Pop / Rock" bpm="100" mixout="FULL" frequency="44100" bitrate="1411" dateingested="2008-05-15 06:08:18"/> </tracks> </playlist>""" % locals()) playlist = Playlist._from_xml(playlist_xml, client) download_url = playlist.get_download_url(track_format, member_id) download_url_template = client.config.playlistdownload_url format_identifier = client.config.get_format_identifier(track_format) expected_url = download_url_template.replace('{memberaccountid}', member_id).\ replace('{id}', playlist_id).\ replace('{trackformat}', format_identifier) assert download_url == expected_url, 'url: %s != expected: %s' % (download_url, expected_url)
def test_from_xml(): client = init_client() test_member_id = get_random_md5() username = '******' firstname = 'Test' lastname = 'User' email = '*****@*****.**' xml_str = """<memberaccount id="%(test_member_id)s"> <username>%(username)s</username> <firstname>%(firstname)s</firstname> <lastname>%(lastname)s</lastname> <email>%(email)s</email> </memberaccount>""" % \ {'test_member_id': test_member_id, 'username': username, 'firstname': firstname, 'lastname': lastname, 'email': email} member_xml = ET.fromstring(xml_str) member = Member._from_xml(member_xml, client) assert member.id == test_member_id assert member.firstname == firstname
def parse_chunks(chunk): """Pass through a chunk of document IDs and extract quotes""" db_client = utils.init_client(MONGO_ARGS) collection = db_client[DB_NAME][READ_COL] for idx in chunk: doc = collection.find_one({'_id': idx}, no_cursor_timeout=True) parse_doc(collection, doc)
def test_album_init(): client = init_client() album_id = '1c5f47572d9152f3' album_xml = ET.fromstring(textwrap.dedent("""<album featured="false" code="HM001" detail="Razor-sharp pop& rock bristling with spiky guitars & infectious, feelgood inspiration … and tons of attitude." name="HM 001 Sample Album" displaytitle="HM 001 Sample Album " id="%s"/> """ % (album_id))) album = Album._from_xml(album_xml, client) assert album.id == album_id
def parse_chunks(chunk): """Pass through a chunk of document IDs and extract quotes""" db_client = utils.init_client(MONGO_ARGS) read_collection = db_client[DB_NAME][READ_COL] write_collection = db_client[DB_NAME][WRITE_COL] if WRITE_COL else None # Create requests session object for more persistent HTTP connections session = requests.Session() for idx in chunk: doc = read_collection.find_one({'_id': idx}, no_cursor_timeout=True) update_db(read_collection, write_collection, doc, session)
def test_send_password(HttpMock): client = init_client() test_username = '******' content = """<?xml version="1.0" encoding="utf-8"?> <responsecode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <code>OK</code> </responsecode>""" http = build_http_mock(HttpMock, content=content) Member.send_password(test_username, client)
def test_get_categories(HttpMock): client = init_client() cwd = os.path.dirname(__file__) categories_xml = ET.parse(os.path.join(cwd, 'categories.xml')) http = build_http_mock(HttpMock, content=ET.tostring(categories_xml.getroot())) categories = Category.query.get_categories(client) categories_in_xml = len(categories_xml.getroot().find('categories').findall('category')) client_categories = len(categories) assert categories_in_xml == client_categories, 'Category counts do not match %s != %s' % \ (categories_in_xml, client_categories)
def test_album_init(): client = init_client() album_id = '1c5f47572d9152f3' album_xml = ET.fromstring( textwrap.dedent( """<album featured="false" code="HM001" detail="Razor-sharp pop& rock bristling with spiky guitars & infectious, feelgood inspiration … and tons of attitude." name="HM 001 Sample Album" displaytitle="HM 001 Sample Album " id="%s"/> """ % (album_id))) album = Album._from_xml(album_xml, client) assert album.id == album_id
def test_track_dict(): client = init_client() track_id = '17376d36f309f18d' track_xml = ET.fromstring(textwrap.dedent("""<track tracknumber="1" time="02:50" lengthseconds="170" comment="Make sure you’re down the front for this fiery Post Punk workout." composer=""S. Milton, J. Wygens"" publisher="" name="Guerilla Pop" albumid="1c5f47572d9152f3" id="%(track_id)s" keywords="" displaytitle="Guerilla Pop" genre="Pop / Rock" tempo="" instrumentation="" bpm="" mixout="" frequency="44100" bitrate="1411" dateingested="2008-05-15 06:08:18"/>""") % locals()) track = Track._from_xml(track_xml, client) track_dict = track.as_dict() assert track_dict['id'] == track_id
def test_get_member_playlists(HttpMock): client = init_client() test_member_id = get_random_md5() username = '******' firstname = 'Test' lastname = 'User' email = '*****@*****.**' member_xml = ET.fromstring("""<memberaccount id="%(test_member_id)s"> <username>%(username)s</username> <firstname>%(firstname)s</firstname> <lastname>%(lastname)s</lastname> <email>%(email)s</email> </memberaccount>""" % {'test_member_id': test_member_id, 'username': username, 'firstname': firstname, 'lastname': lastname, 'email': email}) member = Member._from_xml(member_xml, client) test_playlist_id = get_random_md5() test_playlist_name = 'test playlist' content = """<?xml version="1.0" encoding="utf-8"?> <ResponsePlaylists> <playlists> <playlist id="%(id)s" name="%(name)s"> <tracks> <track tracknumber="001" time="02:50" lengthseconds="170" comment="Make sure you’re down the front for this fiery Post Punk workout." composer=""S. Milton, J. Wygens"" publisher="HM" name="Guerilla Pop" id="17376d36f309f18d" keywords="" lyrics="" displaytitle="Guerilla Pop" genre="Pop / Rock" tempo="" instrumentation="" bpm="" mixout="" frequency="44100" bitrate="1411" /> </tracks> </playlist> </playlists> </ResponsePlaylists>""" % {'id': test_playlist_id, 'name': test_playlist_name} http = build_http_mock(HttpMock, content=content) playlists = member.get_playlists() assert isinstance(playlists, list) playlist = playlists[0] assert playlist.id == test_playlist_id assert playlist.name == test_playlist_name
def test_get_categories(HttpMock): client = init_client() cwd = os.path.dirname(__file__) categories_xml = ET.parse(os.path.join(cwd, 'categories.xml')) http = build_http_mock(HttpMock, content=ET.tostring(categories_xml.getroot())) categories = Category.query.get_categories(client) categories_in_xml = len( categories_xml.getroot().find('categories').findall('category')) client_categories = len(categories) assert categories_in_xml == client_categories, 'Category counts do not match %s != %s' % \ (categories_in_xml, client_categories)
def test_member_invalid(HttpMock): client = init_client() test_member_id = get_random_md5() content = """<?xml version="1.0" encoding="utf-8"?> <responsemember> <error> <code>7</code> <description>Member Does Not Exist</description> </error> </responsemember>""" http = build_http_mock(HttpMock, content=content) member = Member.query.get_by_id(test_member_id, client)
def test_member_authenticate_fail(HttpMock): client = init_client() test_username = '******' test_password = get_random_md5() content = """<?xml version="1.0" encoding="utf-8"?> <responsemember> <error> <code>6</code> <description>Invalid Login Details</description> </error> </responsemember>""" http = build_http_mock(HttpMock, content=content) member = Member.authenticate(test_username, test_password, client)
def test_track_dict(): client = init_client() track_id = '17376d36f309f18d' track_xml = ET.fromstring( textwrap.dedent( """<track tracknumber="1" time="02:50" lengthseconds="170" comment="Make sure you’re down the front for this fiery Post Punk workout." composer=""S. Milton, J. Wygens"" publisher="" name="Guerilla Pop" albumid="1c5f47572d9152f3" id="%(track_id)s" keywords="" displaytitle="Guerilla Pop" genre="Pop / Rock" tempo="" instrumentation="" bpm="" mixout="" frequency="44100" bitrate="1411" dateingested="2008-05-15 06:08:18"/>""" ) % locals()) track = Track._from_xml(track_xml, client) track_dict = track.as_dict() assert track_dict['id'] == track_id
def test_add_track(HttpMock): client = init_client() test_playlist_id = get_random_md5() track_id = get_random_md5() content = """<?xml version="1.0" encoding="utf-8"?> <responsecode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <code>OK</code> </responsecode>""" http = build_http_mock(HttpMock, content=content) playlist = Playlist(_client=client) playlist.member_id = 123 playlist.id = test_playlist_id playlist.add_track(track_id)
def test_get_download_url_missing_format(HttpMock): client = init_client() member_id = 'bacba2b288328238bcbac' track_format = 'BAD-FORMAT' track_id = '17376d36f309f18d' track_name = 'Guerilla Pop' track_xml = ET.fromstring(textwrap.dedent("""<track tracknumber="1" time="02:50" lengthseconds="170" comment="Make sure you’re down the front for this fiery Post Punk workout." composer=""S. Milton, J. Wygens"" publisher="" name="%(track_name)s" albumid="1c5f47572d9152f3" id="%(track_id)s" keywords="" displaytitle="Guerilla Pop" genre="Pop / Rock" tempo="" instrumentation="" bpm="" mixout="" frequency="44100" bitrate="1411" dateingested="2008-05-15 06:08:18"/>""") % locals()) track = Track._from_xml(track_xml, client) download_url = track.get_download_url(track_format, member_id)
def test_get_library_logo_url(HttpMock): client = init_client() width = 200 height = 400 library_id = '11235813' library_logo_url = "http://download.harvestmedia.net/wslibrarylogo/8185d768cd8fcaa7/{id}/{width}/{height}" library_xml = ET.fromstring(textwrap.dedent("""<library detail="" name="" id="%(library_id)s" location="Talent, Oregon" website="http://risefromyourgrave.com" librarylogourl="%(library_logo_url)s" />""" % locals())) library = Library._from_xml(library_xml, client) library_logo_url = library.get_logo_url(width, height) expected_url = library_logo_url.replace('{id}', library_id).replace('{width}', str(width)).replace('{height}]', str(height)) assert library_logo_url == expected_url
def test_get_member_favourites(HttpMock): client = init_client() test_member_id = get_random_md5() username = '******' firstname = 'Test' lastname = 'User' email = '*****@*****.**' member_xml = ET.fromstring("""<memberaccount id="%(test_member_id)s"> <username>%(username)s</username> <firstname>%(firstname)s</firstname> <lastname>%(lastname)s</lastname> <email>%(email)s</email> </memberaccount>""" % \ {'test_member_id': test_member_id, 'username': username, 'firstname': firstname, 'lastname': lastname, 'email': email}) member = Member._from_xml(member_xml, client) test_track_id = get_random_md5() test_track_name = 'test track' xml_response = """<?xml version="1.0" encoding="utf-8"?> <ResponseFavourites> <favourites> <tracks> <track id="%(id)s" name="%(name)s" /> </tracks> </favourites> </ResponseFavourites>""" % { 'id': test_track_id, 'name': test_track_name } http = build_http_mock(HttpMock, content=xml_response) favourites = member.get_favourites() assert isinstance(favourites, list) favourite = favourites[0] assert favourite.id == test_track_id assert favourite.name == test_track_name
def test_get_cover_url(HttpMock): client = init_client() album_id = '1c5f47572d9152f3' album_xml = ET.fromstring( textwrap.dedent( """<album featured="false" code="HM001" detail="Razor-sharp pop& rock bristling with spiky guitars & infectious, feelgood inspiration … and tons of attitude." name="HM 001 Sample Album" displaytitle="HM 001 Sample Album " id="%s"/> """ % (album_id))) album_art_url = "http://asset.harvestmedia.net/albumart/8185d768cd8fcaa7/{id}/{width}/{height}" expiry = datetime.datetime.now() test_token = get_random_md5() return_values = [ (200, """<?xml version="1.0" encoding="utf-8"?> <responseservicetoken> <token value="%s" expiry="%s"/> </responseservicetoken>""" % \ (test_token, expiry.strftime("%Y-%m-%dT%H:%M:%S"))), (200, """<?xml version="1.0" encoding="utf-8"?> <responseserviceinfo> <asseturl albumart="%(album_art_url)s" waveform="http://asset.harvestmedia.net/waveform/8185d768cd8fcaa7/{id}/{width}/{height}" trackstream="http://asset.harvestmedia.net/trackstream/8185d768cd8fcaa7/{id}" trackdownload=" http://asset.harvestmedia.net/trackdownload/8185d768cd8fcaa7/{id}/{trackformat}" /> <trackformats> <trackformat identifier="8185d768cd8fcaa7" extension="mp3" bitrate="320" samplerate="48" samplesize="16" /> <trackformat identifier="768cd8fcaa8185d7" extension="wav" bitrate="1536" samplerate="48" samplesize="16" /> <trackformat identifier="7jsi8fcaa818df57" extension="aif" bitrate="1536" samplerate="48" samplesize="16" /> </trackformats> </responseserviceinfo>""" % locals()), ] http = build_http_mock(HttpMock, responses=return_values) width = 200 height = 300 album = Album._from_xml(album_xml, client) cover_art_url = album.get_cover_url(width, height) expected_url = album_art_url.replace('{id}', album_id).replace( '{width}', str(width)).replace('{height}', str(height)) assert cover_art_url == expected_url
def test_get_download_url_missing_format(HttpMock): client = init_client() member_id = 'bacba2b288328238bcbac' track_format = 'BAD-FORMAT' track_id = '17376d36f309f18d' track_name = 'Guerilla Pop' track_xml = ET.fromstring( textwrap.dedent( """<track tracknumber="1" time="02:50" lengthseconds="170" comment="Make sure you’re down the front for this fiery Post Punk workout." composer=""S. Milton, J. Wygens"" publisher="" name="%(track_name)s" albumid="1c5f47572d9152f3" id="%(track_id)s" keywords="" displaytitle="Guerilla Pop" genre="Pop / Rock" tempo="" instrumentation="" bpm="" mixout="" frequency="44100" bitrate="1411" dateingested="2008-05-15 06:08:18"/>""" ) % locals()) track = Track._from_xml(track_xml, client) download_url = track.get_download_url(track_format, member_id)
def run_pool(poolsize, chunksize): """Concurrently perform quote extraction based on a filter query""" # Find ALL ids in the database within the query bounds (one-time only) client = utils.init_client(MONGO_ARGS) id_collection = client[DB_NAME][READ_COL] query = utils.prepare_query(filters) document_ids = id_collection.find(query, no_cursor_timeout=True).distinct('_id') app_logger.info("Obtained ID list for {} articles.".format(len(document_ids))) # Check for doc limit if DOC_LIMIT > 0: document_ids = document_ids[:DOC_LIMIT] app_logger.info("Processing {} articles...".format(len(document_ids))) # Process quotes using a pool of executors pool = Pool(processes=poolsize) pool.map(parse_chunks, chunker(document_ids, chunksize=chunksize)) pool.close()
def test_get_member_favourites(HttpMock): client = init_client() test_member_id = get_random_md5() username = '******' firstname = 'Test' lastname = 'User' email = '*****@*****.**' member_xml = ET.fromstring("""<memberaccount id="%(test_member_id)s"> <username>%(username)s</username> <firstname>%(firstname)s</firstname> <lastname>%(lastname)s</lastname> <email>%(email)s</email> </memberaccount>""" % \ {'test_member_id': test_member_id, 'username': username, 'firstname': firstname, 'lastname': lastname, 'email': email}) member = Member._from_xml(member_xml, client) test_track_id = get_random_md5() test_track_name = 'test track' xml_response = """<?xml version="1.0" encoding="utf-8"?> <ResponseFavourites> <favourites> <tracks> <track id="%(id)s" name="%(name)s" /> </tracks> </favourites> </ResponseFavourites>""" % {'id': test_track_id, 'name': test_track_name} http = build_http_mock(HttpMock, content=xml_response) favourites = member.get_favourites() assert isinstance(favourites, list) favourite = favourites[0] assert favourite.id == test_track_id assert favourite.name == test_track_name
def test_album_notracks(HttpMock): client = init_client() album_id = '1c5f47572d9152f3' album_xml = ET.fromstring(textwrap.dedent("""<album featured="false" code="HM001" detail="Razor-sharp pop& rock bristling with spiky guitars & infectious, feelgood inspiration … and tons of attitude." name="HM 001 Sample Album" displaytitle="HM 001 Sample Album " id="%s"/> """ % (album_id))) return_values = [ (200, """<responsetracks> <tracks> </tracks> </responsetracks>""" % locals(),), ] http = build_http_mock(HttpMock, responses=return_values) album = Album._from_xml(album_xml, client) tracks = album.get_tracks(get_full_detail=False) assert len(tracks) == 0
def test_get_download_url_missing_format(HttpMock): client = init_client() member_id = 'bacba2b288328238bcbac' track_format = 'BAD-FORMAT' playlist_id = "908098a8a0ba8b065" playlist_xml = ET.fromstring(""" <playlist id="%(playlist_id)s" name="sample playlist"> <tracks> <track tracknumber="1" time="02:50" lengthseconds="170" comment="Track Comment" composer="JJ Jayjay" publisher="PP Peepee" name="Epic Track" albumid="1abcbacbac33" id="11bacbcbabcb3b2823" displaytitle="Epic Track" genre="Pop / Rock" bpm="100" mixout="FULL" frequency="44100" bitrate="1411" dateingested="2008-05-15 06:08:18"/> </tracks> </playlist>""" % locals()) playlist = Playlist._from_xml(playlist_xml, client) download_url = playlist.get_download_url(track_format, member_id)
def test_get_waveform_url(HttpMock): client = init_client() track_id = '17376d36f309f18d' track_name = 'Guerilla Pop' track_xml = ET.fromstring(textwrap.dedent("""<track tracknumber="1" time="02:50" lengthseconds="170" comment="Make sure you’re down the front for this fiery Post Punk workout." composer=""S. Milton, J. Wygens"" publisher="" name="%(track_name)s" albumid="1c5f47572d9152f3" id="%(track_id)s" keywords="" displaytitle="Guerilla Pop" genre="Pop / Rock" tempo="" instrumentation="" bpm="" mixout="" frequency="44100" bitrate="1411" dateingested="2008-05-15 06:08:18"/>""") % locals()) track = Track._from_xml(track_xml, client) width = 200 height = 300 waveform_url = track.get_waveform_url(width, height) waveform_url_template = client.config.waveform_url expected_url = waveform_url_template.replace('{id}', track_id).replace('{width}', str(width)).replace('{height}', str(height)) assert waveform_url == expected_url
def test_register_member(HttpMock): client = init_client() test_member_id = get_random_md5() username = '******' first_name = 'Test' last_name = 'User' email = '*****@*****.**' xml_response = """<?xml version="1.0" encoding="utf-8"?> <ResponseMember> <memberaccount id="%(test_member_id)s"> <username>%(username)s</username> <first_name>%(first_name)s</first_name> <last_name>%(last_name)s</last_name> <email>%(email)s</email> </memberaccount> </ResponseMember>""" % { 'test_member_id': test_member_id, 'username': username, 'first_name': first_name, 'last_name': last_name, 'email': email } username = username first_name = first_name last_name = last_name email = email termsaccept = 'true' fileformat = 'MP3' http = build_http_mock(HttpMock, content=xml_response) member = Member.register(_client=client, username=username, firstname=first_name, lastname=last_name, email=email, termsaccept=termsaccept, fileformat=fileformat) assert member.id == test_member_id assert member.username == username
def test_get_playlist_art_url(HttpMock): client = init_client() playlist_id = '112358' playlist_xml = ET.fromstring(textwrap.dedent("""<playlist name="EFF!" id="%s" createddate="2012-04-17 06:24:45" trackcount="0" />""" % (playlist_id))) playlist_art_url = 'http://download.harvestmedia.net/wsplaylistart/8185d768cd8fcaa7/{id}/{width}/{height}' expiry = datetime.datetime.now() test_token = get_random_md5() return_values = [ (200, """<?xml version="1.0" encoding="utf-8"?> <responseservicetoken> <token value="%s" expiry="%s"/> </responseservicetoken>""" % \ (test_token, expiry.strftime("%Y-%m-%dT%H:%M:%S"))), (200, """<?xml version="1.0" encoding="utf-8"?> <responseserviceinfo> <asseturl waveform="http://asset.harvestmedia.net/waveform/8185d768cd8fcaa7/{id}/{width}/{height}" trackstream="http://asset.harvestmedia.net/trackstream/8185d768cd8fcaa7/{id}" trackdownload=" http://asset.harvestmedia.net/trackdownload/8185d768cd8fcaa7/{id}/{trackformat}" playlistart="%(playlist_art_url)s" /> <trackformats> <trackformat identifier="8185d768cd8fcaa7" extension="mp3" bitrate="320" samplerate="48" samplesize="16" /> <trackformat identifier="768cd8fcaa8185d7" extension="wav" bitrate="1536" samplerate="48" samplesize="16" /> <trackformat identifier="7jsi8fcaa818df57" extension="aif" bitrate="1536" samplerate="48" samplesize="16" /> </trackformats> </responseserviceinfo>""" % locals()), ] http = build_http_mock(HttpMock, responses=return_values) width = 200 height = 300 playlist = Playlist._from_xml(playlist_xml, client) cover_art_url = playlist.get_cover_url(width, height) expected_url = playlist_art_url.replace('{id}', playlist_id).replace('{width}', str(width)).replace('{height}', str(height)) assert cover_art_url == expected_url
def test_get_libraries(HttpMock): client = init_client() return_values = [ (200, """<ResponseLibraries> <libraries> <library id="abc123" name="VIDEOHELPER" detail="Library description" /> <library id="abc125" name="MODULES" detail="Library description" /> </libraries> </ResponseLibraries>"""), ] http = build_http_mock(HttpMock, responses=return_values) libraries = Library.query.get_libraries(client) assert isinstance(libraries, list) library = libraries[0] assert library.id == 'abc123' assert library.name == 'VIDEOHELPER'
def test_add_playlist(HttpMock): client = init_client() test_member_id = get_random_md5() test_playlist_id = get_random_md5() test_playlist_name = 'test playlist' content = """<?xml version="1.0" encoding="utf-8"?> <ResponsePlaylists> <playlists> <playlist id="%(id)s" name="%(name)s" /> </playlists> </ResponsePlaylists>""" % \ {'id': test_playlist_id, 'name': test_playlist_name} http = build_http_mock(HttpMock, content=content) playlist = Playlist.add(_client=client, member_id=test_member_id, playlist_name=test_playlist_name) assert playlist.id == test_playlist_id assert playlist.name == test_playlist_name
def test_get_cover_url(HttpMock): client = init_client() album_id = '1c5f47572d9152f3' album_xml = ET.fromstring(textwrap.dedent("""<album featured="false" code="HM001" detail="Razor-sharp pop& rock bristling with spiky guitars & infectious, feelgood inspiration … and tons of attitude." name="HM 001 Sample Album" displaytitle="HM 001 Sample Album " id="%s"/> """ % (album_id))) album_art_url = "http://asset.harvestmedia.net/albumart/8185d768cd8fcaa7/{id}/{width}/{height}" expiry = datetime.datetime.now() test_token = get_random_md5() return_values = [ (200, """<?xml version="1.0" encoding="utf-8"?> <responseservicetoken> <token value="%s" expiry="%s"/> </responseservicetoken>""" % \ (test_token, expiry.strftime("%Y-%m-%dT%H:%M:%S"))), (200, """<?xml version="1.0" encoding="utf-8"?> <responseserviceinfo> <asseturl albumart="%(album_art_url)s" waveform="http://asset.harvestmedia.net/waveform/8185d768cd8fcaa7/{id}/{width}/{height}" trackstream="http://asset.harvestmedia.net/trackstream/8185d768cd8fcaa7/{id}" trackdownload=" http://asset.harvestmedia.net/trackdownload/8185d768cd8fcaa7/{id}/{trackformat}" /> <trackformats> <trackformat identifier="8185d768cd8fcaa7" extension="mp3" bitrate="320" samplerate="48" samplesize="16" /> <trackformat identifier="768cd8fcaa8185d7" extension="wav" bitrate="1536" samplerate="48" samplesize="16" /> <trackformat identifier="7jsi8fcaa818df57" extension="aif" bitrate="1536" samplerate="48" samplesize="16" /> </trackformats> </responseserviceinfo>""" % locals()), ] http = build_http_mock(HttpMock, responses=return_values) width = 200 height = 300 album = Album._from_xml(album_xml, client) cover_art_url = album.get_cover_url(width, height) expected_url = album_art_url.replace('{id}', album_id).replace('{width}', str(width)).replace('{height}', str(height)) assert cover_art_url == expected_url
def test_get_library_logo_url(HttpMock): client = init_client() width = 200 height = 400 library_id = '11235813' library_logo_url = "http://download.harvestmedia.net/wslibrarylogo/8185d768cd8fcaa7/{id}/{width}/{height}" library_xml = ET.fromstring( textwrap.dedent( """<library detail="" name="" id="%(library_id)s" location="Talent, Oregon" website="http://risefromyourgrave.com" librarylogourl="%(library_logo_url)s" />""" % locals())) library = Library._from_xml(library_xml, client) library_logo_url = library.get_logo_url(width, height) expected_url = library_logo_url.replace('{id}', library_id).replace( '{width}', str(width)).replace('{height}]', str(height)) assert library_logo_url == expected_url
def test_member_authenticate(HttpMock): client = init_client() test_member_id = get_random_md5() test_username = '******' test_password = get_random_md5() test_firstname = 'first' test_lastname = 'last' test_email = '*****@*****.**' content = """<?xml version="1.0" encoding="utf-8"?> <ResponseMember> <memberaccount id="%(test_member_id)s"> <username>%(test_username)s</username> <firstname>%(test_firstname)s</firstname> <lastname>%(test_lastname)s</lastname> <email>%(test_email)s</email> </memberaccount> </ResponseMember>""" % locals() http = build_http_mock(HttpMock, content=content) member = Member.authenticate(test_username, test_password, client) assert member.id == test_member_id
def test_expired_token_refetch(HttpMock): api_key = get_random_md5() expiry = datetime.datetime.now() expiry2 = expiry + datetime.timedelta(hours=22) test_second_token = get_random_md5() return_values = [ (200, """<?xml version="1.0" encoding="utf-8"?> <responseservicetoken> <token value="%s" expiry="%s"/> </responseservicetoken>""" % \ (test_second_token, expiry2.strftime("%Y-%m-%dT%H:%M:%S"))), (200, """<?xml version="1.0" encoding="utf-8"?> <responseserviceinfo> <asseturl albumart="http://asset.harvestmedia.net/albumart/8185d768cd8fcaa7/{id}/{width}/{height}" waveform="http://asset.harvestmedia.net/waveform/8185d768cd8fcaa7/{id}/{width}/{height}" trackstream="http://asset.harvestmedia.net/trackstream/8185d768cd8fcaa7/{id}" trackdownload=" http://asset.harvestmedia.net/trackdownload/8185d768cd8fcaa7/{id}/{trackformat}" /> <trackformats> <trackformat identifier="8185d768cd8fcaa7" extension="mp3" bitrate="320" samplerate="48" samplesize="16" /> <trackformat identifier="768cd8fcaa8185d7" extension="wav" bitrate="1536" samplerate="48" samplesize="16" /> <trackformat identifier="7jsi8fcaa818df57" extension="aif" bitrate="1536" samplerate="48" samplesize="16" /> </trackformats> </responseserviceinfo>"""), ] client = init_client() # force-expire the token client.config.service_token.expiry = ( datetime.datetime.now() - datetime.timedelta(hours=12)).isoformat() http = build_http_mock(HttpMock, responses=return_values) # this should fetch a new token since the old one is expired client.get_service_info() # token should match the SECOND token assert client.config.service_token.token == test_second_token
def test_get_download_url(HttpMock): client = init_client() member_id = 'bacba2b288328238bcbac' track_format = 'mp3' track_id = '17376d36f309f18d' track_name = 'Guerilla Pop' track_xml = ET.fromstring(textwrap.dedent("""<track tracknumber="1" time="02:50" lengthseconds="170" comment="Make sure you’re down the front for this fiery Post Punk workout." composer=""S. Milton, J. Wygens"" publisher="" name="%(track_name)s" albumid="1c5f47572d9152f3" id="%(track_id)s" keywords="" displaytitle="Guerilla Pop" genre="Pop / Rock" tempo="" instrumentation="" bpm="" mixout="" frequency="44100" bitrate="1411" dateingested="2008-05-15 06:08:18"/>""") % locals()) track = Track._from_xml(track_xml, client) download_url = track.get_download_url(track_format, member_id) download_url_template = client.config.download_url format_identifier = client.config.get_format_identifier(track_format) expected_url = download_url_template.replace('{memberaccountid}', member_id).\ replace('{id}', track_id).\ replace('{trackformat}', format_identifier) assert download_url == expected_url, 'url: %s != expected: %s' % (download_url, expected_url)
def test_expired_token_refetch(HttpMock): api_key = get_random_md5() expiry = datetime.datetime.now() expiry2 = expiry + datetime.timedelta(hours=22) test_second_token = get_random_md5() return_values = [ (200, """<?xml version="1.0" encoding="utf-8"?> <responseservicetoken> <token value="%s" expiry="%s"/> </responseservicetoken>""" % \ (test_second_token, expiry2.strftime("%Y-%m-%dT%H:%M:%S"))), (200, """<?xml version="1.0" encoding="utf-8"?> <responseserviceinfo> <asseturl albumart="http://asset.harvestmedia.net/albumart/8185d768cd8fcaa7/{id}/{width}/{height}" waveform="http://asset.harvestmedia.net/waveform/8185d768cd8fcaa7/{id}/{width}/{height}" trackstream="http://asset.harvestmedia.net/trackstream/8185d768cd8fcaa7/{id}" trackdownload=" http://asset.harvestmedia.net/trackdownload/8185d768cd8fcaa7/{id}/{trackformat}" /> <trackformats> <trackformat identifier="8185d768cd8fcaa7" extension="mp3" bitrate="320" samplerate="48" samplesize="16" /> <trackformat identifier="768cd8fcaa8185d7" extension="wav" bitrate="1536" samplerate="48" samplesize="16" /> <trackformat identifier="7jsi8fcaa818df57" extension="aif" bitrate="1536" samplerate="48" samplesize="16" /> </trackformats> </responseserviceinfo>"""), ] client = init_client() # force-expire the token client.config.service_token.expiry = (datetime.datetime.now() - datetime.timedelta(hours=12)).isoformat() http = build_http_mock(HttpMock, responses=return_values) # this should fetch a new token since the old one is expired client.get_service_info() # token should match the SECOND token assert client.config.service_token.token == test_second_token
def test_member_update(HttpMock): client = init_client() test_member_id = get_random_md5() test_username = '******' test_email = '*****@*****.**' test_firstname = 'test' test_lastname = 'user' test_username_update = 'new name' return_values = [ (200, """<?xml version="1.0" encoding="utf-8"?> <ResponseMember> <memberaccount id="%(test_member_id)s"> <username>%(test_username)s</username> <firstname>%(test_firstname)s</firstname> <lastname>%(test_lastname)s</lastname> <email>%(test_email)s</email> </memberaccount> </ResponseMember>""" % locals()), (200, """<?xml version="1.0" encoding="utf-8"?> <ResponseMember> <memberaccount id="%(test_member_id)s"> <username>%(test_username_update)s</username> <firstname>%(test_firstname)s</firstname> <lastname>%(test_lastname)s</lastname> <email>%(test_email)s</email> </memberaccount> </ResponseMember>""" % locals()), ] http = build_http_mock(HttpMock, responses=return_values) member = Member.query.get_by_id(test_member_id, client) assert member.username == test_username member.username = test_username_update member.update() assert member.username == test_username_update
def test_get_featured_playlists(HttpMock): client = init_client() test_playlist_id = get_random_md5() test_playlist_name = 'test playlist' content = """<?xml version="1.0" encoding="utf-8"?> <responsefeaturedplaylists> <playlists> <playlist id="%(id)s" name="%(name)s"> <tracks> <track tracknumber="001" time="01:07" lengthseconds="67" comment="If a certain animated film studio were to remake The Andy Griffith Show as a digital short, we'd nominate this for the theme. Warm, chunky, a little slow on the uptake... a.k.a. the anti-lemonade song. Ending starts @ 1:08. Lite Mix, without main rhythm acoustic guitars." composer="D. Holter/K. White" publisher="TLL UNDERscore Nashcap (ASCAP)" name="Pencilneck Strut" id="902dea1d377473df" keywords="Cute, Goofy, Lighthearted, Happy, Comical, Twang, Rural, Fun, Mischievous, Celebration, Campy, Childlike, Cheerful, Simple, Quirky, Swampy, Playful" lyrics="" displaytitle="Pencilneck Strut" genre="Country" tempo="Medium" instrumentation="Acoustic Guitar, Banjo, Percussion" bpm="130" mixout="Alt2" frequency="2650" bitrate="24" /> </tracks> </playlist> </playlists> </responsefeaturedplaylists> """ % {'id': test_playlist_id, 'name': test_playlist_name} http = build_http_mock(HttpMock, content=content) playlists = Playlist.query.get_featured_playlists(client) assert isinstance(playlists, list) playlist = playlists[0] assert playlist.id == test_playlist_id assert playlist.name == test_playlist_name