From a38b48c5a238687134dbda80f12ed484747311d2 Mon Sep 17 00:00:00 2001 From: Borislav Stoilov Date: Sat, 20 Jun 2020 13:15:35 +0300 Subject: [PATCH] Release 1.1.0 (#63) * update download images example * minor * Improved docs, added logout and some utility methods --- README.md | 27 +++- examples.py | 31 +++-- py3pin/Pinterest.py | 295 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 336 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 6999d99..47c3cc3 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ # py3-pinterest [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) -Unofficial Pinterest API implemented in python 3 that can do almost all pinterest tasks like comment, pin, repin, follow, unfollow and more. +Unofficial Pinterest API implemented in python 3 that can do all Pinterest tasks like comment, pin, repin, follow, unfollow and more. + It is implemented by directly calling the pinterest servers, mimicking an actual browser, so you don't need pinterest API key. If you see any issues, or find bugs feel free to report them here on the github repo. @@ -18,16 +19,26 @@ If you see any issues, or find bugs feel free to report them here on the github ```pinterest = Pinterest(email='your email goes here', password='password goes here', username='look in pinterest url', cred_root='cred root dir')``` cred_root is the dir (automatically created if missing) that will store some cookies nad sessions, so you don't need to login before each request. -Make sure you specify a path with read/write persmissions. +Make sure you specify a path with read/write permissions. + +Proxies example: +``` +proxies = {"http":"http://username:password@proxy_ip:proxy_port"} +Pinterest(email='emai', password='pass', username='name', cred_root='cred_root', proxies=proxies) +``` # The following features are currently supported +## Login/Logout +Login will store auth cookies for later use. These cookies are usually valid for ~15 days, then you will start getting 403 and 401 errors, which means you need to call login again. ## Login Login is required to permit actions to the Pinterest servers. Login will store auth cookies for later use. These cookies are usually valid for ~15 days, then you will start getting 403 and 401 errors, which means you need to call login again. ```pinterest.login()``` +```pinterest.logout()``` + ## Load profile @@ -100,8 +111,14 @@ A pinterest feature they use to pin from websites ### Pin +Pin image by web url: + ```pinterest.pin(board_id=board_id, image_url=image_url, description=description, title=title)``` +Pin image from local file: + +```pinterest.upload_pin(board_id=board_id, section_id=section_id, image_file=image_path, description=description, title=title, link=link)``` + ### Get home feed pins ``` home_feed_batch = pinterest.home_feed()``` @@ -110,11 +127,11 @@ A pinterest feature they use to pin from websites ```rec_batch = pinterest.board_recommendations(board_id=board_id)``` -### Get pin by id +### Get pin information by id ```pinterest.load_pin(pin_id='pin_id')``` -### Section support +### Board Section support ```pinterest.create_board_section(board_id=board_id, section_name=section_name)``` ```pinterest.delete_board_section(section_id=section_id)``` ```pinterest.get_board_sections(board_id=board_id)``` @@ -161,7 +178,7 @@ If username is not provided current user will be used ```search_batch = pinterest.search(scope='boards', query='food')``` -Current pinterest scopes are: pins, buyable_pins, my_pins, videos, users, boards +Current pinterest scopes are: pins, buyable_pins, my_pins, videos, boards ## User interactions diff --git a/examples.py b/examples.py index f857cca..ec052e8 100644 --- a/examples.py +++ b/examples.py @@ -5,16 +5,20 @@ username='username', cred_root='cred_root') +# proxies example: +# proxies = {"http":"http://username:password@proxy_ip:proxy_port"} +# Pinterest(email='emai', password='pass', username='name', cred_root='cred_root', proxies=proxies) -# login is required to perform further actions. Login will obtain and store cookies for further use, they last around 15 days. -pinterest.login() +# login will obtain and store cookies for further use, they last around 15 days. +# NOTE: Since login will store the cookies in local file you don't need to call it more then 3-4 times a month. +# pinterest.login() def get_user_profile(): return pinterest.get_user_overview(username='username') -def get_user_boards(username=None): +def get_user_boards_batched(username=None): boards = [] board_batch = pinterest.boards(username=username) while len(board_batch) > 0: @@ -24,7 +28,11 @@ def get_user_boards(username=None): return boards -def get_board_pins(board_id=''): +def get_boards(username=None): + return pinterest.boards_all(username=username) + + +def get_board_pins_batched(board_id=''): board_feed = [] feed_batch = pinterest.board_feed(board_id=board_id) while len(feed_batch) > 0: @@ -49,7 +57,7 @@ def unfollow(user_id=''): return pinterest.unfollow_user(user_id=user_id) -def get_following(username=None, max_items=500): +def get_following_batched(username=None, max_items=500): # you can get following on any user, default is current user # pinterest.get_following(username='some_user') @@ -62,7 +70,12 @@ def get_following(username=None, max_items=500): return following -def get_followers(username=None, max_items=500): +def get_following(username=None): + # Gets full following list of a user + return pinterest.get_following_all(username=username) + + +def get_followers_batched(username=None, max_items=500): followers = [] followers_batch = pinterest.get_user_followers(username=username) while len(followers_batch) > 0 and len(followers) < max_items: @@ -72,6 +85,11 @@ def get_followers(username=None, max_items=500): return followers +def get_followers(username=None): + # Gets a full list of user followers + return pinterest.get_user_followers_all(username=username) + + def get_home_feed(max_items=100): # This is what pinterest displays on your home page # useful for auto repins @@ -204,4 +222,3 @@ def get_board_sections(board_id=''): def get_board_section_feed(section_id=''): return pinterest.get_section_pins(section_id=section_id) - diff --git a/py3pin/Pinterest.py b/py3pin/Pinterest.py index 35785d7..14a6723 100644 --- a/py3pin/Pinterest.py +++ b/py3pin/Pinterest.py @@ -18,6 +18,7 @@ HOME_PAGE = 'https://www.pinterest.com/' LOGIN_PAGE = 'https://www.pinterest.com/login/?referrer=home_page' CREATE_USER_SESSION = 'https://www.pinterest.com/resource/UserSessionResource/create/' +DELETE_USER_SESSION = 'https://www.pinterest.com/resource/UserSessionResource/delete/' USER_RESOURCE = 'https://www.pinterest.com/_ngjs/resource/UserResource/get' BOARD_PICKER_RESOURCE = 'https://www.pinterest.com/resource/BoardPickerBoardsResource/get' BOARDS_RESOURCE = 'https://www.pinterest.com/_ngjs/resource/BoardsResource/get' @@ -60,7 +61,7 @@ class Pinterest: - def __init__(self, password='', proxies=None, username='', email='', cred_root='data'): + def __init__(self, password='', proxies=None, username='', email='', cred_root='data', user_agent=None): self.email = email self.username = username self.password = password @@ -68,6 +69,7 @@ def __init__(self, password='', proxies=None, username='', email='', cred_root=' self.bookmark_manager = BookmarkManager() self.http = requests.session() self.proxies = proxies + self.user_agent = user_agent data_path = os.path.join(cred_root, self.email) + os.sep if not os.path.isdir(data_path): @@ -79,13 +81,16 @@ def __init__(self, password='', proxies=None, username='', email='', cred_root=' if cookies is not None: self.http.cookies.update(cookies) + if self.user_agent is None: + self.user_agent = AGENT_STRING + def request(self, method, url, data=None, files=None, extra_headers=None): headers = CaseInsensitiveDict([ ('Referer', HOME_PAGE), ('X-Requested-With', 'XMLHttpRequest'), ('Accept', 'application/json'), ('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'), - ('User-Agent', AGENT_STRING)]) + ('User-Agent', self.user_agent)]) csrftoken = self.http.cookies.get('csrftoken') if csrftoken: headers.update([('X-CSRFToken', csrftoken)]) @@ -106,6 +111,14 @@ def post(self, url, data=None, files=None, headers=None): return self.request('POST', url=url, data=data, files=files, extra_headers=headers) def login(self): + """ + Logs user in with the provided credentials + User session is stored in the 'cred_root' folder + and reused so there is no need to login every time. + Pinterest sessions lasts for about 15 days + Ideally you need to call this method 3-4 times a month at most. + :return python dict object describing the pinterest response + """ self.get(HOME_PAGE) self.get(LOGIN_PAGE) @@ -117,7 +130,22 @@ def login(self): data = self.req_builder.buildPost(options=options, source_url='/login/?referrer=home_page') return self.post(url=CREATE_USER_SESSION, data=data) + def logout(self): + """ + Logs current user out. Takes few seconds for the session to be invalidated on pinterest's side + """ + options = { + 'disable_auth_failure_redirect': True + } + + data = self.req_builder.buildPost(options=options) + return self.post(url=DELETE_USER_SESSION, data=data) + def get_user_overview(self, username=None): + """ + :param username target username, if left blank current user is assumed + :return python dict describing the pinterest user profile response + """ if username is None: username = self.username @@ -132,6 +160,14 @@ def get_user_overview(self, username=None): return result['resource_response']['data'] def boards(self, username=None, page_size=50): + """ + The data returned is chunked, this comes from pinterest'a rest api. + Some users might have huge number of boards that is why it make sense to chunk the data. + In order to obtain all boards this method needs to be called until it returns empty list + :param username: target username, if left blank current user is assumed + :param page_size: controls the batch size for each request + :return python dict describing all the boards of a user. + """ if username is None: username = self.username @@ -157,7 +193,31 @@ def boards(self, username=None, page_size=50): self.bookmark_manager.add_bookmark(primary='boards', secondary=username, bookmark=bookmark) return result['resource_response']['data'] + def boards_all(self, username=None): + """ + Obtains all boards of a user. + NOTE: some users might have hube amounts of boards. + In such cases 'boards' method (which is batched) should be used in order to avoid memory issues + :param username: target user, if left blank current user is assumed + :return all boards of a user + """ + boards = [] + board_batch = self.boards(username=username) + while len(board_batch) > 0: + boards += board_batch + board_batch = self.boards(username=username) + + return boards + def create_board(self, name, description='', category='other', privacy='public', layout='default'): + """ + Creates a new board and returns the response from pinterest. + :param name: board name (should be unique per user) + :param description: board description + :param category: if you have defined categories (it is not visible to external users) + :param privace: can be public or private + :param layout: looks like a legacy parameter but is it mandatory (can be left as + """ options = { "name": name, "description": description, @@ -173,26 +233,54 @@ def create_board(self, name, description='', category='other', privacy='public', return self.post(url=CREATE_BOARD_RESOURCE, data=data) def follow_board(self, board_id): + """ + Follows a board with current user. + :param board_id: the id of the board to follow + :return python dict with the pinterest response + """ options = {"board_id": board_id} data = self.req_builder.buildPost(options=options) return self.post(url=FOLLOW_BOARD_RESOURCE, data=data) def unfollow_board(self, board_id): + """ + UnFollows a board with current user. + :param board_id: the id of the board to follow + :return python dict with the pinterest response + """ options = {"board_id": board_id} data = self.req_builder.buildPost(options=options) return self.post(url=UNFOLLOW_BOARD_RESOURCE, data=data) def follow_user(self, user_id): + """ + Follows a user with current user. + :param user_id: the id of the user to follow + :return python dict with the pinterest response + """ options = {"user_id": user_id} data = self.req_builder.buildPost(options=options) return self.post(url=FOLLOW_USER_RESOURCE, data=data) def unfollow_user(self, user_id): + """ + UnFollows a user with current user. + :param user_id: the id of the user to follow + :return python dict with the pinterest response + """ options = {"user_id": user_id} data = self.req_builder.buildPost(options=options) return self.post(url=UNFOLLOW_USER_RESOURCE, data=data) def get_following(self, username=None, page_size=250): + """ + Get all users following this particular user. + The response of this method is batched, meaning it needs to be called + until empty list is returned + :param username: target user, if left blank current user is assumed + :param page_size: + :return: python dict describing the 'following' list + """ if username is None: username = self.username @@ -222,7 +310,30 @@ def get_following(self, username=None, page_size=250): return result['data'] + def get_following_all(self, username=None): + """ + Obtains list of all users that the specified user follows. + NOTE: Some users might have huge following lists. + In such cases using 'get_following' (which is batched) is preferred. + :param username: target username + :return: python dict containing all following + """ + following = [] + following_batch = self.get_following(username=username) + while len(following_batch) > 0: + following += following_batch + following_batch = self.get_following(username=username) + + return following + def get_user_followers(self, username=None, page_size=250): + """ + Obtains a list of user's followers. + The response from this method is batched, meaning it needs to be called until empty list is returned. + :param username: target username, is left blank current user is assumed + :param page_size: batch size + :return: python dict describing user followers + """ if username is None: username = self.username @@ -253,7 +364,33 @@ def get_user_followers(self, username=None, page_size=250): return result['data'] + def get_user_followers_all(self, username=None): + """ + Obtains a list of all the followers a user has. + NOTE: Some users might have huge followers lists. + In such cases 'get_user_followers' should be used to avoid memory errors + :param username: target user, is left blank current user is assumed + :return: list of follower objects + """ + followers = [] + followers_batch = self.get_user_followers(username=username) + while len(followers_batch) > 0: + followers += followers_batch + followers_batch = self.get_user_followers(username=username) + + return followers + def pin(self, board_id, image_url, description='', link='', title='', section_id=None): + """ + Perfoms a pin operation. If you want to upload local image use 'upload_pin' + :param board_id: id of the target board (current user should have rights to pin to is) + :param image_url: web url of an image (not local one) + :param description: pin description (can be blank) + :param link: link to include (can be blank) + :param title: title can be blank + :param section_id: board section should be previously defined and its optional + :return: python dict describing the pinterest response + """ options = { "board_id": board_id, "image_url": image_url, @@ -270,11 +407,21 @@ def pin(self, board_id, image_url, description='', link='', title='', section_id return self.post(url=PIN_RESOURCE_CREATE, data=data) def upload_pin(self, board_id, image_file, description='', link='', title='', section_id=None): + """ + This method is simmilar to 'pin' except the image for the pin is local file. + """ image_url = self._upload_image(image_file=image_file).json()['image_url'] return self.pin(board_id=board_id, description=description, image_url=image_url, link=link, title=title, section_id=section_id) def repin(self, board_id, pin_id, section_id=None): + """ + Repin/Save action + :param board_id: board id, current user should have right to pin to this board + :param pin_id: pin id to repin + :param section_id: board section should be previously defined and its optional + :return: python dict describing the pinterest response + """ options = { "board_id": board_id, "pin_id": pin_id, @@ -302,12 +449,23 @@ def _upload_image(self, image_file): return self.post(url=UPLOAD_IMAGE, data=form_data, headers=headers) def delete_pin(self, pin_id): + """ + Deletes a pint the user owne + :param pin_id: pin id to delete + :return: python dict describing the pinterest response + """ options = {"id": pin_id} source_url = '/{}/'.format(self.username) data = self.req_builder.buildPost(options=options, source_url=source_url) return self.post(url=DELETE_PIN_RESOURCE, data=data) def comment(self, pin_id, text): + """ + Put comment on a pin + :param pin_id: pin id to comment on + :param text: text of the comment + :return: python dict describing the pinterest response + """ pin_data = self.load_pin(pin_id=pin_id) options = { "objectId": pin_data['aggregated_pin_data']['id'], @@ -320,6 +478,11 @@ def comment(self, pin_id, text): return self.post(url=CREATE_COMMENT_RESOURCE, data=data) def load_pin(self, pin_id): + """ + Loads full information about a pin + :param pin_id: pin id to load + :return: python dict describing the pinterest response + """ resp = self.get(url=LOAD_PIN_URL_FORMAT.format(pin_id)) soup = BeautifulSoup(resp.text, 'html.parser') scripts = soup.findAll('script') @@ -327,9 +490,16 @@ def load_pin(self, pin_id): for s in scripts: if 'pins' in s.text and 'aggregated_pin_data' in s.text: pin_data = json.loads(s.text) - return pin_data['resourceResponses'][0]['response']['data'] + return pin_data['pins'][str(pin_id)] def get_comments(self, pin_id, page_size=50): + """ + Get comments on a pin. + The response is batched, meaning this method should be called util empty list is returned + :param pin_id: target pin id + :param page_size: batch size + :return: list of comment objects + """ pin_data = self.load_pin(pin_id=pin_id) next_bookmark = self.bookmark_manager.get_bookmark(primary='pin_comments', secondary=pin_id) @@ -357,18 +527,53 @@ def get_comments(self, pin_id, page_size=50): return resp['data'] + def get_comments_all(self, pin_id): + """ + Obtains all comments of a pin. + NOTE: IF pin has too many comments this might cause memory issues. + In such cases use 'get_comments' which is batched + :param pin_id: + :return: list of comment objects + """ + results = [] + search_batch = self.get_comments(pin_id=pin_id) + while len(search_batch) > 0: + results += search_batch + search_batch = self.get_comments(pin_id=pin_id) + + return results + def delete_comment(self, pin_id, comment_id): + """ + Deletes a comment + :param pin_id: pin id to search the comment in + :param comment_id: comment id + :return: + """ options = {"commentId": comment_id} source_url = "/pin/{}/".format(pin_id) data = self.req_builder.buildPost(options=options, source_url=source_url) return self.post(url=DELETE_COMMENT, data=data) def invite(self, board_id, user_id): + """ + Invite a user to one of the current users boards + :param board_id: board to invite to + :param user_id: user to invite + :return: python dict describing the pinterest response + """ options = {"board_id": board_id, "invited_user_ids": [user_id]} data = self.req_builder.buildPost(options=options) return self.post(url=BOARD_INVITE_RESOURCE, data=data) def get_board_invites(self, board_id, page_size=100): + """ + Returns a list of users invited to the specified board. + This method is batched and needs to be called until empty list is returned. + :param board_id: id of target board + :param page_size: batch size + :return: list of board objects + """ options = { "isPrefetch": False, "board_id": board_id, @@ -384,7 +589,30 @@ def get_board_invites(self, board_id, page_size=100): return resp['resource_response']['data'] + def get_board_invites_all(self, board_id): + """ + Obtains all invites of a board. + NOTE: If board has too many invites this might cause memory issues. + In such cases use 'get_board_invites' which is batched + :param board_id: + :return: list of board invite objects + """ + results = [] + search_batch = self.get_board_invites(board_id=board_id) + while len(search_batch) > 0: + results += search_batch + search_batch = self.get_board_invites(board_id=board_id) + + return results + def delete_invite(self, board_id, invited_user_id, also_block=False): + """ + Deletes invite for a board + :param board_id: board id + :param invited_user_id: invited user id + :param also_block: you can also block the user (default false) + :return: python dict describing the pinterest response + """ options = { "ban": also_block, "board_id": board_id, @@ -395,6 +623,14 @@ def delete_invite(self, board_id, invited_user_id, also_block=False): return self.post(url=BOARD_DELETE_INVITE_RESOURCE, data=data) def search(self, scope, query, page_size=250): + """ + Gives access to pinterest search api + This method is batched, meaning is needs to be called until empty list is returned. + :param scope: can be pins, buyable_pins, my_pins, videos, boards + :param query: search phrase + :param page_size: batch size + :return: list of search results + """ next_bookmark = self.bookmark_manager.get_bookmark(primary='search', secondary=query) @@ -427,6 +663,14 @@ def search(self, scope, query, page_size=250): return resp['resource_response']['data']['results'] def board_recommendations(self, board_id='', page_size=50): + """ + This gives the list of pins you see when you open a board and click on 'More Ideas' + This method is batched and needs to be called until empty list is returned in order to obtain all + of the results. + :param board_id: target board id + :param page_size: batch size + :return: + """ next_bookmark = self.bookmark_manager.get_bookmark(primary='boards', secondary=board_id) if next_bookmark == '-end-': @@ -448,6 +692,9 @@ def board_recommendations(self, board_id='', page_size=50): return response['resource_response']['data'] def get_pinnable_images(self, url): + """ + Simple API pinterest uses to suggest images from site. + """ options = {"isPrefetch": 'false', "url": url, "source": "pin_create", @@ -465,7 +712,13 @@ def get_pinnable_images(self, url): return urls def home_feed(self, page_size=100): - + """ + This gives the list of pins you see when you open the pinterest home page. + This method is batched, in order to obtain all home feed items + it needs to be called until empty list is returned + :param page_size: + :return: + """ next_bookmark = self.bookmark_manager.get_bookmark(primary='home_feed') if next_bookmark == '-end-': return [] @@ -494,6 +747,11 @@ def home_feed(self, page_size=100): return response['resource_response']['data'] def board_feed(self, board_id='', page_size=250): + """ + Gives a list of all pins in a board. + This method is batched, meaning in order to obtain all pins in a board you need + to call it until empty list is returned. + """ next_bookmark = self.bookmark_manager.get_bookmark(primary='board_feed', secondary=board_id) if next_bookmark == '-end-': @@ -517,7 +775,11 @@ def board_feed(self, board_id='', page_size=250): return response['resource_response']['data'] - def initiate_conversation(self, user_ids=None, message='hi'): + def initiate_conversation(self, user_ids, message='hi'): + """ + Initiates a new conversation with one or more users + :return: python dict object describing the pinterest response + """ options = { "user_ids": user_ids, "text": message @@ -526,6 +788,9 @@ def initiate_conversation(self, user_ids=None, message='hi'): return self.post(url=CONVERSATION_RESOURCE_CREATE, data=data) def send_message(self, message='', conversation_id='', pin_id=''): + """ + Sends a new mesage to an already initiated conversation + """ options = { "conversation_id": conversation_id, "text": message, @@ -536,6 +801,9 @@ def send_message(self, message='', conversation_id='', pin_id=''): return self.post(url=SEND_MESSAGE, data=data) def load_conversation(self, conversation_id=''): + """ + Loads a list of all messages in a conversation + """ messages = [] message_batch = self._load_conversation_batch(conversation_id=conversation_id) @@ -567,6 +835,9 @@ def _load_conversation_batch(self, conversation_id='', page_size=25): return response['resource_response']['data'] def get_conversations(self): + """ + Loads a list of all conversations the current user has + """ conversations = [] conv_batch = self._get_conversation_batch() while len(conv_batch) > 0: @@ -596,6 +867,9 @@ def _get_conversation_batch(self): return response['resource_response']['data'] def create_board_section(self, board_id='', section_name=''): + """ + Creates a new section in a board the current user owns + """ options = { "board_id": board_id, "initial_pins": [], @@ -607,6 +881,9 @@ def create_board_section(self, board_id='', section_name=''): return self.post(url=BOARD_SECTION_RESOURCE, data=data) def get_board_sections(self, board_id=''): + """ + Obtains a list of all sections of a board + """ options = { "isPrefetch": False, "board_id": board_id, @@ -618,6 +895,11 @@ def get_board_sections(self, board_id=''): return response['resource_response']['data'] def get_section_pins(self, section_id='', page_size=250): + """ + Returns a list of all pins in a board section. + This method is batched meaning in order to obtain all pins in the section + you need to call is until empty list is returned + """ options = { "isPrefetch": False, "field_set_key": "react_grid_pin", @@ -638,6 +920,9 @@ def get_section_pins(self, section_id='', page_size=250): return pins def delete_board_section(self, section_id=''): + """ + Deletes a board section by id + """ options = { "section_id": section_id }