def return_results(rdio, user_id, song_artist_url_list, from_tweet_id=None): db = get_db() prefs = get_db_prefs(user_id, db=db) playlist_key = prefs.get(Preferences.PlaylistToSaveTo, "new") add_to_collection = prefs.get(Preferences.AddToCollection, False) track_keys_list = [s[3] for s in song_artist_url_list] playlists_call = rdio.call("getPlaylists") if "result" in playlists_call: playlists = playlists_call["result"]["owned"] else: playlists = [] p_keys = [playlist["key"] for playlist in playlists] p_names = [playlist["name"] for playlist in playlists] if playlist_key in ["new", "alwaysnew"] or playlist_key not in p_keys: new_name = generate_playlist_name(p_names) result = rdio.call( "createPlaylist", { "name": new_name, "description": "Songs found by discoversong on %s." % datetime.datetime.now().strftime("%A, %d %b %Y %H:%M"), "tracks": ", ".join(track_keys_list), }, ) new_key = result["result"]["key"] if playlist_key == "new" or playlist_key not in p_keys: prefs[Preferences.PlaylistToSaveTo] = new_key db.update(USER_TABLE, where="rdio_user_id=%i" % user_id, prefs=BSONPostgresSerializer.from_dict(prefs)) # else leave 'alwaysnew' to repeat this behavior every time else: rdio.call("addToPlaylist", {"playlist": playlist_key, "tracks": ", ".join(track_keys_list)}) if add_to_collection: rdio.call("addToCollection", {"keys": ", ".join(track_keys_list)}) stats.found_songs(user_id, len(song_artist_url_list)) twitter_name_config = Capabilities.Twitter().config_options_dict()["twitter_name"] user, message = get_discoversong_user(user_id) twitter_name = "@" + twitter_name_config.get_value(user) reply_to_tweet_id = from_tweet_id song, artist, url, track_key = song_artist_url_list[0] try: tweet_about_found_song(song, artist, url, mention_name=twitter_name, reply_to_tweet_id=reply_to_tweet_id) except twython.TwythonError as twe: if twe.error_code == 403: pass
def return_results(rdio, user_id, song_artist_url_list, from_tweet_id=None): db = get_db() prefs = get_db_prefs(user_id, db=db) playlist_key = prefs.get(Preferences.PlaylistToSaveTo, 'new') add_to_collection = prefs.get(Preferences.AddToCollection, False) track_keys_list = [s[3] for s in song_artist_url_list] playlists_call = rdio.call('getPlaylists') if 'result' in playlists_call: playlists = playlists_call['result']['owned'] else: playlists = [] p_keys = [playlist['key'] for playlist in playlists] p_names = [playlist['name'] for playlist in playlists] if playlist_key in ['new', 'alwaysnew'] or playlist_key not in p_keys: new_name = generate_playlist_name(p_names) result = rdio.call('createPlaylist', {'name': new_name, 'description': 'Songs found by discoversong on %s.' % datetime.datetime.now().strftime('%A, %d %b %Y %H:%M'), 'tracks': ', '.join(track_keys_list)}) new_key = result['result']['key'] if playlist_key == 'new' or playlist_key not in p_keys: prefs[Preferences.PlaylistToSaveTo] = new_key db.update(USER_TABLE, where="rdio_user_id=%i" % user_id, prefs=BSONPostgresSerializer.from_dict(prefs)) # else leave 'alwaysnew' to repeat this behavior every time else: rdio.call('addToPlaylist', {'playlist': playlist_key, 'tracks': ', '.join(track_keys_list)}) if add_to_collection: rdio.call('addToCollection', {'keys': ', '.join(track_keys_list)}) stats.found_songs(user_id, len(song_artist_url_list)) twitter_name_config = Capabilities.Twitter().config_options_dict()['twitter_name'] user, message = get_discoversong_user(user_id) twitter_name = '@' + twitter_name_config.get_value(user) reply_to_tweet_id = from_tweet_id song, artist, url, track_key = song_artist_url_list[0] try: tweet_about_found_song(song, artist, url, mention_name=twitter_name, reply_to_tweet_id=reply_to_tweet_id) except twython.TwythonError as twe: if twe.error_code == 403: pass
def POST(self): db = get_db() input = get_input() envelope = json.loads(input['envelope']) to_addresses = envelope['to'] print 'received email to', to_addresses for to_address in to_addresses: lookup = db.select('discoversong_user', what='rdio_user_id, playlist, token, secret', where="address='%s'" % to_address) if len(lookup) == 1: result = lookup[0] access_token = str(result['token']) access_token_secret = str(result['secret']) rdio, current_user = get_rdio_and_current_user(access_token=access_token, access_token_secret=access_token_secret) print 'found user', current_user['username'] subject = input['subject'] title, artist = parse(subject) print 'parsed artist', artist, 'title', title search_result = rdio.call('search', {'query': ' '.join([title, artist]), 'types': 'Track'}) track_keys = [] name_artist_pairs_found = {} for possible_hit in search_result['result']['results']: if possible_hit['canStream']: name = possible_hit['name'] artist_name = possible_hit['artist'] if name_artist_pairs_found.has_key((name, artist_name)): continue name_artist_pairs_found[(name, artist_name)] = True track_key = possible_hit['key'] track_keys.append(track_key) print 'found tracks', track_keys playlist_key = result['playlist'] if playlist_key in ['new', 'alwaysnew']: p_names = [playlist['name'] for playlist in rdio.call('getPlaylists')['result']['owned']] new_name = generate_playlist_name(p_names) print 'creating new playlist', new_name result = rdio.call('createPlaylist', {'name': new_name, 'description': 'Songs found by discoversong on %s.' % datetime.datetime.now().strftime('%A, %d %b %Y %H:%M'), 'tracks': ', '.join(track_keys)}) new_key = result['result']['key'] if playlist_key == 'new': print 'setting', new_key, 'as the playlist to use next time' user_id = int(current_user['key'][1:]) db.update('discoversong_user', where="rdio_user_id=%i" % user_id, playlist=new_key) # else leave 'alwaysnew' to repeat this behavior every time else: rdio.call('addToPlaylist', {'playlist': playlist_key, 'tracks': ', '.join(track_keys)}) return None