Esempio n. 1
0
 def post_object(self, path, data=None, file_data=None):
     if not 'access_token' in data and self.access_token:
         data['access_token'] = self.access_token
     url = "https://graph.facebook.com/" + self.api_version + "/%s" % str(path)
     if not data:
         data = {}
     try:
         if file_data:
             form = MultiPartForm()
             for key, val in iteritems(data):
                 form.add_field(key, val)
             for key, val in file_data.iteritmes():
                 form.add_file(key, val['name'], val['source'], mimetype=val.get('mimetype'))
             request = Request(url)
             body = str(form)
             request.add_header('Content-type', form.get_content_type())
             request.add_header('Content-length', len(body))
             request.data =body
             ret = urlopen(request).read()
         else:
             ret = urlopen(url, urlencode(data)).read()
         return json.loads(ret)
     except HTTPError as e:
         self._error_handle(e)
         return {}
Esempio n. 2
0
 def get_object(self, path, **kwargs):
     if not 'access_token' in kwargs and self.access_token:
         kwargs['access_token'] = self.access_token
     url = "https://graph.facebook.com/" + self.api_version + "/%s?%s" % (str(path), urlencode(kwargs))
     try:
         ret = urlopen(url).read()
         return json.loads(ret)
     except HTTPError as e:
         self._error_handle(e)
Esempio n. 3
0
    def get_access_token(self, redirect_uri, code):
        url = "oauth/access_token?%s" % (urlencode({
            'client_id': self.app_id, 'redirect_uri': redirect_uri, 'client_secret': self.app_secret, 'code': code}))
        parsed = urlparse.parse_qs(urlopen(url).read())
        access_token = parsed['access_token']
        if type(access_token) is list or type(access_token) is tuple:
            access_token = access_token[0]

        self.access_token = access_token
        return access_token
Esempio n. 4
0
    def get_access_token_from_short_token(self, short):
        url = "https://graph.facebook.com/oauth/access_token?%s" % \
              (urlencode({'client_id': self.app_id, 'grant_type': 'fb_exchange_token',
                                 'client_secret': self.app_secret, 'fb_exchange_token': short}))
        parsed = urlparse.parse_qs(urlopen(url).read())
        access_token = parsed['access_token']
        if type(access_token) is list or type(access_token) is tuple:
            access_token = access_token[0]

        self.access_token = access_token
        return access_token
Esempio n. 5
0
    def get_access_token_and_expire_timestamp(self, request, redirect_uri):
        if not request.session.has_key('facebook_state'):
            return None, None
        elif request.session['facebook_state'] != request.GET.get('state'):
            return None, None

        url = "https://graph.facebook.com/oauth/access_token?%s" % (urlencode({
            'client_id': self.app_id, 'redirect_uri': redirect_uri, 'client_secret': self.app_secret,
            'code': request.GET.get('code')}))
        parsed = urlparse.parse_qs(urlopen(url).read())
        access_token = parsed['access_token']
        expires = parsed['expires']
        if type(access_token) is list or type(access_token) is tuple:
            access_token = access_token[0]
            expires = expires[0]
        expires = int(expires) + int(time.time())
        self.access_token = access_token
        return access_token, expires
Esempio n. 6
0
 def pre_create(self, data, files, acceptable, required, exclude, request, request_kwargs, or_get):
     if 'photo' in files:
         photo = EmbeddedPhoto()
         photo.source.save(generate_random_from_vschar_set(50) + '.jpg', File(files['photo']), False)
         photo.height = photo.source.height
         photo.width = photo.source.width
         data['photo'] = photo
         del(files['photo'])
     elif 'photo_url' in data:
         file = StringIO(urlopen(data['photo_url']).read())
         if file:
             photo = EmbeddedPhoto()
             photo.source.save(generate_random_from_vschar_set(50) + '.jpg', File(file), False)
             photo.height = photo.source.height
             photo.width = photo.source.width
             data['photo'] = photo
             del(data['photo_url'])
         else:
             raise RestBadRequest()
     return data, files
