コード例 #1
0
 def setUp(self):        
     self.websites = ['www.google.com', 'www.bing.com', 'www.yahoo.com']
     self.content = ContentHandler()
     
     self.m1 = Media(id = 0, content_type = Media.PICTURE)
     self.m1.moods.add(Mood.HAPPY)
     self.m2 = Media(id = 1, content_type = Media.PICTURE)
     self.m2.moods.add(Mood.HAPPY)
     self.m3 = Media(id = 2, content_type = Media.PICTURE)
     self.m3.moods.add(Mood.HAPPY)
     
     self.p1 = Picture(media = self.m1, url = self.websites[0], flickr_id = 0)
     self.p2 = Picture(media = self.m2, url = self.websites[1], flickr_id = 1)
     self.p3 = Picture(media = self.m3, url = self.websites[2], flickr_id = 2)
     
     self.r1 = Rank(media = self.m1, thumbs_up = 0, thumbs_down = 0)
     self.r2 = Rank(media = self.m2, thumbs_up = 1, thumbs_down = 20)
     self.r3 = Rank(media = self.m3, thumbs_up = 7, thumbs_down = 5)
     
     self.m1.save()
     self.m2.save()
     self.m3.save()
     self.p1.save()
     self.p2.save()
     self.p3.save()
     self.r1.save()
     self.r2.save()
     self.r3.save()
     
     pass
コード例 #2
0
def fetch_flickr_photos(mood_type, terms, num_photos):        
        # grab from flickr
        photos =  flickr.walk(tag_mode = 'all', tags = terms, extras='url_z') 
        for i in range(0, num_photos):
            photo = photos.next()
            url = photo.get('url_z')
            pid = photo.get('id')
            score = computePictureScore(pid, mood_type)
            assert Picture.add(pid, url, mood_type, initialScore = score)
コード例 #3
0
def fetch_flickr_photos(mood_type, terms, num_photos):
    # grab from flickr
    photos = flickr.walk(tag_mode='all', tags=terms, extras='url_z')
    for i in range(0, num_photos):
        photo = photos.next()
        url = photo.get('url_z')
        pid = photo.get('id')
        score = computePictureScore(pid, mood_type)
        assert Picture.add(pid, url, mood_type, initialScore=score)
コード例 #4
0
    def setUp(self):
        self.websites = ['www.google.com', 'www.bing.com', 'www.yahoo.com']
        self.content = ContentHandler()

        self.m1 = Media(id=0, content_type=Media.PICTURE)
        self.m1.moods.add(Mood.HAPPY)
        self.m2 = Media(id=1, content_type=Media.PICTURE)
        self.m2.moods.add(Mood.HAPPY)
        self.m3 = Media(id=2, content_type=Media.PICTURE)
        self.m3.moods.add(Mood.HAPPY)

        self.p1 = Picture(media=self.m1, url=self.websites[0], flickr_id=0)
        self.p2 = Picture(media=self.m2, url=self.websites[1], flickr_id=1)
        self.p3 = Picture(media=self.m3, url=self.websites[2], flickr_id=2)

        self.r1 = Rank(media=self.m1, thumbs_up=0, thumbs_down=0)
        self.r2 = Rank(media=self.m2, thumbs_up=1, thumbs_down=20)
        self.r3 = Rank(media=self.m3, thumbs_up=7, thumbs_down=5)

        self.m1.save()
        self.m2.save()
        self.m3.save()
        self.p1.save()
        self.p2.save()
        self.p3.save()
        self.r1.save()
        self.r2.save()
        self.r3.save()

        pass
コード例 #5
0
def pullPictures(mood, terms, add_num = 0):        
    # get pics
    pics = flickr.photos_search(tags=terms,\
                                safe_search=1,\
                                per_page=_SEARCH_NUM)
    
    # calculate how many pictures to do
    length = len(pics[0])
    myLen = length if add_num > length else add_num
    
    # keep track of which picture we are looking at
    picture_map = range(0, length)
    random.shuffle(picture_map)
    
    for i in range(0, myLen):
        first_attrib = None
        photo_id = 0
        
        while len(picture_map) != 0:
            idx = picture_map.pop()
            first_attrib = pics[0][idx].attrib
            photo_id = int(first_attrib['id'])
            initial_score = computePictureScore(photo_id, mood)
            
            try:
                # throw error if bad things happen
                flickr.photos_getInfo(photo_id=photo_id)
            except Exception:
                continue
            
            try:                    
                # throw error 
                Picture.objects.get(flickr_id=photo_id)
                # photo id already exist in database
                if len(picture_map) == 0:
                    raise Exception('I FAILED!!')
            except Exception:
                break
    
        url = _getURL(first_attrib)
        assert Picture.add(photo_id, url, mood, initialScore=initial_score)
