Esempio n. 1
0
def retrieve_all_events_media(request):
    artists = Artist.objects.filter(name__in=interesting_artists)
    artists_ids = [a.id for a in artists]

    for event in Event.objects.filter(datetime__lt=timezone.now(), artists__in=artists_ids
            #, id__gt=1086
                                      ):
        if not Media.objects.filter(event=event.id):
            print "log >>> processing event", event.title
            retrieved_tags = list()

            for e in event.title.replace(",", " ").replace("@", " ").split():
                retrieved_tags.append(e.lower())

            lat = event.venue.latitude
            lng = event.venue.longitude

            start = event.datetime - std_timedelta(hours=3)
            end = event.datetime + std_timedelta(hours=15)

            start_epoch = int(time.mktime(start.timetuple()))
            end_epoch = int(time.mktime(end.timetuple()))

            medias = api.media_search('', 1000, lat, lng, start_epoch, end_epoch)

            for media in medias:
                #if main_tag in [mt.name for mt in medium.tags]:
                    if [mt for mt in media.tags if mt.name in retrieved_tags]:
                        #print "\n", media.tags
                        db_media = Media.objects.filter(instagram_id=media.id)
                        if not db_media:
                            media_object = Media()
                            media_object.instagram_id = media.id
                            media_object.std_resolution_url = media.images['standard_resolution'].url
                            media_object.low_resolution_url = media.images['low_resolution'].url
                            media_object.thumbnail_url = media.images['thumbnail'].url
                            media_object.location = media.location
                            if media.location.point:
                                media_object.longitude = media.location.point.longitude
                                media_object.latitude = media.location.point.latitude
                            media_object.created_time = media.created_time
                            media_object.like_count = media.like_count
                            media_object.event = event.id
                            media_object.save()

    return render(request, 'instaPhotoCollector/index.html')
Esempio n. 2
0
def explore_event(request, event_id, page):
    context = {}
    image_objects = list()
    retrieved_tags = list()

    event = Event.objects.get(id=event_id)

    # fixme : instagram query search for given date and place by our time zone, make time zone aware

    event_medias = Media.objects.filter(event=event_id)

    if event_medias.count() == 0:
        print " >>> retrieving images for event", event.id
        for e in event.title.replace(",", " ").replace("@", " ").split():
            retrieved_tags.append(e.lower())
        #main_tag = retrieved_tags[0]
        #retrieved_tags.remove(main_tag)

        lat = event.venue.latitude
        lng = event.venue.longitude

        start = event.datetime - std_timedelta(hours=12)
        end = event.datetime + std_timedelta(hours=12)

        start_epoch = int(time.mktime(start.timetuple()))
        end_epoch = int(time.mktime(end.timetuple()))

        # todo: understand what's first parameter for api.media_search()
        medias = api.media_search('', 1000, lat, lng, start_epoch, end_epoch)

        for media in medias:
            #if main_tag in [mt.name for mt in medium.tags]:
                if [mt for mt in media.tags if mt.name in retrieved_tags]:
                    #print "\n", media.tags
                    db_media = Media.objects.filter(instagram_id=media.id)
                    if not db_media:
                        media_object = Media()
                        media_object.instagram_id = media.id
                        media_object.std_resolution_url = media.images['standard_resolution'].url
                        media_object.low_resolution_url = media.images['low_resolution'].url
                        media_object.thumbnail_url = media.images['thumbnail'].url
                        media_object.location = media.location
                        if media.location.point:
                            media_object.longitude = media.location.point.longitude
                            media_object.latitude = media.location.point.latitude
                        media_object.created_time = media.created_time
                        media_object.like_count = media.like_count
                        media_object.event = event_id
                        media_object.save()
                        #print media_object

        event_medias = Media.objects.filter(event=event_id)
    else:
        print " >>> images for event", event.id, "already on DB"

    number_of_element = event_medias.count()
    print " >>> retrieved", number_of_element, "images"
    number_of_pages = number_of_element / MEDIA_PER_PAGE
    if number_of_element % MEDIA_PER_PAGE != 0:
        number_of_pages += 1

    start_number = (int(page) - 1) * MEDIA_PER_PAGE
    end_number = int(page) * MEDIA_PER_PAGE

    event_medias = event_medias[start_number:end_number]

    for event_media in event_medias:
        image = {}
        image['image_url'] = event_media.std_resolution_url

        image_objects.append(image)

    context['event_id'] = event_id
    context['event_title'] = event.title
    context['image_objects'] = image_objects

    context['range'] = range(1, number_of_pages + 1)
    context['first_page'] = 1
    context['page'] = page
    context['last_page'] = number_of_pages

    return render(request, 'instaPhotoCollector/event.html', context)