Last active
December 14, 2015 10:58
-
-
Save binux/5075775 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- encoding: utf-8 -*- | |
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8: | |
# Author: Binux<i@binux.me> | |
# http://binux.me | |
# Created on 2013-03-15 16:39:39 | |
import re | |
import json | |
import requests | |
class PinterestAPI(object): | |
def __init__(self): | |
self.reset() | |
def reset(self): | |
self.session = requests.Session() | |
self.session.headers.update({ | |
'User-Agent': 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-us) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/5 ', | |
}) | |
def login(self, email, password): | |
r = self.session.get('https://pinterest.com/login/') | |
r.raise_for_status() | |
self.app_version = re.search(r'{"app_version": "(\w+)"}', r.text).group(1) | |
r = self.session.post('https://pinterest.com/resource/UserSessionResource/create/', | |
data={ | |
'data': json.dumps({ | |
'options': { | |
'username_or_email': email, | |
'password': password, | |
}, | |
'context': { | |
'app_version': self.app_version, | |
} | |
}), | |
'source_url': '/login/', | |
}, | |
headers = { | |
'Referer': 'https://pinterest.com/login/', | |
'X-CSRFToken': self.session.cookies['csrftoken'], | |
'X-NEW-APP': 1, | |
'X-Requested-With': 'XMLHttpRequest', | |
}) | |
r.raise_for_status() | |
r = self.session.get('https://pinterest.com/') | |
if r.url == 'http://pinterest.com/': | |
self.username = re.search(r'"username": "(\w+)", ', r.text).group(1) | |
self.email = email | |
self.password = password | |
return True | |
return False | |
def add(self, board_id, description, link, image_url): | |
data = { | |
"options":{ | |
"board_id":board_id, | |
"description":description, | |
"link":link, | |
"image_url":image_url, | |
"method":"scraped" | |
}, | |
"context":{ | |
"app_version":self.app_version, | |
} | |
} | |
r = self.session.post('http://pinterest.com/resource/PinResource/create/', | |
data = { | |
'data': json.dumps(data), | |
'source_url': '/', | |
'module_path': 'App()', | |
}, | |
headers = { | |
'X-CSRFToken': self.session.cookies['csrftoken'], | |
'X-NEW-APP': 1, | |
'X-Requested-With': 'XMLHttpRequest', | |
}) | |
r_json = json.loads(r.text) | |
return r_json['http_status'] == 200 and r_json['resource_response']['error'] is None | |
def boards(self): | |
data = { | |
"options":{}, | |
"module":{ | |
"name":"UserBoards", | |
"options":{ | |
"username":self.username, | |
"secret_board_count":0 | |
}, | |
"append":False, | |
"errorStrategy":0 | |
}, | |
"context":{ | |
"app_version":self.app_version, | |
} | |
} | |
r = self.session.get('http://pinterest.com/resource/NoopResource/get/', params={'data': json.dumps(data)}) | |
assert json.loads(r.text)['http_status'] == 200, 'http_status == 200' | |
result = {} | |
def parse(tree): | |
if not tree.get('children'): | |
return | |
for each in tree['children']: | |
if each['name'] == 'ShowModalButton' and each.get('id') == 'boardCoverEditButton': | |
board = each['options']['submodule']['options'] | |
result[board['board_name']] = board['board_id'] | |
parse(each) | |
parse(json.loads(r.text)['module']['tree']) | |
return result | |
def get_info(self): | |
r = self.session.get('http://pinterest.com/resource/UserSettingsResource/get/') | |
if r.ok: | |
return json.loads(r.text)['resource_response']['data'] | |
def check_login(self): | |
if self.get_info(): | |
return True | |
else: | |
self.reset() | |
return self.login(self.email, self.password) | |
if __name__ == '__main__': | |
import os | |
p = PinterestAPI() | |
p.login(os.environ['PINTEREST_USER'], os.environ['PINTEREST_PASS']) | |
print p.check_login() | |
print p.get_info() | |
boards = p.boards() | |
print boards | |
print p.add(boards[boards.keys()[0]], '', '', '') |
What about the PinterestAPI class? Is it the official API package? Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
Currently "http://pinterest.com/resource/PinResource/" page is exist? I think that is not exist. So i have some issues for the pinterest pinning action. So please give me a solution.
Thanks