def get(self, path, data=None, expand=()): # Resolves the search result response problem headers = self.headers if 'content-type' in headers: headers.pop('content-type') # Get the function path path = format_path(path, self.eventbrite_api_url) if data is None: data = {} # Manage expansions if data.get('expand'): # Do nothing because expand is already passed in pass elif expand: # Manage expansions data['expand'] = ','.join(expand) else: # Anything else is None data['expand'] = 'none' # return requests.get(path, headers=headers, params=data or {}) url = path if data: url = url + "?" + urllib.urlencode(data) resp = urlfetch.Fetch(url, headers=headers, method="GET", deadline=60) return RequestsShim(resp, url)
def newImage(name): #steal an image from imgur/random nbytes = 10000000 goodAspect = False maxSize = 800000 while nbytes > maxSize or nbytes < 1000 or not goodAspect: #url = "http://i.imgur.com/1Qz3K.jpg" randurl = "http://imgur.com/random" try: response = urllib.urlopen(randurl) msg = response.read() startPos = msg.find('<img src=\"') + 10 endPos = msg.find('\" alt=', startPos) url = msg[startPos:endPos] timage = urlfetch.Fetch(url).content nbytes = sys.getsizeof(timage) goodAspect = sizeCheck(timage) except: nbytes = 10000000 rimage = ImageM(key_name=name) rimage.url = url timage = smartResize(timage, 400, 400) #resize and re-encode as jpg rimage.name = name rimage.picture = db.Blob(timage) rimage.drawn = False rimage.played = False rimage.put() rimage.mput() return url
def send_message(self, recipient_id, text): headers = { 'Content-Type': 'application/json', } data = json.dumps({ 'recipient': { 'id': recipient_id, }, 'message': { 'text': text } }) result = urlfetch.Fetch( url=URL_FB.format(access_token=PAGE_ACCESS_TOKEN), payload=data, method=urlfetch.POST, headers=headers) if result.status_code != 200: logging.info(data) logging.error(result.content) else: pass
def get(self): url = "http://www.czone2.tcg.gov.tw/GMaps/desc.cfm?sn=09912-704897" result = urlfetch.Fetch(url) if result.status_code == 200: self.response.out.write(result.content) else: self.response.out.write(result.content)
def getCurrentBalance(self): # Bank Account response = urlfetch.Fetch(self.bank_account_url, payload=str(self.bank_account_hardcodedData), method=urlfetch.POST, headers={ 'vary': 'Accept-Encoding', "Content-Type": "application/json", "Accept": "application/json" }, allow_truncated=False, follow_redirects=True, deadline=None, validate_certificate=None) if response.status_code == 200: content = ast.literal_eval(response.content) current_balance = content \ .get('DetailAccountList') \ .get('CardAccount') \ .get('AccountDetail') \ .get('Amounts') \ .get('DDAAvailableBalanceAmount') return current_balance else: return "Something went wrong - Please try again"
def get(self): template = JINJA_ENVIRONMENT.get_template('forecast.html') # Transaction History response = urlfetch.Fetch( self.transaction_history_url, payload=str(self.transaction_history_hardcodedData), method=urlfetch.POST, headers = { 'vary': 'Accept-Encoding', "Content-Type": "application/json", "Accept": "application/json" }, allow_truncated=False, follow_redirects=True, deadline=None, validate_certificate=None ) if response.status_code == 200: # Content content = ast.literal_eval(response.content) \ .get('CCTranHistoryResponseData') \ .get('TransactionDetails') \ .get('TransactionData') ordered_content = self.monthly_processing(content) ordered_content = self.setMonetaryValue(ordered_content) else: self.response.out.write("ERROR") ordered_content['balance'] = self.getCurrentBalance ordered_content['revenue'] = 10000 self.response.out.write(template.render(ordered_content))
def createAccount(self, request): """ Create new custom account """ status = StringMsg() # return status status.data = 'error' # default to error # Verify if user passed reCAPTCHA # POST request to Google reCAPTCHA API url = 'https://www.google.com/recaptcha/api/siteverify?secret=%s&response=%s' % (GRECAPTCHA_SECRET, request.recaptcha) try: result = urlfetch.Fetch(url, method=2) except: raise endpoints.BadRequestException('urlfetch error: Unable to POST to Google reCAPTCHA') return status data = json.loads(result.content) if not data['success']: status.data = 'recaptcha_fail' return status user_id = 'ca_' + request.email # Get profile from datastore -- if profile not found, then profile=None profile_key = ndb.Key(Profile, user_id) profile = profile_key.get() # If profile exists, return status if profile: status.data = 'user_exists' return status # Salt and hash the password salt = Crypto.Random.new().read(16) passkey = KDF.PBKDF2(request.password, salt).encode('hex') salt_passkey = salt.encode('hex') + '|' + passkey # Generate new session ID session_id = Crypto.Random.new().read(16).encode('hex') # Create new profile for user Profile( key = profile_key, userId = user_id, contactEmail = request.email, salt_passkey = salt_passkey, session_id = session_id, loggedIn = True, emailVerified = False, notifications = [False, True] ).put() # Generate user access token token = self._genToken({'userId': user_id, 'session_id': session_id}) # If we get here, means we suceeded status.data = 'success' status.accessToken = token return status
def delete(self, path, data=None): path = format_path(path, self.eventbrite_api_url) # return requests.delete(path, headers=self.headers, data=data or {}) url = path if params: url = url + "?" + urllib.urlencode(data) resp = urlfetch.Fetch(url, headers=headers, method="DELETE", deadline=60) return RequestsShim(resp, url)
def generate_image(cls, url): image_data = urlfetch.Fetch(url).content image = cls.weave(image_data) if not image: return None blob = db.Blob(image) cache = cls(url=url, image=blob) cache.save() return cache
def post(self, path, data=None): path = format_path(path, self.eventbrite_api_url) json_data = json.dumps(data or {}) # return requests.post(path, headers=self.headers, data=json_data) url = path resp = urlfetch.Fetch(url, headers=self.headers, method="POST", payload=json_data, deadline=60) return RequestsShim(resp, url)
def UrlHandler(url, receipt): try: logging.info("fetching " + url) result = urlfetch.Fetch(url, deadline=30) if result.status_code == 200: logging.info("done " + url) TMailHandler(receipt, result.content, "Re: " + url) else: logging.error(url + ":" + result.status) except Exception, e: print(e) return None
def get(self): counselor = Counselor.get_by_key_name(str(self.request.get('key'))) if counselor.avatar: self.response.headers['Content-Type'] = "image/jpg" self.response.out.write(counselor.avatar) else: self.response.headers['Content-Type'] = "image/jpg" self.response.out.write( urlfetch.Fetch( 'https://second-friend.appspot.com/static/img/avatar.jpg'). content)
def get_user_email(token): response = None try: response = urlfetch.Fetch( url='https://www.googleapis.com/oauth2/v1/userinfo?alt=json', method=urlfetch.GET, headers={'Authorization': 'Bearer %s' % token}, deadline=60) except Exception, error_message: logging.exception( 'Failed to access Quasar URL, exception happened - %s' % error_message)
def quote(category): resp = urlfetch.Fetch( 'https://www.brainyquote.com/topics/{}'.format(category)) soup = BeautifulSoup(resp.content, 'html.parser') quotes = [q.text for q in soup.find_all('a', {'title': 'view quote'})] length = len(quotes) if length: quote = quotes[np.random.randint(length - 1)] return jsonify(quote=quote) else: return 'Quote category not found!', 500
def login(self, request): """ Check username/password to login """ status = StringMsg() # return status status.data = 'error' # default to error # Verify if user passed reCAPTCHA # POST request to Google reCAPTCHA API url = 'https://www.google.com/recaptcha/api/siteverify?secret=%s&response=%s' % (GRECAPTCHA_SECRET, request.recaptcha) try: result = urlfetch.Fetch(url, method=2) except: raise endpoints.BadRequestException('urlfetch error: Unable to POST to Google reCAPTCHA') return status data = json.loads(result.content) if not data['success']: status.data = 'recaptcha_fail' return status user_id = 'ca_' + request.email # Get profile from datastore -- if profile not found, then profile=None profile_key = ndb.Key(Profile, user_id) profile = profile_key.get() # If profile does not exist, return False if not profile: return status # Parse salt and passkey from DB, compare it to provided version db_salt, db_passkey = profile.salt_passkey.split('|') passkey = KDF.PBKDF2(request.password, db_salt.decode('hex')).encode('hex') # Passwords don't match, return False if passkey != db_passkey: return status # Generate new session ID session_id = Crypto.Random.new().read(16).encode('hex') profile.session_id = session_id # Update user's status to logged-in profile.loggedIn = True profile.put() # Generate user access token token = self._genToken({'userId': user_id, 'session_id': session_id}) # If we get here, means we suceeded status.data = 'success' status.accessToken = token return status
def _postFbNotif(self, user_id, message, href): """ Post FB notification with message to user """ # Get profile of user_id profile_key = ndb.Key(Profile, user_id) profile = profile_key.get() # Only post FB notif if FB user and user enabled FB notifs if not (user_id[:3] == 'fb_' and profile.notifications[0]): return False fb_user_id = user_id[3:] # Get App Access Token, different than User Token # https://developers.facebook.com/docs/facebook-login/access-tokens/#apptokens url = 'https://graph.facebook.com/v%s/oauth/access_token?grant_type=client_credentials&client_id=%s&client_secret=%s' % (FB_API_VERSION, FB_APP_ID, FB_APP_SECRET) try: result = urlfetch.Fetch(url, method=1) except: raise endpoints.BadRequestException('urlfetch error: FB app access token') return False token = json.loads(result.content)['access_token'] url = 'https://graph.facebook.com/v%s/%s/notifications?access_token=%s&template=%s&href=%s' % (FB_API_VERSION, fb_user_id, token, message, href) try: result = urlfetch.Fetch(url, method=2) except: raise endpoints.BadRequestException('urlfetch error: Unable to POST FB notification') return False data = json.loads(result.content) if 'error' in data: raise endpoints.BadRequestException('FB notification error') return False return True
def get(self,page): page_json = urlfetch.Fetch('http://www.reddit.com/r/'+page+'.json' ) obj = json.loads( page_json.content ) for subs in obj.get('data').get('children'): if not subs['data']['url']: continue path = urlparse.urlparse(subs['data']['url']).path ext = os.path.splitext(path)[1] print ext if not ext or ext == ".gif": continue try: image = urlfetch.Fetch(subs['data']['url']).content img = images.Image(image) img.im_feeling_lucky() png_data = img png_data = img.execute_transforms(images.PNG) temp = subs['data']['title'] temp = temp.replace("\"", "\'") print "put" except Exception,e: print e self.redirect('/')
def insert(self, image_data): """Stable on appspot, not local""" """Hm, unstable everywhere""" image = images.Image(image_data) result = urlfetch.Fetch(self.__watermark_addr) watermark = images.Image(result.content) if watermark.width > image.width: watermark.resize(image.width) watermark.execute_transforms(images.JPEG) watermarked_image = images.composite( [ (image_data, 0, 0, 1.0, images.TOP_LEFT), (watermark._image_data, 0, 0, 0.5, images.BOTTOM_CENTER) ], image.width, image.height, 0, images.JPEG) return watermarked_image
def _getFbUserId(self, token): """ Given token, find FB user ID from FB, and return it """ url = 'https://graph.facebook.com/v%s/me?access_token=%s&fields=id' % (FB_API_VERSION, token) try: result = urlfetch.Fetch(url, method=1) except: raise endpoints.BadRequestException('urlfetch error: Get FB user ID') return None data = json.loads(result.content) if 'error' in data: raise endpoints.BadRequestException('FB OAuth token error') return None user_id = 'fb_' + data['id'] return user_id
def get_firebase_certificates(): """Fetches the current Firebase certificates. Note: in a production application, you should cache this for at least an hour. """ try: result = urlfetch.Fetch(FIREBASE_CERTIFICATES_URL, validate_certificate=True) data = result.content except urlfetch_errors.Error: logging.error('Error while fetching Firebase certificates.') raise certificates = json.loads(data) return certificates
def writeCache(self, request, cache=None, _beforeWriteCache=None): logging.debug('writeCache') url = self.origin + request if self.stripForwardedQueryString is True: url = url.split('?').pop(0) headers = {'User-Agent' : http.userAgent} # Bypass google cache headers['Cache-Control'] = 'no-cache, max-age=0, must-revalidate' if not self.disableIfModifiedSince and cache: headers['If-Modified-Since'] = web.httpdate(cache.lastModified) try: response = urlfetch.Fetch(url=url, headers=headers) except Exception, e: if cache: return cache logging.warning('urlfetch error, redirect to origin. (%s: %s)' % (type(e), e)) raise web.SeeOther(url, absolute=True)
def deviceToken(): device_id = None yandex_token = request.args.get('Yandex-Token') if yandex_token: try: response = urlfetch.Fetch( url='https://quasar.yandex.net/glagol/device_list', method=urlfetch.GET, headers={'Authorization': 'oauth %s' % yandex_token}, deadline=60) except Exception, error_message: logging.exception( 'Failed to access Quasar URL, exception happened - %s' % error_message) return 'Failed to access Quasar URL' if response: if response.status_code == 200: if response and response.content: logging.info(response.content) response_json = json.loads(response.content) if response_json: if response_json.get('status') == 'ok': if response_json.get('devices') and len( response_json.get('devices')) > 0: device_id = None for dd in response_json.get('devices'): if dd.get( 'platform' ) == 'yandexstation' or dd.get( 'platform') == 'yandexmodule': device_id = dd.get('id') break if not device_id: return 'No Yandex Station found' else: return 'No devices' else: return response.content else: return 'Failed to parse JSON' else: return 'No response received from Quasar' else: return 'Error ' + str(response.status_code)
def facebook(path, method="POST", body=None, accessToken=None): version = "v2.6" url = "https://graph.facebook.com/%s%s?access_token=%s" % ( version, path, accessToken, ) resp = urlfetch.Fetch(url, method=method, payload=json.dumps(body), headers={'content-type': 'application/json'}) if resp.status_code == 200: jsonBody = json.loads(resp.content) logging.info(jsonBody) return jsonBody else: logging.warning(resp.content) logging.warning(body)
def forwardRequest(url, method='GET'): headers = {} for key, value in web.ctx.environ.iteritems(): if not key.startswith('HTTP_'): continue key = '-'.join([k.capitalize() for k in key[5:].split('_')]) headers[key] = value headers['User-Agent'] = http.userAgent payload = web.data() or None try: return urlfetch.Fetch(url=url, method=method, headers=headers, payload=payload) except urlfetch_errors.Error: # We got an error, redirect to the origin # to let client dealing errors with it. raise web.SeeOther(url)
def my_blob(self, img): loc = img.find('http:') if loc < 0 or loc > 6: if img[0] != '/': img = '%s/1%s' % (self.url, img) else: #img = '%s%s' % (self.url, img) loc2 = img.find('//') if loc2 < 0: img = '%s%s' % (self.url, img) else: img = 'http://google.com/%s/%s' % ( self.url.split('//')[1].split('/'), img) test = getImg(img) if not test: store = Img(url=img) store.picture = db.Blob(urlfetch.Fetch(img).content) store.put() pass_on = 'image?img=%s' % img return pass_on
def post(self): name = util.escape(self.request.get("user_first") + " " + self.request.get("user_last")) password = util.escape(self.request.get("user_pass")) email = util.escape(self.request.get("user_email")) user = databases.User.register(name, password, email) user.put() data = db.Blob(urlfetch.Fetch("http://i.imgur.com/efHNR.gif").content) filetype = 'gif' name = 'blank_profile.gif' image = databases.userImage(name = name, data = data, filetype = filetype, user = user) image.put() self.set_cookie("user", str(user.key().id())) self.redirect('/')
def get_golflink_details(self): if self.golflink_no: try: glpage = urlfetch.Fetch( "http://www.golflink.com.au/HandicapHistory.aspx?golflink_no=" + self.golflink_no.golflink_no_for_golflink()).content except: glpage = None if glpage != None: soup = BeautifulSoup(glpage) try: self.exact_handicap = soup.find("div", id="exactHandicap").string self.playing_handicap = int( soup.find("div", id="playingHandicap").string) except: self.exact_handicap = "n/a" self.playing_handicap = None #TODO - get status self.handicap_status = ""
def forgotPassword(self, request): """ Forgot password, send user password reset link via email """ status = StringMsg() status.data = 'error' # Verify if user passed reCAPTCHA # POST request to Google reCAPTCHA API url = 'https://www.google.com/recaptcha/api/siteverify?secret=%s&response=%s' % (GRECAPTCHA_SECRET, request.recaptcha) try: result = urlfetch.Fetch(url, method=2) except: raise endpoints.BadRequestException('urlfetch error: Unable to POST to Google reCAPTCHA') return status data = json.loads(result.content) if not data['success']: status.data = 'recaptcha_fail' return status user_id = 'ca_' + request.email # Get profile from datastore -- if profile not found, then profile=None profile_key = ndb.Key(Profile, user_id) profile = profile_key.get() # Check if profile exists. If not, return status if not profile: status.data = 'invalid_email' return status # If email unverified, return status if not profile.emailVerified: status.data = 'unverified_email' return status # Send password reset link to user's email if self._emailPwReset(profile): status.data = 'success' return status
def _postToSparkpost(self, payload): """ Post to Sparkpost API. Return True/False status """ payload_json = json.dumps(payload) headers = { 'Authorization': SPARKPOST_SECRET, 'Content-Type': 'application/json', } url = 'https://api.sparkpost.com/api/v1/transmissions?num_rcpt_errors=3' try: result = urlfetch.Fetch(url, headers=headers, payload=payload_json, method=2) except: raise endpoints.BadRequestException('urlfetch error: Unable to POST to SparkPost') return False data = json.loads(result.content) # Determine status from SparkPost, return True/False if 'errors' in data: return False if data['results']['total_accepted_recipients'] != 1: return False return True
def get(self): from content import recipes for r in recipes: recipe = Recipe(author=helpers.get_current_user()) recipe.name = r['name'] recipe.teaser = r['teaser'] recipe.type = r['type'] recipe.preparation_time = r['preparation_time'] recipe.cooking_time = r['cooking_time'] from google.appengine.api import urlfetch recipe.image = db.Blob(urlfetch.Fetch(r['image']).content) recipe.publish = r['publish'] recipe.ingredients = r['ingredients'] recipe.method = r['method'] for f in r['foodtypes']: if FoodType.all().filter('name', f).count() == 1: ft = FoodType.all().filter('name', f)[0] else: ft = FoodType(name=f) ft.put() recipe.foodtypes_list.append(ft.key()) recipe.put() self.response.out.write("OK")