Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a new topic #8

Open
Adarrical opened this issue Mar 15, 2020 · 4 comments
Open

Creating a new topic #8

Adarrical opened this issue Mar 15, 2020 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@Adarrical
Copy link

Adarrical commented Mar 15, 2020

Hi Sam,

I use the package to create a topic. I get 404 error.
Class Client call host/topics.json?title.... etc but api documentation create post and topic in the same way host/posts.json

Sam, if you change your code host/topics.json by host/posts.json topics are created succesfully.

Thanks in advance.
Albert Darrical.

@samamorgan
Copy link
Owner

Can you show the code that is making this call and the full response?

@samamorgan samamorgan self-assigned this Mar 15, 2020
@samamorgan samamorgan added the bug Something isn't working label Mar 15, 2020
@Adarrical
Copy link
Author

Adarrical commented Mar 15, 2020

Hi Sam,

Here my full code:

import sys 
import requests
from datetime import datetime,timedelta
sys.path.append('../classes_py')
from Console import ConInputs
import discourse 

def create_topic(title, raw, category=33):
     client = discourse.Client(
             host='https://foro.ojodigital.com/',
             api_username='Albert',
             api_key='[REDACTED]',
     )

     
     return client.create_topic(title,raw,category)   


categories = {'Paisajes':13 , 
              'Otras': 17, 
              'Urbanas': 16, 
              'Retratos':14,
              'Sociales': 15,
              'Series': 18, 
              'Desnudo': 11, 
              'Flora y fauna': 12,
              'Macros':10,
              'Series':18, }

con_input = ConInputs()  #Get input date

fecha_inicio = con_input.gettime()   #Date start
fecha_fin = (fecha_inicio + timedelta(days=6))  #Date end


for category_k, category_v in categories.items(): 
        
        url_ = f"https://foro.ojodigital.com/search.json?q=category%3A{category_v}+after%3A{fecha_inicio-timedelta(days=1)}+before%3A{fecha_fin+timedelta(days=1)}"
        url_ = url_ + "&limit=None"
        
        print(url_)
        
        response = requests.get(url_)
        response_json = response.json()

        if response_json['posts'] == []:
                print("Categoria vacía")  #No Topics in category
        else:
                
                title = f'{category_k} del {fecha_inicio.strftime("%d")}-{fecha_inicio.strftime("%m")}-{fecha_inicio.strftime("%Y")} al {fecha_fin.strftime("%d")}-{fecha_fin.strftime("%m")}-{fecha_fin.strftime("%Y")}'

                raw = 'Estas son las fotografias de esta semana'
              
                response_t = create_topic(title, raw)
               
                               
                input("Press any key")
                for topic in response_json['topics'] :
                        creation_date = datetime.strptime(topic['created_at'][:10],'%Y-%m-%d').date()
                        if creation_date > fecha_inicio and creation_date < fecha_fin:
                                topic_id, topic_title, topic_img= topic['id'],topic['title'], topic['image_url']
                                print(topic_id, topic_title, topic_img, creation_date)
Error 404: 
Traceback (most recent call last):
  File "c:\Pruebaspy\test_url.py", line 55, in <module>
    response_t = create_topic(title, raw)
  File "c:\Pruebaspy\test_url.py", line 17, in create_topic
    return client.create_topic(title,raw,category)
  File "C:\Phyton\lib\site-packages\discourse\client.py", line 96, in create_topic
    response = self._request('POST', 'topics.json', params={
  File "C:\Phyton\lib\site-packages\discourse\client.py", line 31, in _request
    response.raise_for_status()
  File "C:\Phyton\lib\site-packages\requests\models.py", line 941, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://foro.ojodigital.com/topics.json?title=Paisajes+del+02-03-2020+al+08-03-2020&raw=Estas+son+las+fotografias+de+esta+semana&category=33

@Adarrical
Copy link
Author

I've modified my API Key.

If you change your code _response = self.request('POST', 'topics.json', params=... by _response = self.request('POST', 'posts.json', params= program works fine.

thanks in advance.
Albert Darrical.

samamorgan added a commit that referenced this issue Mar 23, 2020
@samamorgan
Copy link
Owner

Thanks for the report! Fixed in the dev branch, will work on merging with a new dot release soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants