Interact with discourse from Python?

Você nem sempre precisa usar a interface de outra pessoa.

Não é muito complicado fazer isso através da biblioteca requests integrada, e se você usar algo (por exemplo, Postman, que eu já configurei), você pode importar nossa especificação de API para ele e então pedir para gerar código:

e então adaptar isso às suas necessidades:

import json
import pandas
import requests

url = "https://try.discourse.org/categories.json?include_subcategories=false"

payload = {}
headers = {
  'Accept': 'application/json'
}

response = requests.request("GET", url, headers=headers, data=payload)
response_data = json.loads(response.text)
data = pandas.DataFrame(response_data['category_list']['categories']).set_index('id')

agora eu tenho informações da categoria:

            name   color text_color  ... uploaded_background_dark                                             topics can_vote
id                                   ...                                                                                     
5        general  25AAE2     FFFFFF  ...                     None  [{'fancy_title': 'Testing dulu ya jangan di hi...      NaN
4         videos  258af1     FFFFFF  ...                     None  [{'fancy_title': 'Ikan ganteng yang’&rdq...      NaN
86      calendar  12A89D     FFFFFF  ...                     None  [{'fancy_title': 'Category Calendar demo topic...      NaN
2           tech     444     FFFFFF  ...                     None  [{'fancy_title': 'Poll: What’s your pref...      NaN
1      discourse  00B355     FFFFFF  ...                     None  [{'fancy_title': 'Welcome to our demo!', 'id':...      NaN
53  Topic Voting  F7941D     FFFFFF  ...                     None  [{'fancy_title': 'Is this topic worth voting f...     True
6         gaming  800080     FFFFFF  ...                     None  [{'fancy_title': 'Impressions Games City Build...      NaN
8         movies  B22222     FFFFFF  ...                     None  [{'fancy_title': 'What’s your all-time f...      NaN
9         sports  0000FF     FFFFFF  ...                     None  [{'fancy_title': 'Modernizing the antiquated b...      NaN

[9 rows x 45 columns]

Se você quiser acessar informações protegidas, você vai querer usar uma chave de API.

6 curtidas