· 6 years ago · Sep 29, 2019, 12:30 PM
1#https://gist.github.com/gbaman/b3137e18c739e0cf98539bf4ec4366ad
2import requests
3import pandas as pd
4url = "https://api.pipefy.com/graphql"
5
6payload = """{\"query\":\"{ me { name } }\"}"""
7#TODO:retirar essa key daí pfv!!!!!
8headers = {
9 'authorization': "Bearer SUA_KEY_DO_PIPEFY_AQUI",
10 'content-type': "application/json"
11 }
12def run_query(query): # A simple function to use requests.post to make the API call. Note the json= section.
13 request = requests.post('https://api.pipefy.com/graphql', json={'query': query}, headers=headers)
14 if request.status_code == 200:
15 return request.json()
16 else:
17 raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code, query))
18
19def get_table_record(id):
20 query = """
21 {
22 table_record(id: """+str(id)+""") {
23 assignees {
24 id
25 name
26 }
27 created_at
28 created_by {
29 id
30 name
31 }
32 due_date
33 finished_at
34 id
35 labels {
36 id
37 name
38 }
39 parent_relations {
40 name
41 source_type
42 }
43 record_fields {
44 array_value
45 date_value
46 datetime_value
47 filled_at
48 float_value
49 name
50 required
51 updated_at
52 value
53 }
54 summary {
55 title
56 value
57 }
58 table {
59 id
60 }
61 title
62 updated_at
63 url
64 }
65 }
66 """
67 return run_query(query)
68
69def get_table(id):
70 query = """
71 {
72 table_records( table_id: \""""+str(id)+"""\") {
73 edges {
74
75 cursor
76 node {
77 id
78 title
79 url
80
81 }
82 }
83 pageInfo {
84 endCursor
85 hasNextPage
86 hasPreviousPage
87 startCursor
88
89 }
90 }
91 }
92 """
93 return run_query(query)
94
95#Database Inventário
96inventario_table = get_table("T_WuNkpd")
97print(inventario_table)
98#Coletando os ids
99item_ids = []
100for edge in inventario_table['data']['table_records']['edges']:
101 item_ids.append(edge['node']['id'])
102
103#Coletando as colunas da database
104columns = []
105record_to_extract_column = get_table_record(item_ids[0])
106for record_field in record_to_extract_column['data']['table_record']['record_fields']:
107 columns.append(record_field['name'])
108 #print(i['name'])
109 #print(i['value'])
110
111inventario_df = pd.DataFrame()
112
113#Salvando os Itens dentro do dataframe
114for item_id in item_ids:
115 record_aux = get_table_record(item_id)
116 record_dict_aux = {}
117 for record_field in record_aux['data']['table_record']['record_fields']:
118 if record_field['name'] not in record_dict_aux.keys():
119 record_dict_aux.update({record_field['name']:0})
120 record_dict_aux[record_field['name']] = record_field['value']
121 #print(record_field['name'])
122 #print(record_field['value'])
123 print(record_dict_aux)
124 inventario_df = inventario_df.append(record_dict_aux,ignore_index=True)
125
126inventario_df.to_csv('inventario.csv')
127print(columns)