Skip to content

Commit

Permalink
Merge pull request #7 from niuware/development
Browse files Browse the repository at this point in the history
Add 'add/remove' methods for the 'Comments' API endpoint
  • Loading branch information
niuware authored Nov 15, 2019
2 parents 9b57ae4 + 404b0ae commit 3834428
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 21 deletions.
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ Endponint for accessing comments and threaded comments of any public or friends
|of_comment(comment: `TComment`)|Returns a generator of `TComment` instances with all threaded comments of a comment.|
|like(comment: `TComment`)|Likes a comment.|
|unlike(comment: `TComment`)|Unlikes a comment.|
|add(timeline_post: `TTimelinePost`, text: `string`, parent_comment: `TComment` = None) -> `TComment` \| `None`|Adds a new comment to a post. You can reply to a comment if `parent_comment` argument is provided. An instance of the created comment is return if succeeded otherwise `None`.|
|remove(timeline_post: `TTimelinePost`, comment: `TComment`)|Removes a comment from a post. Only comments authored by the current logged in account can be removed.|

### StoryReel

Expand Down Expand Up @@ -166,20 +168,22 @@ Endpoint for accessing the story details of a story reel item. This endpoint is
### TTimelinePost
|Field|Type|Details|
|---|---|---|
|id|`string`|The Instagram Id of the user|
|owner|`string`|The owner account Instagram Id|
|timestamp|`integer`|The timestamp of the post|
|is_video|`bool`|A flag to know if the story is a video|
|id|`string`|The Instagram Id of the post|
|shortcode|`string`|The Instagram shortcode Id of the post|
|owner|`string`|The post author's Instagram Id|
|timestamp|`integer`|The created timestamp of the post|
|caption|`string`|The caption of the post|
|is_video|`bool`|A flag to know if the post is a video|
|like_count|`integer`|The like count of the post|
|comment_count|`integer`|The comment count of the post|
|display_resources|`list`|A list of image URLs associated with the post|
|display_resources|`list`|A list of image URL strings associated with the post|
|video_url|`string`|The video URL (if available) associated with the post|

### TComment
|Field|Type|Details|
|---|---|---|
|id|`string`|The Instagram Id of the comment|
|text|`string`|The comment text|
|text|`string`|The text of the comment|
|username|`string`|The author's username|
|timestamp|`integer`|The timestamp of the comment|
|viewer_has_liked|`bool`|A flag to know if the viewer liked the comment|
Expand All @@ -190,14 +194,14 @@ Endpoint for accessing the story details of a story reel item. This endpoint is
|Field|Type|Details|
|---|---|---|
|id|`string`|The Instagram Id of the story|
|owner|`string`|The owner account Instagram Id|
|timestamp|`integer`|The timestamp of the story|
|owner|`string`|The story author's Instagram Id|
|timestamp|`integer`|The created timestamp of the story|
|expire_at|`integer`|The expiration timestamp of the story|
|audience|`string`|The type of audience of the story. If public the value is `MediaAudience.DEFAULT`, if private the value is `MediaAudience.BESTIES`|
|is_video|`bool`|A flag to know if the story is a video|
|view_count|`integer`|The view count of the story. The count is only available for stories posted by the currently logged in user. Other accounts will have a count equal to `0`.|
|display_resources|`list`|A list of image URLs associated with the story|
|video_resources|`list`|A list of video URLs associated with the story|
|display_resources|`list`|A list of image URL strings associated with the story|
|video_resources|`list`|A list of video URL strings associated with the story|

### TStoryViewer
|Field|Type|Details|
Expand Down
10 changes: 10 additions & 0 deletions instpector/apis/instagram/base_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ def post(self, url_path, data=None, **options):
raise ParseDataException
return None

def quick_post(self, url_path, data=None, **options):
response = self.post(url_path, data=data, use_auth=True,
headers={
"Content-Type": "application/x-www-form-urlencoded"
},
**options)
if response and response.get("status") == "ok":
return True
return False

def _download_resources_list(self, item, name, extension, low_quality):
try:
resources = getattr(item, name)
Expand Down
39 changes: 39 additions & 0 deletions instpector/apis/instagram/comments.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .like_graph_ql import LikeGraphQL
from .parser import Parser
from .definitions import TComment


class Comments(LikeGraphQL):
Expand Down Expand Up @@ -34,3 +35,41 @@ def of_comment(self, comment):
page_info_parser="edge_threaded_comments",
page_info_parser_path="comment",
data_parser=Parser.threaded_comments)

def add(self, timeline_post, text, parent_comment=None):
post_id = getattr(timeline_post, "id", None)
if post_id is None:
return False
comment_id = getattr(parent_comment, "id") if parent_comment else ""
data = {
"comment_text": text,
"replied_to_comment_id": comment_id,
}
response = self.post("/web/comments/{id}/add/".format(id=post_id),
data=data, use_auth=True,
headers={
"Content-Type": "application/x-www-form-urlencoded"
})
if response and response.get("status") == "ok":
owner = response.get("from") or {}
thread_count = 0 if comment_id == "" else None
return TComment(
id=response.get("id", ""),
text=response.get("text", ""),
timestamp=response.get("created_time"),
username=owner.get("username", ""),
viewer_has_liked=False,
liked_count=0,
thread_count=thread_count
)
return None

def remove(self, timeline_post, comment):
post_id = getattr(timeline_post, "id", None)
if post_id is None:
return False
comment_id = getattr(comment, "id", None)
if comment_id is None:
return False
url = "/web/comments/{id}/delete/{comment_id}/"
return self.quick_post(url.format(id=post_id, comment_id=comment_id))
3 changes: 2 additions & 1 deletion instpector/apis/instagram/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
))

TTimelinePost = namedtuple("TTimelinePost", (
"id owner timestamp is_video like_count comment_count display_resources video_url shortcode"
"id owner timestamp is_video like_count comment_count display_resources "
"video_url shortcode caption"
))

TStoryReelItem = namedtuple("TStoryReelItem", (
Expand Down
9 changes: 1 addition & 8 deletions instpector/apis/instagram/like_graph_ql.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@ def _toggle_like(self, obj, action):
obj_id = getattr(obj, "id", None)
if obj_id is None:
return False
response = self.post(self._url.format(action=endpoint, id=obj_id),
use_auth=True,
headers={
"Content-Type": "application/x-www-form-urlencoded"
})
if response and response.get("status") == "ok":
return True
return False
return self.quick_post(self._url.format(action=endpoint, id=obj_id))

def unlike(self, obj):
return self._toggle_like(obj, 'unlike')
Expand Down
7 changes: 6 additions & 1 deletion instpector/apis/instagram/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def timeline(data):
comments = node.get("edge_media_to_comment") or {}
display_resources = node.get("display_resources") or {}
owner = node.get("owner") or {}
edge_caption = node.get("edge_media_to_caption") or {}
caption_edges = edge_caption.get("edges") or []
node_caption = caption_edges[0] if caption_edges else {}
caption = node_caption.get("node") or {}
post = TTimelinePost(
id=node.get("id", ""),
owner=owner.get("id", ""),
Expand All @@ -69,7 +73,8 @@ def timeline(data):
comment_count=comments.get("count", 0),
display_resources=list(map(lambda res: res.get("src"), display_resources)),
video_url=node.get("video_url"),
shortcode=node.get("shortcode")
shortcode=node.get("shortcode"),
caption=caption.get("text", "")
)
yield post

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="instpector",
version="0.2.4",
version="0.2.5",
description="A simple Instagram's web API library",
author="Erik Lopez",
long_description=README,
Expand Down

0 comments on commit 3834428

Please sign in to comment.