コード例 #6
0
def pullPictures(mood, terms, add_num=0):
    # get pics
    pics = flickr.photos_search(tags=terms,\
                                safe_search=1,\
                                per_page=_SEARCH_NUM)

    # calculate how many pictures to do
    length = len(pics[0])
    myLen = length if add_num > length else add_num

    # keep track of which picture we are looking at
    picture_map = range(0, length)
    random.shuffle(picture_map)

    for i in range(0, myLen):
        first_attrib = None
        photo_id = 0

        while len(picture_map) != 0:
            idx = picture_map.pop()
            first_attrib = pics[0][idx].attrib
            photo_id = int(first_attrib['id'])
            initial_score = computePictureScore(photo_id, mood)

            try:
                # throw error if bad things happen
                flickr.photos_getInfo(photo_id=photo_id)
            except Exception:
                continue

            try:
                # throw error
                Picture.objects.get(flickr_id=photo_id)
                # photo id already exist in database
                if len(picture_map) == 0:
                    raise Exception('I FAILED!!')
            except Exception:
                break

        url = _getURL(first_attrib)
        assert Picture.add(photo_id, url, mood, initialScore=initial_score)
コード例 #7
0
class TestContent(TestCase):
    
    def setUp(self):        
        self.websites = ['www.google.com', 'www.bing.com', 'www.yahoo.com']
        self.content = ContentHandler()
        
        self.m1 = Media(id = 0, content_type = Media.PICTURE)
        self.m1.moods.add(Mood.HAPPY)
        self.m2 = Media(id = 1, content_type = Media.PICTURE)
        self.m2.moods.add(Mood.HAPPY)
        self.m3 = Media(id = 2, content_type = Media.PICTURE)
        self.m3.moods.add(Mood.HAPPY)
        
        self.p1 = Picture(media = self.m1, url = self.websites[0], flickr_id = 0)
        self.p2 = Picture(media = self.m2, url = self.websites[1], flickr_id = 1)
        self.p3 = Picture(media = self.m3, url = self.websites[2], flickr_id = 2)
        
        self.r1 = Rank(media = self.m1, thumbs_up = 0, thumbs_down = 0)
        self.r2 = Rank(media = self.m2, thumbs_up = 1, thumbs_down = 20)
        self.r3 = Rank(media = self.m3, thumbs_up = 7, thumbs_down = 5)
        
        self.m1.save()
        self.m2.save()
        self.m3.save()
        self.p1.save()
        self.p2.save()
        self.p3.save()
        self.r1.save()
        self.r2.save()
        self.r3.save()
        
        pass
    
    def tearDown(self):
        Media.objects.all().delete()
        Picture.objects.all().delete()
        Rank.objects.all().delete()
        pass
    
    '''
    Get Content Test
    '''
    def test_getContent_normalsituation(self):
        mocker = HttpRequest()
        mocker.GET[Media.URL_TAG] = Media.PICTURE
        mocker.GET[Mood.URL_TAG] = Mood.HAPPY
        
        json_str = self.content.read(mocker)
        self.assertTrue(json_str[ApiDataProvider.PARAM_URL] in self.websites)
        mid_arr = [self.m1.id, self.m2.id, self.m3.id]
        self.assertTrue(json_str[ApiDataProvider.MEDIA_ID] in mid_arr)

    def test_getContent_noparameters(self):
        mocker = HttpRequest()
        
        json_str = self.content.read(mocker)
        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str)
    
    def test_getContent_oneparameter(self):
        mocker1 = HttpRequest()
        mocker2 = HttpRequest()
        mocker1.GET[Media.URL_TAG] = Media.PICTURE
        mocker2.GET[Mood.URL_TAG] = Mood.HAPPY
        
        json_str1 = self.content.read(mocker1)
        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str1)
        
        json_str2 = self.content.read(mocker2)
        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str2)
        
    
    def test_getContent_paramoutofbounds(self):
        mocker = HttpRequest()
        mocker.GET[Media.URL_TAG] = 'XX'
        mocker.GET[Mood.URL_TAG] = Mood.HAPPY
        
        json_str = self.content.read(mocker)
        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str)
        
    def test_getContent_emptyDatabase(self):
        mocker = HttpRequest()
        mocker.GET[Media.URL_TAG] = Media.AUDIO
        mocker.GET[Mood.URL_TAG] = Mood.HAPPY
        
        json_str = self.content.read(mocker)
        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str)
