diff --git a/examples.py b/examples.py index 95dac55..5eab3e3 100644 --- a/examples.py +++ b/examples.py @@ -143,10 +143,9 @@ def search(): results = [] max_results = 100 search_batch = pinterest.search(scope='boards', query='food') - while len(search_batch) > 0: + while len(search_batch) > 0 and len(results) < max_results: results += search_batch - if len(results) > max_results: - break + search_batch = pinterest.search(scope='boards', query='food') return results diff --git a/follow_examples.py b/follow_examples.py new file mode 100644 index 0000000..7ad86ec --- /dev/null +++ b/follow_examples.py @@ -0,0 +1,63 @@ +from py3pin.Pinterest import Pinterest + +username = 'username' +password = 'password!' +email = 'email' +# cred_root is the place the user sessions and cookies will be stored you should specify this to avoid permission issues +cred_root = 'cred_root' + +pinterest = Pinterest(email=email, + password=password, + username=username, + cred_root=cred_root) + + +def search_boards(): + results = [] + max_results = 100 + batch_size = 50 + query = 'food' + scope = 'boards' + + # Obtain a list of boards we want to follow + search_batch = pinterest.search(scope=scope, query=query, page_size=batch_size) + while len(search_batch) > 0 and len(results) < max_results: + results += search_batch + pinterest.search(scope=scope, query=query, page_size=batch_size) + + return results + +def search_users(): + # pinterest no longer allows to search for users, we will search for pins and extract the pinners + results = [] + max_results = 100 + batch_size = 50 + query = 'food' + scope = 'pins' + + # Obtain a list of boards we want to follow + search_batch = pinterest.search(scope=scope, query=query, page_size=batch_size) + while len(search_batch) > 0 and len(results) < max_results: + for res in search_batch: + if 'pinner' in res: + results.append(res['pinner']) + search_batch = pinterest.search(scope=scope, query=query, page_size=batch_size) + + return results + + +boards = search_boards() + +# Follow boards +for b in boards: + print(b['url']) + pinterest.follow_board(board_id=b['id'], board_url=b['url']) + # Instead of break you need a way to pause for some time in order to not get banned from pinterest. + break + +users = search_users() + +for u in users: + pinterest.follow_user(user_id=u['id'], username=u['username']) + # Instead of break you need a way to pause for some time in order to not get banned from pinterest. + break