Esempio n. 7
0
    def get_google_reverse_code(lat, lng):
        GOOGLE_REVERSE_GEOCODE = "http://maps.googleapis.com/maps/api/geocode/json?latlng=%s&sensor=false&language=ko"
        GOOGLE_REVERSE_GEOCODE_EN = "http://maps.googleapis.com/maps/api/geocode/json?latlng=%s&sensor=false&language=en"
        geocode_query = GOOGLE_REVERSE_GEOCODE % (str(lat) + ',' + str(lng))
        geocode_query_en = GOOGLE_REVERSE_GEOCODE_EN % (str(lat) + ',' + str(lng))
        # print geocode_query
        ret = urlopen(geocode_query).read()
        geocode_data = {}


        for geocode_data_ko in json.loads(ret.decode('utf8'))['results']:
            new_lat, new_lng = geocode_data_ko['geometry']['location']['lat'], geocode_data_ko['geometry']['location']['lng']
            cur_key = str(new_lat) + ',' + str(new_lng)

            current_geocode_data = geocode_data.get(cur_key, {'components': [],
                                                              'lat': new_lat, 'lng': new_lng})

            # current_geocode_data['locality']['original'] = original_address_component[0]
            # for sub in original_address_component[1:-1]:
            # current_geocode_data['sub_localities'].append({'original': sub})
            current_geocode_data['formatted_address'] = geocode_data_ko['formatted_address']
            geocode_data_ko['address_components'].reverse()
            sub_locality_idx = 0
            for each_data in geocode_data_ko['address_components']:
                com_types = each_data['types']
                if 'post_box' in com_types or 'floor' in com_types or 'room' in com_types or 'street_number' in com_types:
                    continue
                elif 'postal_code' in com_types:
                    current_geocode_data['postal_code'] = each_data['long_name']
                else:
                    try:
                        current_geocode_data['components'][sub_locality_idx]
                    except IndexError:
                        current_geocode_data['components'].append({})

                    current_geocode_data['components'][sub_locality_idx]['ko'] = each_data['long_name']
                    current_geocode_data['components'][sub_locality_idx]['types'] = each_data['types']
                    current_geocode_data['components'][sub_locality_idx]['depth'] = sub_locality_idx
                    sub_locality_idx += 1
            current_geocode_data['bounds'] = geocode_data_ko['geometry'].get('bounds')
            current_geocode_data['level'] = sub_locality_idx - 1
            geocode_data[cur_key] = current_geocode_data


        ret = urlopen(geocode_query_en).read()
        for geocode_data_en in json.loads(ret.decode('utf8'))['results']:
            new_lat, new_lng = geocode_data_en['geometry']['location']['lat'], geocode_data_en['geometry']['location']['lng']
            cur_key = str(new_lat) + ',' + str(new_lng)


            current_geocode_data = geocode_data.get(cur_key, {'components': [], 'lat': new_lat, 'lng': new_lng})

            current_geocode_data['formatted_address_ascii'] = geocode_data_en['formatted_address']
            geocode_data_en['address_components'].reverse()
            sub_locality_idx = 0
            for each_data in geocode_data_en['address_components']:
                com_types = each_data['types']
                if 'post_box' in com_types or 'floor' in com_types or 'room' in com_types or 'street_number' in com_types:
                    continue
                elif 'postal_code' in com_types:
                    current_geocode_data['postal_code'] = each_data['long_name']
                else:
                    try:
                        current_geocode_data['components'][sub_locality_idx]
                    except IndexError:
                        current_geocode_data['components'].append({})

                    current_geocode_data['components'][sub_locality_idx]['ascii'] = each_data['long_name']
                    current_geocode_data['components'][sub_locality_idx]['types'] = each_data['types']
                    current_geocode_data['components'][sub_locality_idx]['depth'] = sub_locality_idx
                    sub_locality_idx += 1
            current_geocode_data['from_command'] = True
            current_geocode_data['bounds'] = geocode_data_en['geometry'].get('bounds')
            current_geocode_data['level'] = sub_locality_idx - 1
            geocode_data[cur_key] = current_geocode_data
        # print(geocode_data)
        for each in geocode_data:
            current_geocode_data = geocode_data[each]
            region = RegionResource().create(data=current_geocode_data)
            address = Address(region=region, postal_code=current_geocode_data.get('postal_code', None))
            yield address, current_geocode_data['formatted_address'], current_geocode_data['lat'], current_geocode_data['lng'], current_geocode_data['level'], current_geocode_data['bounds']