コード例 #8
0
class TestRate(TestCase):
    
    def setUp(self):
        self.websites = ['www.google.com', 'www.bing.com', 'www.yahoo.com']
        self.rank = RankHandler()
        
        self.m1 = Media(id = 0, content_type = Media.PICTURE)
        self.m1.moods.add(Mood.HAPPY)
        self.m2 = Media(id = 1, content_type = Media.PICTURE)
        self.m2.moods.add(Mood.HAPPY)
        self.m3 = Media(id = 2, content_type = Media.PICTURE)
        self.m3.moods.add(Mood.HAPPY)
        
        self.p1 = Picture(media = self.m1, url = self.websites[0], flickr_id = 0)
        self.p2 = Picture(media = self.m2, url = self.websites[1], flickr_id = 1)
        self.p3 = Picture(media = self.m3, url = self.websites[2], flickr_id = 2)
        
        self.r1 = Rank(media = self.m1, thumbs_up = 0, thumbs_down = 0)
        self.r2 = Rank(media = self.m2, thumbs_up = 1, thumbs_down = 20)
        self.r3 = Rank(media = self.m3, thumbs_up = 7, thumbs_down = 5)
        
        self.m1.save()
        self.m2.save()
        self.m3.save()
        self.p1.save()
        self.p2.save()
        self.p3.save()
        self.r1.save()
        self.r2.save()
        self.r3.save()
        
        pass
    
    def tearDown(self):
        Media.objects.all().delete()
        Picture.objects.all().delete()
        Rank.objects.all().delete()
        pass
    
    '''
    Rate Content Test
    '''
    def test_rateContent_normalCase_thumbup(self):
        mocker = HttpRequest()
        mocker.POST[Rank.URL_TAG] = Rank.THUMBS_UP
        mocker.POST[ApiDataProvider.MEDIA_ID] = self.m1.id
        
        # previous state
        thumbedup_object = Rank.objects.get(media=self.m1)
        init_thumbup = thumbedup_object.thumbs_up
        init_thumbdown = thumbedup_object.thumbs_down
        
        # do stuff
        json_str = self.rank.create(mocker)
        
        # checks
        self.assertTrue(ApiDataProvider.STATUS_SUCCESS in json_str)
        
        thumbedup_object = Rank.objects.get(media=self.m1)
        self.assertEqual(init_thumbup + 1, thumbedup_object.thumbs_up)
        self.assertEqual(init_thumbdown, thumbedup_object.thumbs_down)
        
    def test_rateContent_normalCase_thumbdown(self):
        mocker = HttpRequest()
        mocker.POST[Rank.URL_TAG] = Rank.THUMBS_DOWN
        mocker.POST[ApiDataProvider.MEDIA_ID] = self.m1.id
        
        # previous state
        thumbeddown_object = Rank.objects.get(media=self.m1)
        init_thumbup = thumbeddown_object.thumbs_up
        init_thumbdown = thumbeddown_object.thumbs_down
        
        # do stuff
        json_str = self.rank.create(mocker)
        
        # checks
        self.assertTrue(ApiDataProvider.STATUS_SUCCESS in json_str)
        
        thumbeddown_object = Rank.objects.get(media=self.m1)
        self.assertEqual(init_thumbup, thumbeddown_object.thumbs_up)
        self.assertEqual(init_thumbdown + 1, thumbeddown_object.thumbs_down)
        
    def test_rateContent_emptyParameter(self):
        mocker = HttpRequest()
        
        json_str = self.rank.create(mocker)
        
        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str)   
        
    def test_rateContent_missingparam(self):
        mocker1 = HttpRequest()
        mocker1.POST[ApiDataProvider.MEDIA_ID] = self.m1
        
        json_str1 = self.rank.create(mocker1)
        
        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str1)    
    
        mocker2 = HttpRequest()
        mocker2.POST[Rank.URL_TAG] = Rank.THUMBS_UP
        
        json_str2 = self.rank.create(mocker2)
        
        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str2)    
    
        
    def test_rateContent_paramOutOfBounds(self):
        mocker = HttpRequest()
        mocker.POST[Rank.URL_TAG] = '2'
        mocker.POST[ApiDataProvider.MEDIA_ID] = '0'
        
        json_str = self.rank.create(mocker)
        
        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str)
        
    def test_rateContent_databaseEmpty(self):
        mocker = HttpRequest()
        mocker.POST[Rank.URL_TAG] = Rank.THUMBS_DOWN
        mocker.POST[ApiDataProvider.MEDIA_ID] = '-1'
        
        json_str = self.rank.create(mocker)
        
        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str)
コード例 #9
0
class TestContent(TestCase):
    def setUp(self):
        self.websites = ['www.google.com', 'www.bing.com', 'www.yahoo.com']
        self.content = ContentHandler()

        self.m1 = Media(id=0, content_type=Media.PICTURE)
        self.m1.moods.add(Mood.HAPPY)
        self.m2 = Media(id=1, content_type=Media.PICTURE)
        self.m2.moods.add(Mood.HAPPY)
        self.m3 = Media(id=2, content_type=Media.PICTURE)
        self.m3.moods.add(Mood.HAPPY)

        self.p1 = Picture(media=self.m1, url=self.websites[0], flickr_id=0)
        self.p2 = Picture(media=self.m2, url=self.websites[1], flickr_id=1)
        self.p3 = Picture(media=self.m3, url=self.websites[2], flickr_id=2)

        self.r1 = Rank(media=self.m1, thumbs_up=0, thumbs_down=0)
        self.r2 = Rank(media=self.m2, thumbs_up=1, thumbs_down=20)
        self.r3 = Rank(media=self.m3, thumbs_up=7, thumbs_down=5)

        self.m1.save()
        self.m2.save()
        self.m3.save()
        self.p1.save()
        self.p2.save()
        self.p3.save()
        self.r1.save()
        self.r2.save()
        self.r3.save()

        pass

    def tearDown(self):
        Media.objects.all().delete()
        Picture.objects.all().delete()
        Rank.objects.all().delete()
        pass

    '''
    Get Content Test
    '''

    def test_getContent_normalsituation(self):
        mocker = HttpRequest()
        mocker.GET[Media.URL_TAG] = Media.PICTURE
        mocker.GET[Mood.URL_TAG] = Mood.HAPPY

        json_str = self.content.read(mocker)
        self.assertTrue(json_str[ApiDataProvider.PARAM_URL] in self.websites)
        mid_arr = [self.m1.id, self.m2.id, self.m3.id]
        self.assertTrue(json_str[ApiDataProvider.MEDIA_ID] in mid_arr)

    def test_getContent_noparameters(self):
        mocker = HttpRequest()

        json_str = self.content.read(mocker)
        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str)

    def test_getContent_oneparameter(self):
        mocker1 = HttpRequest()
        mocker2 = HttpRequest()
        mocker1.GET[Media.URL_TAG] = Media.PICTURE
        mocker2.GET[Mood.URL_TAG] = Mood.HAPPY

        json_str1 = self.content.read(mocker1)
        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str1)

        json_str2 = self.content.read(mocker2)
        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str2)

    def test_getContent_paramoutofbounds(self):
        mocker = HttpRequest()
        mocker.GET[Media.URL_TAG] = 'XX'
        mocker.GET[Mood.URL_TAG] = Mood.HAPPY

        json_str = self.content.read(mocker)
        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str)

    def test_getContent_emptyDatabase(self):
        mocker = HttpRequest()
        mocker.GET[Media.URL_TAG] = Media.AUDIO
        mocker.GET[Mood.URL_TAG] = Mood.HAPPY

        json_str = self.content.read(mocker)
        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str)
