Skip to content

Commit

Permalink
Merge pull request #25 from bstoilov/upload-pin-feature
Browse files Browse the repository at this point in the history
upload pin from local file
  • Loading branch information
bstoilov committed Nov 21, 2019
2 parents 072819e + f5cbcc3 commit 4aba5cd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
11 changes: 11 additions & 0 deletions examples.py
Expand Up @@ -112,6 +112,16 @@ def pin(board_id='',
title=title, link=link)


def upload_pin(board_id='',
section_id=None,
image_path='my_imag.png',
description='this is auto pin',
title='a bot did this',
link='https://www.google.com/'):
return pinterest.upload_pin(board_id=board_id, section_id=section_id, image_file=image_path,
description=description, title=title, link=link)


def search(max_items=100, scope='boards', query='food'):
# After change in pinterest API, you can no longer search for users
# Instead you need to search for something else and extract the user data from there.
Expand Down Expand Up @@ -184,3 +194,4 @@ def get_board_sections(board_id=''):

def get_board_section_feed(username='', board_name='', section_name=''):
return pinterest.get_section_pins(username=username, board_name=board_name, section_name=section_name)

34 changes: 31 additions & 3 deletions py3pin/Pinterest.py
Expand Up @@ -2,7 +2,9 @@
import os

import requests
import mimetypes
import requests.cookies
from requests_toolbelt import MultipartEncoder
from bs4 import BeautifulSoup
from py3pin.BookmarkManager import BookmarkManager
from py3pin.Registry import Registry
Expand Down Expand Up @@ -52,6 +54,7 @@
BOARD_SECTION_RESOURCE = 'https://www.pinterest.com/resource/BoardSectionResource/create/'
GET_BOARD_SECTIONS = 'https://www.pinterest.com/resource/BoardSectionsResource/get/'
BOARD_SECTION_EDIT_RESOURCE = 'https://www.pinterest.com/resource/BoardSectionEditResource/delete/'
UPLOAD_IMAGE = 'https://www.pinterest.com/upload-image/'


class Pinterest:
Expand All @@ -75,7 +78,7 @@ def __init__(self, password='', proxies=None, username='', email='', cred_root='
if cookies is not None:
self.http.cookies.update(cookies)

def request(self, method, url, data=None, files=None):
def request(self, method, url, data=None, files=None, extra_headers=None):
headers = CaseInsensitiveDict([
('Referer', HOME_PAGE),
('X-Requested-With', 'XMLHttpRequest'),
Expand All @@ -86,6 +89,10 @@ def request(self, method, url, data=None, files=None):
if csrftoken:
headers.update([('X-CSRFToken', csrftoken)])

if extra_headers is not None:
for h in extra_headers:
headers.update([(h, extra_headers[h])])

response = self.http.request(method, url, data=data, headers=headers, files=files, proxies=self.proxies)
response.raise_for_status()
self.registry.update(Registry.Key.COOKIES, response.cookies)
Expand All @@ -94,8 +101,8 @@ def request(self, method, url, data=None, files=None):
def get(self, url):
return self.request('GET', url=url)

def post(self, url, data=None, files=None):
return self.request('POST', url=url, data=data, files=files)
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):
self.get(HOME_PAGE)
Expand Down Expand Up @@ -261,6 +268,11 @@ 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):
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):
options = {
"board_id": board_id,
Expand All @@ -272,6 +284,22 @@ def repin(self, board_id, pin_id, section_id=None):
data = self.req_builder.buildPost(options=options, source_url=source_url)
return self.post(url=REPIN_RESOURCE_CREATE, data=data)

def _upload_image(self, image_file):
file_name = os.path.basename(image_file)
mime_type = mimetypes.guess_type(image_file)[0]

form_data = MultipartEncoder(fields={
'img': ('%s' % file_name, open(image_file, 'rb'), mime_type)
})

headers = {
'Content-Length': '%s' % form_data.len,
'Content-Type': form_data.content_type,
'X-UPLOAD-SOURCE': 'pinner_uploader'
}

return self.post(url=UPLOAD_IMAGE, data=form_data, headers=headers)

def delete_pin(self, pin_id):
options = {"id": pin_id}
source_url = '/{}/'.format(self.username)
Expand Down

0 comments on commit 4aba5cd

Please sign in to comment.