Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #24 from michaelhelmick/pep8_cleanup
Browse files Browse the repository at this point in the history
Pep8 cleanup
  • Loading branch information
Mike Krieger committed Mar 2, 2012
2 parents ba62e57 + 911e4c9 commit 2a73f90
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 148 deletions.
7 changes: 5 additions & 2 deletions instagram/bind.py
Expand Up @@ -4,17 +4,20 @@
import simplejson
re_path_template = re.compile('{\w+}')


def encode_string(value):
return value.encode('utf-8') \
if isinstance(value, unicode) else str(value)


class InstagramClientError(Exception):
def __init__(self, error_message):
self.error_message = error_message

def __str__(self):
return self.error_message


class InstagramAPIError(Exception):

def __init__(self, status_code, error_type, error_message, *args, **kwargs):
Expand All @@ -25,6 +28,7 @@ def __init__(self, status_code, error_type, error_message, *args, **kwargs):
def __str__(self):
return "(%s) %s-%s" % (self.status_code, self.error_type, self.error_message)


def bind_method(**config):

class InstagramAPIMethod(object):
Expand Down Expand Up @@ -126,7 +130,7 @@ def execute(self):
url, method, body, headers = OAuth2Request(self.api).prepare_request(self.method,
self.path,
self.parameters,
include_secret = self.include_secret)
include_secret=self.include_secret)
if self.as_generator:
return self._paginator_with_url(url, method, body, headers)
else:
Expand All @@ -136,7 +140,6 @@ def execute(self):
else:
return content


def _call(api, *args, **kwargs):
method = InstagramAPIMethod(api, *args, **kwargs)
return method.execute()
Expand Down
230 changes: 114 additions & 116 deletions instagram/client.py
Expand Up @@ -7,8 +7,9 @@

SUPPORTED_FORMATS = ['json']


class InstagramAPI(oauth2.OAuth2API):

host = "api.instagram.com"
base_path = "/v1"
access_token_field = "access_token"
Expand All @@ -25,154 +26,153 @@ def __init__(self, *args, **kwargs):
raise Exception("Unsupported format")
super(InstagramAPI, self).__init__(*args, **kwargs)


media_popular = bind_method(
path = "/media/popular",
accepts_parameters = MEDIA_ACCEPT_PARAMETERS,
root_class = Media)
path="/media/popular",
accepts_parameters=MEDIA_ACCEPT_PARAMETERS,
root_class=Media)

media_search = bind_method(
path = "/media/search",
accepts_parameters = SEARCH_ACCEPT_PARAMETERS + ['lat', 'lng', 'min_timestamp', 'max_timestamp'],
root_class = Media)
path="/media/search",
accepts_parameters=SEARCH_ACCEPT_PARAMETERS + ['lat', 'lng', 'min_timestamp', 'max_timestamp'],
root_class=Media)

media_likes = bind_method(
path = "/media/{media_id}/likes",
accepts_parameters = ['media_id'],
root_class = User)
path="/media/{media_id}/likes",
accepts_parameters=['media_id'],
root_class=User)

like_media = bind_method(
path = "/media/{media_id}/likes",
method = "POST",
accepts_parameters = ['media_id'],
response_type = "empty")
path="/media/{media_id}/likes",
method="POST",
accepts_parameters=['media_id'],
response_type="empty")

unlike_media = bind_method(
path = "/media/{media_id}/likes",
method = "DELETE",
accepts_parameters = ['media_id'],
response_type = "empty")
path="/media/{media_id}/likes",
method="DELETE",
accepts_parameters=['media_id'],
response_type="empty")

create_media_comment = bind_method(
path = "/media/{media_id}/comments",
method = "POST",
accepts_parameters = ['media_id', 'text'],
response_type = "empty",
root_class = Comment)
path="/media/{media_id}/comments",
method="POST",
accepts_parameters=['media_id', 'text'],
response_type="empty",
root_class=Comment)

delete_comment = bind_method(
path = "/media/{media_id}/comments/{comment_id}",
method = "DELETE",
accepts_parameters = ['media_id', 'comment_id'],
response_type = "empty")
path="/media/{media_id}/comments/{comment_id}",
method="DELETE",
accepts_parameters=['media_id', 'comment_id'],
response_type="empty")

media_comments = bind_method(
path = "/media/{media_id}/comments",
method = "GET",
accepts_parameters = ['media_id'],
response_type = "list",
root_class = Comment)
path="/media/{media_id}/comments",
method="GET",
accepts_parameters=['media_id'],
response_type="list",
root_class=Comment)

media = bind_method(
path = "/media/{media_id}",
accepts_parameters = ['media_id'],
response_type = "entry",
root_class = Media)
path="/media/{media_id}",
accepts_parameters=['media_id'],
response_type="entry",
root_class=Media)

user_media_feed = bind_method(
path = "/users/self/feed",
accepts_parameters = MEDIA_ACCEPT_PARAMETERS,
root_class = Media,
paginates = True)
path="/users/self/feed",
accepts_parameters=MEDIA_ACCEPT_PARAMETERS,
root_class=Media,
paginates=True)

user_liked_media = bind_method(
path = "/users/self/media/liked",
accepts_parameters = MEDIA_ACCEPT_PARAMETERS,
root_class = Media,
paginates = True)
path="/users/self/media/liked",
accepts_parameters=MEDIA_ACCEPT_PARAMETERS,
root_class=Media,
paginates=True)