コード例 #10
0
class TestRate(TestCase):
    def setUp(self):
        self.websites = ['www.google.com', 'www.bing.com', 'www.yahoo.com']
        self.rank = RankHandler()

        self.m1 = Media(id=0, content_type=Media.PICTURE)
        self.m1.moods.add(Mood.HAPPY)
        self.m2 = Media(id=1, content_type=Media.PICTURE)
        self.m2.moods.add(Mood.HAPPY)
        self.m3 = Media(id=2, content_type=Media.PICTURE)
        self.m3.moods.add(Mood.HAPPY)

        self.p1 = Picture(media=self.m1, url=self.websites[0], flickr_id=0)
        self.p2 = Picture(media=self.m2, url=self.websites[1], flickr_id=1)
        self.p3 = Picture(media=self.m3, url=self.websites[2], flickr_id=2)

        self.r1 = Rank(media=self.m1, thumbs_up=0, thumbs_down=0)
        self.r2 = Rank(media=self.m2, thumbs_up=1, thumbs_down=20)
        self.r3 = Rank(media=self.m3, thumbs_up=7, thumbs_down=5)

        self.m1.save()
        self.m2.save()
        self.m3.save()
        self.p1.save()
        self.p2.save()
        self.p3.save()
        self.r1.save()
        self.r2.save()
        self.r3.save()

        pass

    def tearDown(self):
        Media.objects.all().delete()
        Picture.objects.all().delete()
        Rank.objects.all().delete()
        pass

    '''
    Rate Content Test
    '''

    def test_rateContent_normalCase_thumbup(self):
        mocker = HttpRequest()
        mocker.POST[Rank.URL_TAG] = Rank.THUMBS_UP
        mocker.POST[ApiDataProvider.MEDIA_ID] = self.m1.id

        # previous state
        thumbedup_object = Rank.objects.get(media=self.m1)
        init_thumbup = thumbedup_object.thumbs_up
        init_thumbdown = thumbedup_object.thumbs_down

        # do stuff
        json_str = self.rank.create(mocker)

        # checks
        self.assertTrue(ApiDataProvider.STATUS_SUCCESS in json_str)

        thumbedup_object = Rank.objects.get(media=self.m1)
        self.assertEqual(init_thumbup + 1, thumbedup_object.thumbs_up)
        self.assertEqual(init_thumbdown, thumbedup_object.thumbs_down)

    def test_rateContent_normalCase_thumbdown(self):
        mocker = HttpRequest()
        mocker.POST[Rank.URL_TAG] = Rank.THUMBS_DOWN
        mocker.POST[ApiDataProvider.MEDIA_ID] = self.m1.id

        # previous state
        thumbeddown_object = Rank.objects.get(media=self.m1)
        init_thumbup = thumbeddown_object.thumbs_up
        init_thumbdown = thumbeddown_object.thumbs_down

        # do stuff
        json_str = self.rank.create(mocker)

        # checks
        self.assertTrue(ApiDataProvider.STATUS_SUCCESS in json_str)

        thumbeddown_object = Rank.objects.get(media=self.m1)
        self.assertEqual(init_thumbup, thumbeddown_object.thumbs_up)
        self.assertEqual(init_thumbdown + 1, thumbeddown_object.thumbs_down)

    def test_rateContent_emptyParameter(self):
        mocker = HttpRequest()

        json_str = self.rank.create(mocker)

        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str)

    def test_rateContent_missingparam(self):
        mocker1 = HttpRequest()
        mocker1.POST[ApiDataProvider.MEDIA_ID] = self.m1

        json_str1 = self.rank.create(mocker1)

        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str1)

        mocker2 = HttpRequest()
        mocker2.POST[Rank.URL_TAG] = Rank.THUMBS_UP

        json_str2 = self.rank.create(mocker2)

        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str2)

    def test_rateContent_paramOutOfBounds(self):
        mocker = HttpRequest()
        mocker.POST[Rank.URL_TAG] = '2'
        mocker.POST[ApiDataProvider.MEDIA_ID] = '0'

        json_str = self.rank.create(mocker)

        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str)

    def test_rateContent_databaseEmpty(self):
        mocker = HttpRequest()
        mocker.POST[Rank.URL_TAG] = Rank.THUMBS_DOWN
        mocker.POST[ApiDataProvider.MEDIA_ID] = '-1'

        json_str = self.rank.create(mocker)

        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str)
