Ejemplo n.º 1
0
class MyFreeSound:
    def __init__(self):
        self.keyword_getter = KeyWordGetter()

    def get_sounds_from_url(self, url, num=8):
        """
        Return set of sounds (mp3 file urls) based on the contents of a html
        page url. Finds keywords and then queries them to freesound.org.
        """
        keywords = self.keyword_getter.get_keywords_from_url(url)
        print 'Top keywords for %s are %s' % (url, str(keywords))
        results = self.search_words(keywords)[0:num]
        if len(results) < num:
            warn('Could only find %d results instead of %d' % (
                len(results), num))

        return results

    def search_words(self, word_weight_pair):
        """
        Returns collection of sounds (mp3 file urls) sorted by relavence to
        inputed keywords with initial weights.
        """
        word_weight_pairs = []
        worker_threads = []

        # Freesound API calls block so use concurrent threads to increase
        # performance
        for pair in word_weight_pair:
            t = threading.Thread(target=worker, args=(pair, word_weight_pairs))
            t.start()
            worker_threads.append(t)
        for t in worker_threads:
            t.join()
        for t in worker_threads:
            if not t.isAlive():
                # get results from thtead
                t.handled = True
        worker_threads = [t for t in worker_threads if not t.handled]

        # After collecting all sound results from freesound
        # Sort sounds by second element (determined weight)
        return [url for (url, weight) in sorted(word_weight_pairs, key=itemgetter(1), reverse=True)]
Ejemplo n.º 2
0
 def __init__(self):
     self.keyword_getter = KeyWordGetter()