Skip to content

Commit

Permalink
code optimized
Browse files Browse the repository at this point in the history
  • Loading branch information
Anoopsmohan committed Mar 4, 2013
1 parent 1ec23a6 commit 71b99f1
Showing 1 changed file with 41 additions and 34 deletions.
75 changes: 41 additions & 34 deletions user_info/views.py
@@ -1,6 +1,5 @@
import requests

from django.contrib import messages
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template.loader import render_to_string
Expand All @@ -9,47 +8,55 @@


def home(request):
""" Render home page """
return render_to_response('home.html')

def get_info(limit):
""" Fetch info by using vimeo API's and store it in database"""
base_id = 506307
count = 0
while(1):
try:
base_id += 1
info = requests.get('http://vimeo.com/api/v2/%s/info.json' % str(base_id)).json()
if UserDetails.objects.filter(profile_url=info['profile_url']).exists():
continue
user_info = UserDetails()
user_info.name = info['display_name']
user_info.profile_url = info['profile_url']
user_info.paying = int(info['is_plus'])
user_info.staff_pick = False
if int(info['total_channels']) > 0:
channel_info = requests.get('http://vimeo.com/api/v2/%s/channels.json' % str(base_id)).json()
for i in channel_info:
if i['id'] == 927:
user_info.staff_pick = True
break
else:
user_info.staff_pick = False
if int(info['total_videos_uploaded']) >= 0:
user_info.video_uploaded = 1
else:
user_info.video_uploaded = 0
user_info.save()
count += 1
if count >= limit:
break
except:
continue

def delete_info():
""" Delete user details from database """
UserDetails.objects.all().delete()


def fetch_info(request):
if request.GET.get('limit'):
base_id = 406307
count = 0
while(1):
try:
base_id += 1
info = requests.get('http://vimeo.com/api/v2/%s/info.json' % str(base_id)).json
if UserDetails.objects.filter(profile_url=info['profile_url']).exists():
continue
user_info = UserDetails()
user_info.name = info['display_name']
user_info.profile_url = info['profile_url']
user_info.paying = int(info['is_plus'])
user_info.staff_pick = False
if int(info['total_channels']) > 0:
channel_info = requests.get('http://vimeo.com/api/v2/%s/channels.json' % str(base_id)).json
for i in channel_info:
if i['id'] == 927:
user_info.staff_pick = True
break
else:
user_info.staff_pick = False
if int(info['total_videos_uploaded']) >= 0:
user_info.video_uploaded = 1
else:
user_info.video_uploaded = 0
user_info.save()
count += 1
if count >= int(request.GET.get('limit')):
break
except:
continue
get_info(int(request.GET.get('limit')))
return HttpResponse('User Details added')

elif int(request.GET.get('clean')) == 1:
UserDetails.objects.all().delete()
messages.success(request, 'User details removed from the database')
delete_info()
return HttpResponse('User Details Deleted')


Expand Down

0 comments on commit 71b99f1

Please sign in to comment.