コード例 #11
0
def pullAndFilter(mood, terms, add_num, partition_num):
    # get pics
    pics = flickr.photos_search(tags=terms,\
                                safe_search=1,\
                                per_page=_SEARCH_NUM)
    
    # calculate how many pictures to do
    length = len(pics[0])
    
    # myLen = min(partition_num, length)
    myLen = length if partition_num > length else partition_num
    myLen = max(myLen, 0)
    
    #generate a random index
    startIndex = random.randint(0, max(length - myLen, 0))
    
    #generate map for keeping track
    picture_map = range(0, myLen)
    random.shuffle(picture_map)
    
    added_picture_list = []
    
    
    #looping myLen times, get out n number of pictures
    for i in range(0, myLen):
        first_attrib = None
        photo_id = 0
        
        print 'picture progress: ' + str(i) + '/' + str(myLen)
        while len(picture_map) != 0:
            idx = (picture_map.pop() + startIndex) % length
            first_attrib = pics[0][idx].attrib
            photo_id = int(first_attrib['id'])
            initial_score = computePictureScore(photo_id, mood)
            
            del_list = Deleted.objects.filter(content_type=Media.PICTURE, content_id=photo_id)
            if len(del_list) != 0:
                continue
            
            try:
                # throw error if bad things happen
                flickr.photos_getInfo(photo_id=photo_id)
            except Exception:
                continue
            
            try:                    
                # throw error 
                Picture.objects.get(flickr_id=photo_id)
                # photo id already exist in database
                if len(picture_map) == 0:
                    raise Exception('I FAILED!!')
            except Exception:
                break
    
        try:
            url = _getURL(first_attrib)
            added_picture_list.append((photo_id, url, mood, initial_score))
        except Exception:
            print 'The None type url error again, die gracefully'
        
    sorted_picture_list = sorted(added_picture_list, key=lambda tuple: tuple[3], reverse=True)
    
    # add till database is full
    preadd = min(add_num, len(sorted_picture_list))
    for i in range(0, preadd):
        tuple = sorted_picture_list.pop(0)
        assert Picture.add(tuple[0], tuple[1], tuple[2], initialScore=tuple[3])

    # get media data    
    medias = Media.objects.filter(moods=mood, content_type=Media.PICTURE)
    ScoreFilter._calculateFinalScore(medias)
    
    mediaData = Media.objects.filter(moods=mood, score__final_score__gt=-1).order_by('score__final_score')
#    allData = Media.objects.filter(moods=mood, content_type=Media.PICTURE)
#    dataDict = {}
#    for m in allData:
#        if m.score.final_score == -1:
#            dataDict[m] = m.score.initial_score
#        else:
#            dataDict[m] = m.score.final_score
#    
#    for key, value in sorted(dataDict.iteritems(), key=lambda (k,v): (v,k)):
#        mediaData.append(key)
                                        
    if len(mediaData) == 0:
        return preadd
    
    #Add to database if score is higher than existing scores
    lowestMediaIndex = 0
    while len(sorted_picture_list) != 0:
        tuple = sorted_picture_list.pop(0)
        score = tuple[3]
        if score > mediaData[lowestMediaIndex].score.final_score:
            destory(mediaData[lowestMediaIndex].id, Media.PICTURE)
            assert Picture.add(tuple[0], tuple[1], tuple[2], initialScore=tuple[3])
            
            lowestMediaIndex += 1
            if lowestMediaIndex >= len(mediaData):
                break
        else:
            break
        
    return preadd + lowestMediaIndex
