-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducthunt-posts.py
168 lines (148 loc) · 5.17 KB
/
producthunt-posts.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
# ---
# name: producthunt-posts
# deployed: true
# title: Product Hunt Posts
# description: Returns a list of producthunt posts for today
# 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
# returns:
# - name: id
# type: string
# description: The id for the product post
# - name: name
# type: string
# description: The name of the product being featured
# - name: url
# type: string
# description: The url of the product being featured
# - name: tagline
# type: string
# description: The tagline of the product being featured
# - name: description
# type: string
# description: A description of the product being featured
# - name: createdAt
# type: string
# description: The date the product post was created
# - name: featuredAt
# type: string
# description: The date the product was featured
# examples:
# - '"*"'
# - '"id, name, url, tagline"'
# ---
import json
import urllib
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
import itertools
from datetime import *
from decimal import *
from cerberus import Validator
from collections import OrderedDict
# main function entry point
def flexio_handler(flex):
# get the api key from the variable input
auth_token = dict(flex.vars).get('producthunt_connection',{}).get('access_token')
if auth_token is None:
raise ValueError
# get the input
input = flex.input.read()
try:
input = json.loads(input)
if not isinstance(input, list): raise ValueError
except ValueError:
raise ValueError
# define the expected parameters and map the values to the parameter names
# based on the positions of the keys/values
params = OrderedDict()
params['properties'] = {'required': False, 'validator': validator_list, 'coerce': to_list, 'default': '*'}
input = dict(zip(params.keys(), input))
# validate the mapped input against the validator
# if the input is valid return an error
v = Validator(params, allow_unknown = True)
input = v.validated(input)
if input is None:
raise ValueError
# map this function's property names to the API's property names
property_map = OrderedDict()
property_map['id'] = 'id'
property_map['name'] = 'name'
property_map['createdAt'] = 'createdAt'
property_map['featuredAt'] = 'featuredAt'
property_map['url'] = 'url'
property_map['tagline'] = 'tagline'
property_map['description'] = 'description'
# make the request
# see here for more info: https://api.producthunt.com/v2/docs
url = 'https://api.producthunt.com/v2/api/graphql'
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + auth_token,
'Host': 'api.producthunt.com'
}
columns = list(property_map.values())
data = { "query": "query { posts { edges { node {" + " ".join(columns) + " } } } }" }
response = requests_retry_session().post(url, data=json.dumps(data), headers=headers)
response.raise_for_status()
content = response.json()
# get the properties to return and the property map
properties = [p.lower().strip() for p in input['properties']]
# if we have a wildcard, get all the properties
if len(properties) == 1 and properties[0] == '*':
properties = list(property_map.keys())
# build up the result
result = []
result.append(properties)
edges = content.get('data',{}).get('posts',{}).get('edges',[])
for item in edges:
row = [item.get('node').get(property_map.get(p,'')) or '' for p in properties]
result.append(row)
flex.output.content_type = "application/json"
flex.output.write(result)
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 validator_list(field, value, error):
if isinstance(value, str):
return
if isinstance(value, list):
for item in value:
if not isinstance(item, str):
error(field, 'Must be a list with only string values')
return
error(field, 'Must be a string or a list of strings')
def to_string(value):
if isinstance(value, (date, datetime)):
return value.isoformat()
if isinstance(value, (Decimal)):
return str(value)
return value
def to_list(value):
# if we have a list of strings, create a list from them; if we have
# a list of lists, flatten it into a single list of strings
if isinstance(value, str):
return value.split(",")
if isinstance(value, list):
return list(itertools.chain.from_iterable(value))
return None