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)
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)
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)
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)
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
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