コード例 #12
0
class SimpleTest(TestCase):
    def setUp(self):
        self.websites = ['www.google.com', 'www.bing.com', 'www.yahoo.com']
        self.m1 = Media(id = 0, content_type = Media.PICTURE)
        self.m1.moods.add(Mood.HAPPY)
        self.m2 = Media(id = 1, content_type = Media.PICTURE)
        self.m2.moods.add(Mood.HAPPY)
        self.m3 = Media(id = 2, content_type = Media.PICTURE)
        self.m3.moods.add(Mood.HAPPY)
        
        self.p1 = Picture(media = self.m1, url = self.websites[0], flickr_id = 0)
        self.p2 = Picture(media = self.m2, url = self.websites[1], flickr_id = 1)
        self.p3 = Picture(media = self.m3, url = self.websites[2], flickr_id = 2)
        
        self.r1 = Rank(media = self.m1, thumbs_up = 0, thumbs_down = 0)
        self.r2 = Rank(media = self.m2, thumbs_up = 1, thumbs_down = 20)
        self.r3 = Rank(media = self.m3, thumbs_up = 7, thumbs_down = 5)
        
        self.m1.save()
        self.m2.save()
        self.m3.save()
        self.p1.save()
        self.p2.save()
        self.p3.save()
        self.r1.save()
        self.r2.save()
        self.r3.save()
        
        pass
    
    def tearDown(self):
        Media.objects.all().delete()
        Picture.objects.all().delete()
        Rank.objects.all().delete()
        pass
    
    '''
    Testing Get Content
    '''
    def test_getContent_normalsituation(self):
        json_str = ApiDataProvider.getContent(Mood.HAPPY, Media.PICTURE)
        self.assertTrue(json_str[ApiDataProvider.PARAM_URL] in self.websites)
        mid_arr = [self.m1.id, self.m2.id, self.m3.id]
        self.assertTrue(json_str[ApiDataProvider.MEDIA_ID] in mid_arr)
        
    def test_getContent_emptyMood(self):
        json_str = ApiDataProvider.getContent(Mood.EXCITED, Media.PICTURE)
        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str)
    
    def test_getContent_emptyContent(self):
        json_str = ApiDataProvider.getContent(Mood.HAPPY, Media.TEXT)
        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str)
        
    def test_getContent_emptyBoth(self):
        json_str = ApiDataProvider.getContent(Mood.HAPPY, Media.TEXT)
        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str)
        
    def test_getContent_baddatabase_nopictures(self):
        Picture.objects.all().delete()
        json_str = ApiDataProvider.getContent(Mood.HAPPY, Media.PICTURE)
        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str)
        
    def test_getContent_outofboundsinput(self):
        with self.assertRaises(AssertionError) as err:
            ApiDataProvider.getContent(4, Media.PICTURE)
        with self.assertRaises(AssertionError) as err:  
            ApiDataProvider.getContent(Mood.HAPPY, 4)
        
    '''
    Testing Rate Content
    '''
    def test_rateContent_normalsituation_up(self):
        json_str = ApiDataProvider.rateContent(self.m1.id, Rank.THUMBS_UP)
        self.assertTrue(ApiDataProvider.STATUS_SUCCESS in json_str) 
        content0 = Rank.objects.get(media = self.m1)
        self.assertEqual(1, content0.thumbs_up)
        self.assertEqual(0, content0.thumbs_down)
        
    def test_rateContent_normalsituation_up_twice(self):
        json_str = ApiDataProvider.rateContent(self.m1.id, Rank.THUMBS_UP)
        self.assertTrue(ApiDataProvider.STATUS_SUCCESS in json_str)
        json_str = ApiDataProvider.rateContent(self.m1.id, Rank.THUMBS_UP)
        self.assertTrue(ApiDataProvider.STATUS_SUCCESS in json_str)
         
        content0 = Rank.objects.get(media = self.m1)
        self.assertEqual(2, content0.thumbs_up)
        self.assertEqual(0, content0.thumbs_down)
           
    def test_rateContent_normalsituation_down(self):
        json_str = ApiDataProvider.rateContent(self.m1.id, Rank.THUMBS_DOWN)
        self.assertTrue(ApiDataProvider.STATUS_SUCCESS in json_str)
        
        content0 = Rank.objects.get(media=self.m1)
        self.assertEqual(0, content0.thumbs_up)
        self.assertEqual(1, content0.thumbs_down)
             
    def test_rateContent_baddatabase_empty(self):
        json_str = ApiDataProvider.rateContent(-1, Rank.THUMBS_DOWN)
        self.assertTrue(ApiDataProvider.STATUS_ERROR in json_str)
        
    def test_rateContent_illegalInput_wrongmid(self):
        with self.assertRaises(AssertionError) as err:
            ApiDataProvider.rateContent('0', Rank.THUMBS_DOWN)
        with self.assertRaises(AssertionError) as err:  
            ApiDataProvider.rateContent('abc', Rank.THUMBS_UP)
    
    def test_rateContent_illegalInput_wrongthumbing(self):
        with self.assertRaises(AssertionError) as err:
            ApiDataProvider.rateContent(0, 2)