user_recent_media = bind_method(
path = "/users/{user_id}/media/recent",
accepts_parameters = MEDIA_ACCEPT_PARAMETERS + ['user_id'],
root_class = Media,
paginates = True)
path="/users/{user_id}/media/recent",
accepts_parameters=MEDIA_ACCEPT_PARAMETERS + ['user_id'],
root_class=Media,
paginates=True)

user_search = bind_method(
path = "/users/search",
accepts_parameters = SEARCH_ACCEPT_PARAMETERS,
root_class = User)
path="/users/search",
accepts_parameters=SEARCH_ACCEPT_PARAMETERS,
root_class=User)

user_follows = bind_method(
path = "/users/{user_id}/follows",
accepts_parameters = ["user_id"],
paginates = True,
root_class = User)
path="/users/{user_id}/follows",
accepts_parameters=["user_id"],
paginates=True,
root_class=User)

user_followed_by = bind_method(
path = "/users/{user_id}/followed-by",
accepts_parameters = ["user_id"],
paginates = True,
root_class = User)
path="/users/{user_id}/followed-by",
accepts_parameters=["user_id"],
paginates=True,
root_class=User)

user = bind_method(
path = "/users/{user_id}",
accepts_parameters = ["user_id"],
root_class = User,
response_type = "entry")
path="/users/{user_id}",
accepts_parameters=["user_id"],
root_class=User,
response_type="entry")

location_recent_media = bind_method(
path = "/locations/{location_id}/media/recent",
accepts_parameters = MEDIA_ACCEPT_PARAMETERS + ['location_id'],
root_class = Media,
paginates = True)
path="/locations/{location_id}/media/recent",
accepts_parameters=MEDIA_ACCEPT_PARAMETERS + ['location_id'],
root_class=Media,
paginates=True)

location_search = bind_method(
path = "/locations/search",
accepts_parameters = SEARCH_ACCEPT_PARAMETERS + ['lat', 'lng', 'foursquare_id'],
root_class = Location)
path="/locations/search",
accepts_parameters=SEARCH_ACCEPT_PARAMETERS + ['lat', 'lng', 'foursquare_id'],
root_class=Location)

location = bind_method(
path = "/locations/{location_id}",
accepts_parameters = ["location_id"],
root_class = Location,
response_type = "entry")
path="/locations/{location_id}",
accepts_parameters=["location_id"],
root_class=Location,
response_type="entry")

geography_recent_media = bind_method(
path = "/geographies/{geography_id}/media/recent",
accepts_parameters = MEDIA_ACCEPT_PARAMETERS + ["geography_id"],
root_class = Media,
paginates = True)
path="/geographies/{geography_id}/media/recent",
accepts_parameters=MEDIA_ACCEPT_PARAMETERS + ["geography_id"],
root_class=Media,
paginates=True)

tag_recent_media = bind_method(
path = "/tags/{tag_name}/media/recent",
accepts_parameters = MEDIA_ACCEPT_PARAMETERS + ['tag_name'],
root_class = Media,
paginates = True)
path="/tags/{tag_name}/media/recent",
accepts_parameters=MEDIA_ACCEPT_PARAMETERS + ['tag_name'],
root_class=Media,
paginates=True)

tag_search = bind_method(
path = "/tags/search",
accepts_parameters = SEARCH_ACCEPT_PARAMETERS,
root_class = Tag,
paginates = True)
path="/tags/search",
accepts_parameters=SEARCH_ACCEPT_PARAMETERS,
root_class=Tag,
paginates=True)

tag = bind_method(
path = "/tags/{tag_name}",
accepts_parameters = ["tag_name"],
root_class = Tag,
response_type = "entry")
path="/tags/{tag_name}",
accepts_parameters=["tag_name"],
root_class=Tag,
response_type="entry")

user_incoming_requests = bind_method(
path = "/users/self/requested-by",
root_class = User)
path="/users/self/requested-by",
root_class=User)

change_user_relationship = bind_method(
method = "POST",
path = "/users/{user_id}/relationship",
root_class = Relationship,
accepts_parameters = ["user_id", "action"],
paginates = True,
requires_target_user = True,
response_type = "entry")
method="POST",
path="/users/{user_id}/relationship",
root_class=Relationship,
accepts_parameters=["user_id", "action"],
paginates=True,
requires_target_user=True,
response_type="entry")

def _make_relationship_shortcut(action):
def _inner(self, *args, **kwargs):
Expand All @@ -187,28 +187,26 @@ def _inner(self, *args, **kwargs):
approve_user_request = _make_relationship_shortcut('approve')
ignore_user_request = _make_relationship_shortcut('ignore')



def _make_subscription_action(method, include=None, exclude=None):
accepts_parameters = ["object",
"aspect",
"object_id", # Optional if subscribing to all users
"callback_url",
"lat", # Geography
"lng", # Geography
"radius", # Geography
accepts_parameters = ["object",
"aspect",
"object_id", # Optional if subscribing to all users
"callback_url",
"lat", # Geography
"lng", # Geography
"radius", # Geography
"verify_token"]

if include:
accepts_parameters.extend(include)
if exclude:
accepts_parameters = [x for x in accepts_parameters if x not in exclude]
return bind_method(
path = "/subscriptions",
method = method,
accepts_parameters = accepts_parameters,
include_secret = True,
objectify_response = False
path="/subscriptions",
method=method,
accepts_parameters=accepts_parameters,
include_secret=True,
objectify_response=False
)

create_subscription = _make_subscription_action('POST')
Expand Down

0 comments on commit 2a73f90

Please sign in to comment.