-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPinterest_Socket.py
152 lines (133 loc) · 5.49 KB
/
Pinterest_Socket.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/python2
from pinterest.client import raw_client
from pinterest.client import ApiError
import json
from random import choice
from random import randint
import time
from datetime import datetime
class Pintrest_Socket(object):
def __init__(self):
self.APP_ID = "1435585"
self.APP_SECRET = "721b878beede8ced1d3441e550841c85dbe65261"
self.pin_client = raw_client(self.APP_ID, self.APP_SECRET)
def find_pins(self, user_name):
# Grab user boards
user_boards_response = ""
follow_boards_response = ""
try:
user_boards_response = self.pin_client.users(user_name).boards.get()
follow_boards_response = self.pin_client.users(user_name).boards.following.get()
except ApiError, e:
# Something went wrong on the way to the Pinterest
print 'Code:', e.code
print 'Message:', e.message
print 'Detail:', e.detail
return None, None, None
boards = user_boards_response[0] + follow_boards_response[0]
while len(boards) != 0:
chosen_board = choice(boards)
postcard_type = randint(1, 5)
pins = []
if postcard_type <= 3:
# 60% chance to grab from chosen board
pins = self.__grab_pins(chosen_board)
elif postcard_type == 4:
# 20% chance to grab from contributor's board
#TODO: change chosen board to contributor's board
pins = self.__grab_pins(chosen_board)
elif postcard_type == 5:
# 20% chance to grab form a related board
#TODO: change chosen board to a related board
pins = self.__grab_pins(chosen_board)
if len(pins):
# Return top pin
top_pin = pins[0]
domain = None
try:
domain = self.pin_client.domains(top_pin['domain']).get()
except ApiError, e:
# Ignore this error for now. We are probably not using the domain
pass
return pins[0], chosen_board, domain
# No pins found. Remove board if postcard_type 1-3 and loop
if postcard_type <= 3:
boards.remove(chosen_board)
raise Exception("No valid pins for given user.")
def __convert_time(self, pin):
"""
Convert from Pinterest's time format to time_struct
"""
return time.strptime(pin['created_at'], "%a, %d %b %Y %H:%M:%S +0000")
def __filter_pin_date(self, pin):
"""
Make sure this pin was posted in the last month
"""
pin_time = self.__convert_time(pin)
now_time = datetime.now()
cutoff_time = None
if now_time.month == 1:
cutoff_time = datetime(now_time.year-1, 12, now_time.day)
else:
cutoff_time = datetime(now_time.year, now_time.month-1, now_time.day)
if pin_time >= time.mktime(cutoff_time.timetuple()):
# Pin was posted after cutoff date
return 1
# Pin was posted before the cutoff date
return 0
def __filter_pin_size(self, pin):
"""
Make sure this pin has a picture with an appropriate size
"""
pin_size = pin['image_large_size_pixels']
if ((pin_size['width'] >= 600 and pin_size['height'] >= 400) or
(pin_size['width'] >= 400 and pin_size['height'] >= 600)):
# Pin picture is large enough
return 1
# Pin picture is not large enough
return 0
def __grab_pins(self, board):
"""
Grab pins from the last month
"""
board_pins = []
try:
# Keep track of page place of pins
bookmark = None
# Go for a maximum of 10 pages worth of pins
num_calls = 0
while num_calls < 10:
if bookmark == None:
board_pin_response = self.pin_client.boards(board['id']).pins.get()
else:
board_pin_response = self.pin_client.boards(board['id']).pins.get(bookmark=bookmark)
# Sort based on time
sorted_pins = sorted(board_pin_response[0],
key=self.__convert_time)
# Filter based on date
date_filtered_pins = filter(self.__filter_pin_date, sorted_pins)
# Filter based on size
size_filtered_pins = filter(self.__filter_pin_date, date_filtered_pins)
board_pins += size_filtered_pins
# Keep track of next bookmark
bookmark = board_pin_response[1]
if len(sorted_pins) != len(date_filtered_pins) or bookmark == None:
# Kill loop if no more pins, or out of month range
num_calls = 10
else:
# Otherwise iterate
num_calls += 1
except ApiError, e:
print 'Code:', e.code
print 'Message:', e.message
print 'Detail:', e.detail
raise
# Sort based on like count (reversed so top is first)
return sorted(board_pins, key=lambda x: x['like_count'], reverse=1)
def Get(user_name):
client = Pintrest_Socket()
top_pin, board, domain = client.find_pins(user_name)
# print '\n\nTop Pin:\n\n', top_pin
# print '\n\nBoard:\n\n', board
# print '\n\nDomain:\n\n', domain
return top_pin