def testquery(request):
    # creates dummy posts

    p1 = Post(
        url="boobies",
        date="",
        last_track=datetime.datetime.utcnow().replace(tzinfo=utc),
        image="",
        note_count=2,
        note_inc=165,
        text="big",
    )
    p1.save()

    tracking1 = Tracking(
        post=p1, timestamp=datetime.datetime.utcnow().replace(tzinfo=utc), sequence=0, increment=0, count=0
    )
    tracking1.save()
    print(tracking1.post.url)

    # p2=Post(url='ilikecouches', date='', image='', note_count=2, note_inc=33, text='sofa')
    # p2.save()
    # p3=Post(url='foodporn', date='', image='', note_count=2, note_inc=66, text='bbq')
    # p3.save()
    # p4=Post(url='artandstuff', date='', image='', note_count=2, note_inc=0, text='hipster')
    # p4.save()

    # create a blog
    b = Blog(host_name="MOFOBITCH", timestamp=datetime.datetime.utcnow().replace(tzinfo=utc))
    b.save()
    # add the dummy posts to the blog's likes field
    # b.likes.add(p1,p2,p3,p4)
    # query the blog 'MOFOBITCH' and sort all of its likes field by note_inc in descending
    # order, and return a QuerySet of the top 3 note_inc posts
    # b2=Blog.objects.get(host_name='MOFOBITCH').likes.order_by('-note_inc')[0:3]
    # for e in b2:
    # print(e)
    return HttpResponse(200)
Ejemplo n.º 2
0
def _parse_post_json(blog_host_name, liked_post_json):
    ''' Takes the json from a post and extracts all its juicy goodness, then
        makes a database entry for it, if necessary.'''
    
    # Check the last time this post was tracked and ignore if 
    # it's within the last hour.
    new_post = not Post.objects.filter(post_id = liked_post_json['id']).exists()
    
    if (not new_post):
        this_post = Post.objects.get(post_id = liked_post_json['id'])
        deltatime = datetime.datetime.now().replace(tzinfo=utc) - \
            this_post.last_track.replace(tzinfo=utc)            
        if (deltatime.total_seconds()/pv.tracking_interval < 1):
            return 0 
    
    # TODO: this could return an error if the post in our database belongs to another blog!!!!!
    blog_obj = Blog.objects.get(host_name = blog_host_name)
    
    # Example: liked_post_json['post_url'] to get the url
    post_id = liked_post_json['id']
    post_url = liked_post_json['post_url']
    post_date = convert_date(liked_post_json['date'])
    post_count = liked_post_json['note_count']
    current_datetime = datetime.datetime.now().replace(tzinfo=utc)
    
    # Different posts have different types of text fields.
    text_field = {"text": "body",
                  "chat": "body",
                  "photo": "caption",
                  "link": "description",
                  "quote": "source",
                  "answer": "answer",
                  "audio": "caption",
                  "video": "caption"}
    # Set default image.
    def_img = "http://www.athgo.org/ablog/wp-content/uploads/2013/02/tumblr_logo.png"
                  
    img_field = {"text": lambda: def_img,
                  "chat": lambda: def_img,
                  "photo": lambda:liked_post_json['photos'][0]['alt_sizes'][0]['url'],               
                  "link": lambda: def_img,
                  "quote": lambda: def_img,
                  "answer": lambda: def_img,
                  "audio": lambda: liked_post_json.get('album_art', def_img),
                  "video": lambda: def_img}
    updated_times_tracked = 1              
    img = img_field[liked_post_json['type']]()
    txt = strip_tags(liked_post_json[text_field[liked_post_json['type']]].encode('utf-8'))
    
    # Reduce text length if too long.
    if len(txt) > 100:
        txt = txt[:100] + "..."
    
    if (new_post):
        post_obj = Post(post_id = post_id,
                        url = post_url,
                        date = post_date,
                        last_track = current_datetime,
                        times_tracked = 1,
                        image = img,
                        note_count = post_count,
                        note_inc = 0,
                        text = txt)
        
        post_obj.save()
        blog_obj.likes.add(post_obj)  
        blog_obj.save()
    
    else:
        # update note_count and last_track.
        post_obj = Post.objects.get(post_id=post_id)
        prev_count = post_obj.note_count
        updated_times_tracked = post_obj.times_tracked + 1
        Post.objects.filter(post_id=post_id).update(times_tracked = updated_times_tracked, 
                                                 note_inc = post_count - prev_count,
                                                 note_count = post_count,
                                                 last_track = current_datetime)
    # Create new tracking object for post.
    tracking = Tracking(post = post_obj,
                        timestamp = current_datetime,
                        increment = post_obj.note_inc,
                        sequence = updated_times_tracked,
                        count = post_obj.note_count)
    tracking.save()
    
    return 0