-
Notifications
You must be signed in to change notification settings - Fork 2
/
pipedrive-deals.py
308 lines (286 loc) · 10.9 KB
/
pipedrive-deals.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# ---
# name: pipedrive-deals
# deployed: true
# config: index
# title: Pipedrive Deals
# description: Returns a list of deals from Pipedrive
# params:
# - name: properties
# type: array
# description: The properties to return (defaults to all properties). See "Returns" for a listing of the available properties.
# required: false
# - name: filter
# type: string
# description: Filter to apply with key/values specified as a URL query string where the keys correspond to the properties to filter.
# required: false
# returns:
# - name: id
# type: integer
# description: The id of the deal
# - name: title
# type: string
# description: The title of the deal
# - name: label
# type: string
# description: The label of the deal
# - name: value
# type: number
# description: The value of the deal
# - name: currency
# type: string
# description: The currency type of the value of the deal
# - name: add_time
# type: string
# description: The date the deal was added
# - name: update_time
# type: string
# description: The date the deal was last updated
# - name: active
# type: boolean
# description: Whether or not the deal is active
# - name: deleted
# type: boolean
# description: Whether or not the deal is deleted
# - name: status
# type: string
# description: The status of the deal
# - name: probability
# type: string
# description: The probability associated with the deal
# - name: creator_user_id
# type: integer
# description: The id of the creator of the deal
# - name: creator_user_name
# type: string
# description: The name of the creator of the deal
# - name: creator_user_email
# type: string
# description: The email of the creator of the deal
# - name: user_id
# type: integer
# description: The id of the user associated with the deal
# - name: user_name
# type: string
# description: The name of the user associated with the deal
# - name: user_email
# type: string
# description: The email of the user associated with the deal
# - name: person_name
# type: string
# description: The name of the person in the organization associated with the deal
# - name: org_name
# type: string
# description: The name of the organization associated with the deal
# - name: org_address
# type: string
# description: The address of the organization associated with the deal
# - name: pipeline_id
# type: integer
# description: The pipeline id currently associated with the deal
# - name: stage_id
# type: integer
# description: The stage id of the deal
# - name: stage_change_time
# type: string
# description: The date the deal stage was last changed
# - name: last_activity_id
# type: integer
# description: The id of the last activity event associated with the deal
# - name: last_activity_date
# type: string
# description: The date of the last activity event associated with the deal
# - name: next_activity_id
# type: integer
# description: The id of the next activity event scheduled for the deal
# - name: next_activity_date
# type: string
# description: The date of the next activity event scheduled for the deal
# - name: next_activity_subject
# type: string
# description: The subject of the next activity
# - name: next_activity_type
# type: string
# description: The type of the next activity
# - name: next_activity_duration
# type: integer
# description: The duration of the next activity
# - name: next_activity_note
# type: string
# description: Notes associated with the next activity
# - name: expected_close_date
# type: string
# description: The expected date the deal will close
# - name: close_time
# type: string
# description: The date the deal was closed
# - name: won_time
# type: string
# description: The date the deal was won
# - name: lost_time
# type: string
# description: The date the deal was lost
# - name: lost_reason
# type: string
# description: The reason the deal was lost
# - name: products_count
# type: integer
# description: The number of products associated with the deal
# - name: files_count
# type: integer
# description: The number of files associated with the deal
# - name: notes_count
# type: integer
# description: The number of notes associated with the deal
# - name: email_messages_count
# type: integer
# description: The number of emails associated with the deal
# - name: activities_count
# type: integer
# description: The number of activities associated with the deal
# - name: done_activities_count
# type: integer
# description: The number of done activities associated with the deal
# - name: undone_activities_count
# type: integer
# description: The number of undone activities associated with the deal
# - name: reference_activities_count
# type: integer
# description: The number of reference activities associated with the deal
# - name: participants_count
# type: integer
# description: The number of participants associated with the deal
# - name: followers_count
# type: integer
# description: The number of followers associated with the deal
# examples:
# - '""'
# - '"title, value, status, add_time"'
# ---
import json
import urllib
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from datetime import *
from decimal import *
from collections import OrderedDict
# main function entry point
def flexio_handler(flex):
flex.output.content_type = 'application/x-ndjson'
for data in get_data(flex.vars):
flex.output.write(data)
def get_data(params):
# get the api key and company domain from the variable input
auth_token = dict(params).get('pipedrive_connection',{}).get('access_token')
api_base_uri = dict(params).get('pipedrive_connection',{}).get('api_base_uri')
# see here for more info:
# https://developers.pipedrive.com/docs/api/v1/#!/Deals/get_deals
# https://pipedrive.readme.io/docs/core-api-concepts-pagination
headers = {
'Authorization': 'Bearer ' + auth_token
}
url = api_base_uri + '/v1/deals'
page_size = 500
page_cursor_id = None
while True:
url_query_params = {'limit': page_size}
if page_cursor_id is not None:
url_query_params['start'] = page_cursor_id
url_query_str = urllib.parse.urlencode(url_query_params)
page_url = url + '?' + url_query_str
response = requests_retry_session().get(page_url, headers=headers)
response.raise_for_status()
content = response.json()
data = content.get('data',[])
if len(data) == 0: # sanity check in case there's an issue with cursor
break
buffer = ''
for item in data:
item = get_item_info(item)
buffer = buffer + json.dumps(item, default=to_string) + "\n"
yield buffer
has_more = content.get('additional_data',{}).get('pagination',{}).get('more_items_in_collection', False)
if has_more == False:
break
page_cursor_id = content.get('additional_data',{}).get('pagination',{}).get('next_start')
if page_cursor_id is None:
break
def requests_retry_session(
retries=3,
backoff_factor=0.3,
status_forcelist=(429, 500, 502, 503, 504),
session=None,
):
session = session or requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
def to_date(value):
# TODO: convert if needed
return value
def to_string(value):
if isinstance(value, (date, datetime)):
return value.isoformat()
if isinstance(value, (int)):
return str(value)
if isinstance(value, (Decimal)):
return str(value)
return value
def get_item_info(item):
# map this function's property names to the API's property names
info = OrderedDict()
info['id'] = item.get('id')
info['title'] = item.get('title')
info['label'] = item.get('label')
info['value'] = item.get('value')
info['currency'] = item.get('currency')
info['add_time'] = to_date(item.get('add_time'))
info['update_time'] = to_date(item.get('update_time'))
info['active'] = item.get('active')
info['deleted'] = item.get('deleted')
info['status'] = item.get('status')
info['probability'] = item.get('probability')
info['creator_user_id'] = (item.get('creator_user_id') or {}).get('id')
info['creator_user_name'] = (item.get('creator_user_id') or {}).get('name')
info['creator_user_email'] = (item.get('creator_user_id') or {}).get('email')
info['user_id'] = (item.get('user_id') or {}).get('id')
info['user_name'] = (item.get('user_id') or {}).get('name')
info['user_email'] = (item.get('user_id') or {}).get('email')
info['person_name'] = (item.get('person_id') or {}).get('name')
info['org_name'] = (item.get('org_id') or {}).get('name')
info['org_address'] = (item.get('org_id') or {}).get('address')
info['pipeline_id'] = item.get('pipeline_id')
info['stage_id'] = item.get('stage_id')
info['stage_change_time'] = to_date(item.get('stage_change_time'))
info['last_activity_id'] = item.get('last_activity_id')
info['last_activity_date'] = to_date(item.get('last_activity_date'))
info['next_activity_id'] = item.get('next_activity_id')
info['next_activity_date'] = to_date(item.get('next_activity_date'))
info['next_activity_subject'] = item.get('next_activity_subject')
info['next_activity_type'] = item.get('next_activity_type')
info['next_activity_duration'] = item.get('next_activity_duration')
info['next_activity_note'] = item.get('next_activity_note')
info['expected_close_date'] = to_date(item.get('expected_close_date'))
info['close_time'] = to_date(item.get('close_time'))
info['won_time'] = to_date(item.get('won_time'))
info['lost_time'] = to_date(item.get('lost_time'))
info['lost_reason'] = item.get('lost_reason')
info['products_count'] = item.get('products_count')
info['files_count'] = item.get('files_count')
info['notes_count'] = item.get('notes_count')
info['email_messages_count'] = item.get('email_messages_count')
info['activities_count'] = item.get('activities_count')
info['done_activities_count'] = item.get('done_activities_count')
info['undone_activities_count'] = item.get('undone_activities_count')
info['reference_activities_count'] = item.get('reference_activities_count')
info['participants_count'] = item.get('participants_count')
info['followers_count'] = item.get('followers_count')
return info