Exemplo n.º 1
0
 def POST(self):
   db = get_db()
   
   form_input = get_input()
   
   envelope = json.loads(form_input['envelope'])
   to_addresses = envelope['to']
   
   for to_address in to_addresses:
     
     lookup = db.select(USER_TABLE, 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, user_id = get_rdio_and_current_user(access_token=access_token, access_token_secret=access_token_secret)
       
       stats.got_email(user_id)
       
       subject = form_input['subject']
       body = form_input['text']
       
       try:
         title, artist = parse(subject, body)
       except Exception as e:
         logging.exception(e.message)
         return None
       
       search_results = well_formed_search(rdio, user_id, artist, title)
       return_results(rdio, user_id, search_results)
   
   return None
Exemplo n.º 2
0
  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