def get_by_url(self, url): ''' Return existed User, Group, Application by url or new intance with empty pk ''' m = re.findall( r'^(?:https?://)?(?:www.)?(?:ok.ru|odnoklassniki.ru)/(.+)/?$', url) if not len(m): raise ValueError("Wrong domain: %s" % url) slug = m[0] try: assert self.model.slug_prefix and slug.startswith( self.model.slug_prefix) id = int( re.findall(r'^%s(\d+)$' % self.model.slug_prefix, slug)[0]) except (AssertionError, ValueError, IndexError): try: response = api_call('url.getInfo', url=url) assert self.model.resolve_screen_name_type == response['type'] id = int(response['objectId']) except OdnoklassnikiError, e: log.error( "Method get_by_slug returned error instead of response. URL='%s'. Error: %s" % (url, e)) return None except (KeyError, TypeError, ValueError), e: log.error( "Method get_by_slug returned response in strange format: %s. URL='%s'" % (response, url)) return None
def api_call(self, method='get', **kwargs): if self.model.methods_access_tag: kwargs['methods_access_tag'] = self.model.methods_access_tag method = self.methods[method] if self.model.methods_namespace: method = self.model.methods_namespace + '.' + method return api_call(method, **kwargs)
def get_by_url(self, url): ''' Return existed User, Group, Application by url or new intance with empty pk ''' m = re.findall(r'^(?:https?://)?(?:www.)?(?:ok.ru|odnoklassniki.ru)/(.+)/?$', url) if not len(m): raise ValueError("Wrong domain: %s" % url) slug = m[0] try: assert self.model.slug_prefix and slug.startswith(self.model.slug_prefix) id = int(re.findall(r'^%s(\d+)$' % self.model.slug_prefix, slug)[0]) except (AssertionError, ValueError, IndexError): try: response = api_call('url.getInfo', url=url) assert self.model.resolve_screen_name_type == response['type'] id = int(response['objectId']) except OdnoklassnikiError, e: log.error("Method get_by_slug returned error instead of response. URL='%s'. Error: %s" % (url, e)) return None except (KeyError, TypeError, ValueError), e: log.error("Method get_by_slug returned response in strange format: %s. URL='%s'" % (response, url)) return None