Commits

ronald martinez committed dffdfe1

ok

  • Participants
  • Parent commits a61a694

Comments (0)

Files changed (4)

 import json
 import urllib
 import time
+import logging
 
 from datetime import datetime
 from tasks import get_likes
             urllib.urlencode(params)
         )
 
+    #logging.info("url: %s" % url)
     print "url: %s" % url
 
     try:
         data = json.loads(urllib.urlopen(url).read())
     except IOError as exc:
-        print exc
-        time.sleep(5)
-        print "again"
+        logging.info(exc)
+        time.sleep(settings.SECONDS_RETRY_POST)
         posts(url)
 
     items = data.get('data')
-    paging = data.get('paging')
 
     if items:
         for x in items:
                 get_likes.delay(
                     post_id=x.get('id'),
                     total_likes=x.get('likes').get('count')
-                    #total_likes=45
                 )
 
+        paging = data.get('paging')
+
         if 'next' in paging:
-            if not settings.STOP_NEXT_PAGE:
-                posts(url=paging.get('next'))
-    else:
-        print "finicsh"
+            #if not settings.STOP_NEXT_PAGE:
+            posts(url=paging.get('next'))
 
 if __name__ == "__main__":
     tstart = datetime.now()
     posts()
     tend = datetime.now()
+    logging.info('estimated_time: %s' % (tend - tstart))
 ACCESS_TOKEN = ''
 BROKER_URL = "mongodb://localhost:27017/<Your Database>"
 
+SECONDS_RETRY_POST = 5
+
 try:
     from local_settings import *
 except ImportError:
 
 @receiver(event_log)
 def notify_finish_post(sender, time, post_id, **kwargs):
-    #print "insert.."
     fp = open('events.log', 'ar')
     message = '%s: %s\n' % (post_id, time)
     fp.write(message)
 @celery.task
 def get_likes(post_id, url=None, start_time=None, total_likes=0):
 
-    total_likes = total_likes - 1 #facebook format
-
     if not start_time:
         start_time = time.time()
     else:
             urllib.urlencode(params)
         )
 
-    #print "url_detail: %s" % url
-
     data = json.loads(urllib.urlopen(url).read())
-
     likes = data.get('data')
-    paging = data.get('paging')
 
     if likes:
 
-        for i, like in enumerate(likes):
+        for like in likes:
 
             user_id = like.get('id')
             item = db.items.find_one({'user_id': user_id})
+            count = 1
 
             if not item:
                 db.items.insert(dict(
                     user_id=user_id,
-                    count=1
+                    count=count
                 ))
             else:
                 db.items.update(
                     {'user_id': user_id},
-                    {'$inc': {'count': 1}}
+                    {'$inc': {'count': count}}
                 )
 
             post = db.progress.find_one({'post_id': post_id})
             count = int(post.get('count')) + 1 if post else 1
-            percent = (count * 100) / total_likes
 
             if count > total_likes:
                 return
 
+            if total_likes > 1:
+                total_likes = total_likes - 1
+            else:
+                total_likes = 1
+
+            percent = (count * 100) / total_likes
+
             if not post:
                 db.progress.insert(dict(
                     post_id=post_id,
                     }
                 )
 
-            print 'count: %d, percent: %d, total_likes : %d' % (
-                count,
-                percent,
-                total_likes
-            )
+        paging = data.get('paging')
 
         if 'next' in paging:
             get_likes.delay(

templates/index.html

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
 <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
 <script>
-    $.getJSON('/progress_bar', function(data) {
-        $.each(data, function(key, values) {
-        for (x in values){
-            post_id = values[x]['post']
-            percent = values[x]['percent']
-            $("#progressbar_" + post_id).progressbar({ value: 0 });
-            setTimeout(updateProgress, 500, post_id);
-        }});
-    });
 
-    function updateProgress(id) {
+    function add_progress()
+    {
+        $.getJSON('/progress_bar', function(data) {
+            $.each(data, function(key, values) {
+            for (x in values){
+                post_id = values[x]['post']
+                percent = values[x]['percent']
+                $("#progressbar_" + post_id).progressbar({ value: 0 });
+                setTimeout(update_progress, 500, post_id);
+            }});
+        });
+
+        setTimeout(add_progress, 5000);
+    }
+
+    function update_progress(id) {
         $.getJSON('/get_percent/' + id, function(data) {
             $.each(data, function(key, value) {
                 progress = parseInt(value)
                 $('#progressbar_' + id).progressbar("option", "value", progress);
                 if (progress < 100){
-                    setTimeout(updateProgress, 500, id);
+                    setTimeout(update_progress, 500, id);
                 }
             });
         })
     }
+
+    add_progress()
+
 </script>
 </head>
 
 <body>
-    <table id="bar" border="1" width="100%">
+    <table id="bar" border="1" width="50%">
+        <tr>
+            <td>Nro</td>
+            <td>ID</td>
+            <td>Progress</td>
+        </tr>
         {% for n, row in items %}
         <tr>
             <td width="5%">{{ n+1 }}</td>
-            <td width="30%">{{ row.post_id }}</td>
+            <td width="30%"><font size="1">{{ row.post_id }}</font></td>
             <td><div style="width: 100%;"  id="progressbar_{{ row.post_id }}"></div></td>
         </tr>
         {% endfor %}