def fblogin(request): codeobject = request.GET.get('code') if (codeobject is None): parameters = {} parameters['client_id'] = '377058139053021' parameters['redirect_uri'] = 'http://www.dronesclub.in/fblogin' parameters['state'] = 'whatho' parameters['scope'] = 'user_birthday,read_stream' parametersString = urllib.urlencode(parameters) urlstring = "https://www.facebook.com/dialog/oauth?" + parametersString print "URLSTRING is " + urlstring return HttpResponseRedirect(urlstring) else: #exchange the code for an access token parameters = {} parameters['client_id'] = '377058139053021' parameters['redirect_uri'] = 'http://www.dronesclub.in/fblogin' parameters['client_secret'] = '44fc382d9399610f1eaccd153ba9083c' parameters['code'] = codeobject parametersString = urllib.urlencode(parameters) urlstring = "https://graph.facebook.com/oauth/access_token?" + parametersString response = urllib2.urlopen(urlstring) content = response.read() contentPattern = re.compile(r'access_token=(.*?)&expires=.*') accessToken = contentPattern.search(content).groups()[0] urlstring = 'https://graph.facebook.com/me?access_token=' + accessToken userDetailsJson = urllib2.urlopen(urlstring) fb_response = userDetailsJson.read() json_object = json.loads(fb_response) username = '******' + json_object['id'] password = username emailid = json_object['username'] + '@facebook.com' userobject = User.objects.create(username=username, password=password, emailid=emailid) request.session['username'] = username request.session['userobject'] = userobject return books(request)
def fblogin(request): codeobject = request.GET.get('code') if(codeobject is None): parameters = {} parameters['client_id'] = '377058139053021' parameters['redirect_uri'] = 'http://www.dronesclub.in/fblogin' parameters['state'] = 'whatho' parameters['scope'] = 'user_birthday,read_stream' parametersString = urllib.urlencode(parameters) urlstring = "https://www.facebook.com/dialog/oauth?" + parametersString print "URLSTRING is " + urlstring return HttpResponseRedirect(urlstring) else: #exchange the code for an access token parameters = {} parameters['client_id'] = '377058139053021' parameters['redirect_uri'] = 'http://www.dronesclub.in/fblogin' parameters['client_secret'] = '44fc382d9399610f1eaccd153ba9083c' parameters['code'] = codeobject parametersString = urllib.urlencode(parameters) urlstring = "https://graph.facebook.com/oauth/access_token?" + parametersString response = urllib2.urlopen(urlstring) content = response.read() contentPattern = re.compile(r'access_token=(.*?)&expires=.*') accessToken = contentPattern.search(content).groups()[0] urlstring = 'https://graph.facebook.com/me?access_token=' + accessToken userDetailsJson = urllib2.urlopen(urlstring) fb_response = userDetailsJson.read() json_object = json.loads(fb_response) username = '******' + json_object['id'] password = username emailid = json_object['username'] + '@facebook.com' userobject = User.objects.create(username=username, password=password, emailid=emailid) request.session['username'] = username request.session['userobject'] = userobject return books(request)
def addvolume(request, isbn10): ''' Add the given isbn10 based volume to user's shelf ''' u = request.session.get('username', '') if not u: return render_to_response("signup.html") userobject = User.objects.get(username=u) volume = Volume.objects.get(isbn10=isbn10) try: item = Item.objects.get(volume=volume, bookowner=userobject) except Item.DoesNotExist: item = Item.objects.create(volume=volume, bookowner=userobject) item.save() except: #do nothing print "swallowing exception" return books(request)
def addvolume(request, isbn10): ''' Add the given isbn10 based volume to user's shelf ''' u = request.session.get('username', '') if not u: return render_to_response("signup.html") userobject = User.objects.get(username=u) volume = Volume.objects.get(isbn10=isbn10) try: item = Item.objects.get(volume=volume, bookowner=userobject) except Item.DoesNotExist: item = Item.objects.create(volume = volume, bookowner=userobject) item.save() except: #do nothing print "swallowing exception" return books(request)
def addbook(request): u = request.session.get('username', '') userobject = User.objects.get(username=u) if not u: return render_to_response("signup.html") title = request.POST.get('title', '') googlebooksurl = 'https://www.googleapis.com/books/v1/volumes?q=%s&key=AIzaSyAh9amDouy8_zI-teUcigYfYkisrXYOc1s' % urllib2.quote( title) response = urllib2.urlopen(googlebooksurl) content = response.read() json_object = json.loads(content) bookvolumes = [] isbn10s = [] for volume in json_object["items"]: x = volume["volumeInfo"] title = "" authors = [] thumbnail = "" isbn10 = "" if "title" in x: title = x["title"] if "authors" in x: authors = x["authors"] if "imageLinks" in x: imageLinks = x["imageLinks"] if "thumbnail" in imageLinks: thumbnail = imageLinks["smallThumbnail"] if "industryIdentifiers" in x: for object in x["industryIdentifiers"]: if object["type"] == "ISBN_10": isbn10 = object["identifier"] if isbn10 in isbn10s: continue else: isbn10s.append(isbn10) authorsString = "" first = True for author in authors: if first == False: authorsString += "," first = False authorsString += author volume = "" existingVolumes = Volume.objects.filter(isbn10=isbn10) if not existingVolumes: volume = Volume.objects.create(title=title, authors=authorsString, isbn10=isbn10, imageurl=thumbnail) volume.save() else: volume = existingVolumes[0] bookvolumes.append(volume) print bookvolumes return render_to_response("addbooks.html", {"bookvolumes": bookvolumes[0:9]}) return (books(request))
def addbook(request): u = request.session.get('username', '') userobject = User.objects.get(username=u) if not u: return render_to_response("signup.html") title = request.POST.get('title','') googlebooksurl = 'https://www.googleapis.com/books/v1/volumes?q=%s&key=AIzaSyAh9amDouy8_zI-teUcigYfYkisrXYOc1s' % urllib2.quote(title) response = urllib2.urlopen(googlebooksurl) content = response.read() json_object = json.loads(content) bookvolumes = [] isbn10s = [] for volume in json_object["items"]: x = volume["volumeInfo"] title = "" authors = [] thumbnail = "" isbn10 = "" if "title" in x: title = x["title"] if "authors" in x: authors = x["authors"] if "imageLinks" in x: imageLinks = x["imageLinks"] if "thumbnail" in imageLinks: thumbnail = imageLinks["smallThumbnail"] if "industryIdentifiers" in x: for object in x["industryIdentifiers"]: if object["type"] == "ISBN_10": isbn10 = object["identifier"] if isbn10 in isbn10s: continue else: isbn10s.append(isbn10) authorsString = "" first = True for author in authors: if first == False: authorsString += "," first = False authorsString += author volume = "" existingVolumes = Volume.objects.filter(isbn10 = isbn10) if not existingVolumes: volume = Volume.objects.create(title=title, authors=authorsString, isbn10=isbn10, imageurl=thumbnail) volume.save() else: volume = existingVolumes[0] bookvolumes.append(volume) print bookvolumes return render_to_response("addbooks.html",{ "bookvolumes": bookvolumes[0:9] }) return (books(request))