Python으로 100개 그룹을 프로그래매틱하게 생성하기

GPT 봇에게 물어봤고, 꽤 근접한 답변을 주었지만, 저는 초보라 무언가를 망치기 전에 현명한 조언을 듣고 싶습니다.

그룹 이름 100개 목록이 있습니다(쉼표로 구분하거나, 파일로 제공하거나, 다른 형식도 괜찮습니다).

이들을 가져와서 모두 아래에 나열된 동일한 설정을 적용하고 싶습니다:

사용자가 자유롭게 그룹에 가입할 수 있도록 허용(공개적으로 표시되는 그룹 필요)
사용자가 자유롭게 그룹을 나갈 수 있도록 허용
–자동 멤버 없음–

그리고

아래 봇의 답변은 시작점으로 보이지만, 제가 직접 코드를 수정하는 방식에는 확신이 서지 않습니다:

GPT의 답변:

그러나 Discourse API를 사용하여 프로그래밍 방식으로 그룹을 만들 수 있습니다. 이를 위해서는 몇 가지 기술적 지식과 API에 대한 익숙함이 필요합니다.

requests 라이브러리를 사용하여 Python으로 이를 수행하는 기본 예시는 다음과 같습니다:

import requests

api_key = 'your_api_key'
api_username = 'your_api_username'
base_url = 'https://your_discourse_url.com'

headers = {
    'Api-Key': api_key,
    'Api-Username': api_username,
}

for i in range(1, 101):
    data = {
        'name': f'group_{i}',
        'visible': True,
        'automatic_membership_email_domains': 'example.com'
    }
    response = requests.post(f'{base_url}/admin/groups', headers=headers, data=data)
    print(f'Created group {i}, response code: {response.status_code}')

'your_api_key', 'your_api_username', 'https://your_discourse_url.com'를 실제 API 키, API 사용자 이름, Discourse URL로 교체하세요.

감사합니다.

대체로 맞는 것 같습니다.

다만, 100개나 반복하기 전에 먼저 하나나 두 개로 동작하는지 확인해 보세요!

Reverse engineer the Discourse API를 참고하면 그룹 설정이 원하는 대로 모두 가져와지는지 확인할 수 있습니다.

또한 https://meta.discourse.org/t/use-the-discourse-api-ruby-gem/17587도 참고해 보세요.

이런 식으로:

group_names.each do |name|
  begin
  response = client.create_group(name: name,
    visibility_level: 0,
    members_visibility_level: 0,
    mentionable_level: 3,
    messageable_level: 3,
    public_exit: true,
    public_admission: true
  )
  group_id = response["basic_group"]["id"]
  group_name =response["basic_group"]["name"]
  puts "Created #{group_name}(#{group_id})"
  sleep 1
rescue => e
  puts "oops. Had a problem with #{name} #{e}"
  sleep 1
end