コード例 #13
0
def pullAndFilter(mood, terms, add_num, partition_num):
    # get pics
    pics = flickr.photos_search(tags=terms,\
                                safe_search=1,\
                                per_page=_SEARCH_NUM)

    # calculate how many pictures to do
    length = len(pics[0])

    # myLen = min(partition_num, length)
    myLen = length if partition_num > length else partition_num
    myLen = max(myLen, 0)

    #generate a random index
    startIndex = random.randint(0, max(length - myLen, 0))

    #generate map for keeping track
    picture_map = range(0, myLen)
    random.shuffle(picture_map)

    added_picture_list = []

    #looping myLen times, get out n number of pictures
    for i in range(0, myLen):
        first_attrib = None
        photo_id = 0

        print 'picture progress: ' + str(i) + '/' + str(myLen)
        while len(picture_map) != 0:
            idx = (picture_map.pop() + startIndex) % length
            first_attrib = pics[0][idx].attrib
            photo_id = int(first_attrib['id'])
            initial_score = computePictureScore(photo_id, mood)

            del_list = Deleted.objects.filter(content_type=Media.PICTURE,
                                              content_id=photo_id)
            if len(del_list) != 0:
                continue

            try:
                # throw error if bad things happen
                flickr.photos_getInfo(photo_id=photo_id)
            except Exception:
                continue

            try:
                # throw error
                Picture.objects.get(flickr_id=photo_id)
                # photo id already exist in database
                if len(picture_map) == 0:
                    raise Exception('I FAILED!!')
            except Exception:
                break

        try:
            url = _getURL(first_attrib)
            added_picture_list.append((photo_id, url, mood, initial_score))
        except Exception:
            print 'The None type url error again, die gracefully'

    sorted_picture_list = sorted(added_picture_list,
                                 key=lambda tuple: tuple[3],
                                 reverse=True)

    # add till database is full
    preadd = min(add_num, len(sorted_picture_list))
    for i in range(0, preadd):
        tuple = sorted_picture_list.pop(0)
        assert Picture.add(tuple[0], tuple[1], tuple[2], initialScore=tuple[3])

    # get media data
    medias = Media.objects.filter(moods=mood, content_type=Media.PICTURE)
    ScoreFilter._calculateFinalScore(medias)

    mediaData = Media.objects.filter(
        moods=mood, score__final_score__gt=-1).order_by('score__final_score')
    #    allData = Media.objects.filter(moods=mood, content_type=Media.PICTURE)
    #    dataDict = {}
    #    for m in allData:
    #        if m.score.final_score == -1:
    #            dataDict[m] = m.score.initial_score
    #        else:
    #            dataDict[m] = m.score.final_score
    #
    #    for key, value in sorted(dataDict.iteritems(), key=lambda (k,v): (v,k)):
    #        mediaData.append(key)

    if len(mediaData) == 0:
        return preadd

    #Add to database if score is higher than existing scores
    lowestMediaIndex = 0
    while len(sorted_picture_list) != 0:
        tuple = sorted_picture_list.pop(0)
        score = tuple[3]
        if score > mediaData[lowestMediaIndex].score.final_score:
            destory(mediaData[lowestMediaIndex].id, Media.PICTURE)
            assert Picture.add(tuple[0],
                               tuple[1],
                               tuple[2],
                               initialScore=tuple[3])

            lowestMediaIndex += 1
            if lowestMediaIndex >= len(mediaData):
                break
        else:
            break

    return preadd + lowestMediaIndex