def createPicsule(request): docfile = request.FILES['docfile'] newDoc = Document(docfile=docfile) newDoc.save() newDoc = Document.objects.get(id=newDoc.id) ret = {} try: imageObj = Image.open('myproject' + newDoc.docfile.url) info = imageObj._getexif() if info is not None: for tag, value in info.items(): decoded = TAGS.get(tag, tag) if decoded == "GPSInfo": gps_data = {} for t in value: sub_decoded = GPSTAGS.get(t, t) gps_data[sub_decoded] = value[t] lat,long = get_lat_lon(gps_data) ret["Latitude"] = lat; ret["Longitude"] = long; else: ret[decoded] = value except: print "error occured while tryin to grab image exif" #get top songs date = ret.get("DateTime") chart = None if date is not None: dateObj = datetime.datetime.strptime(date, '%Y:%m:%d %H:%M:%S') weekday = dateObj.weekday() #billboard only supports links on a saturday if weekday <= 1 or weekday == 6: while dateObj.weekday() != 5: dateObj = dateObj + datetime.timedelta(days=-1) else: while dateObj.weekday() != 5: dateObj = dateObj + datetime.timedelta(days=1) formattedDate = dateObj.strftime("%Y-%m-%d") chart = billboard.ChartData('hot-100', formattedDate) ret["Top100"] = [str(e) for e in chart.entries[:10]] nonTouchedDate = datetime.datetime.strptime(date, '%Y:%m:%d %H:%M:%S') actualFormattedDate = nonTouchedDate.strftime("%Y-%m-%d") #get weather latitude = ret.get("Latitude") longitude = ret.get("Longitude") if latitude is not None and longitude is not None: locObj = urlopen(Request("http://www.geoplugin.net/extras/postalcode.gp?lat=" + str(latitude) + "&long=" + str(longitude) + "&format=json")).read() jsonObj = json.loads(locObj) if type(jsonObj) is dict: # print jsonObj postalCode = jsonObj.get("geoplugin_postCode") countryCode = jsonObj.get("geoplugin_countryCode") # print postalCode # print countryCode #print formattedDate try: weatherObj = urlopen(Request("https://api.weathersource.com/v1/4d6060d10090464668ef/postal_codes/" + postalCode + "," + countryCode + "/forecast.json?period=day&" + "timestamp=" + actualFormattedDate + "&fields=tempAvg,precip,snowfall," + "windSpdAvg,cldCvrAvg,dewPtAvg,feelsLikeAvg,relHumAvg,sfcPresAvg"))\ .read() jsonWeatherObj = json.loads(weatherObj) if len(jsonWeatherObj) > 0: ret["Weather"] = jsonWeatherObj[0] except: pass #get s&p500 data try: sandpData = urlopen(Request("https://www.quandl.com/api/v3/datasets/YAHOO/INDEX_GSPC.json?start_date=" + actualFormattedDate + "&end_date=" + (nonTouchedDate + datetime.timedelta(days=3)).strftime("%Y-%m-%d"))).read() sandpJson = json.loads(sandpData)["dataset"] if 'data' in sandpJson and len(sandpJson["data"]) > 0: ret["sandp500Open"] = sandpJson["data"][0][1] #1 is open ret["sandp500Close"] = sandpJson["data"][0][4] #4 is close except: print 'too many requests. no data pulled' newDoc.model = ret.get("Model") newDoc.make = ret.get("Make") newDoc.orientation = ret.get("Orientation") newDoc.date = datetime.datetime.strptime(ret.get("DateTime"), '%Y:%m:%d %H:%M:%S') if ret.get("DateTime") is not \ None else datetime.datetime.today() newDoc.width = ret.get("ExifImageWidth") newDoc.height = ret.get("ExifImageHeight") newDoc.longitude = ret.get("Longitude") newDoc.latitude = ret.get("Latitude") newDoc.top100 = json.dumps(ret.get("Top100")) newDoc.weather = json.dumps(ret.get("Weather")) newDoc.sandp500Open = ret.get("sandp500Open") newDoc.sandp500Close = ret.get("sandp500Close") newDoc.save(update_fields=["model", "make", "orientation", "date", "width", "height", "longitude", "latitude", "top100", "weather", "sandp500Open", "sandp500Close"]) return newDoc.id