irahorecka / chrono24

Chrono24 API wrapper
MIT License
12 stars 1 forks source link

Flask App, always get Retrying request and request fails #4

Closed abrarmusa closed 2 weeks ago

abrarmusa commented 3 weeks ago

I have tried hosting the lib as a flask app as:

from flask import Flask, request, jsonify
import chrono24
from chrono24.exceptions import NoListingsFoundException, RequestException

app = Flask(__name__)
REQUIRED_PASSWORD = "TEST"

@app.route('/search', methods=['POST'])
def search():
    # Extract the data from the POST request
    data = request.json
    password = data.get('password', '')
    query = data.get("query", '')

    # Check if the password is provided and matches the required password
    if not password or password != REQUIRED_PASSWORD:
        return jsonify({"error": "Invalid or missing password"}), 403

    # Check if the query is provided
    if not query:
        return jsonify({"error": "Query parameter is missing"}), 400

    # Log the query
    print("Querying: " + query)

    try:
        # Perform the search using chrono24
        search_results = chrono24.query(query)
        listings = []

        for listing in search_results.search_detail():
            listings.append(listing)

        # Return the listings as a JSON response
        return jsonify(listings)

    except NoListingsFoundException:
        # Return an empty array if no listings are found
        return jsonify([])

    except RequestException as e:
        # Return a 500 status code with the error message in case of a request exception
        return jsonify({"error": str(e)}), 500

When the app is run locally, it works great but when i host the app, it always outputs:

Retrying request... Attempt #1. Retrying request... Attempt #2. Retrying request... Attempt #3. Retrying request... Attempt #4. Retrying request... Attempt #5. Retrying request... Attempt #6. Retrying request... Attempt #7. Failed to execute request.

irahorecka commented 3 weeks ago

Thank you for raising this issue. Could you try the following to see if your Chrono24 request continues to fail?

import requests

headers = {
    "accept": "*/*",
    "accept-encoding": "gzip, deflate",
    "accept-language": "pt-PT,pt;q=0.9,en-US;q=0.8,en;q=0.7",
    "cookie": "uid=9c718efe-dcca-4e71-b92d-c3dd7b7f06cc",
    "referer": "https://a3853408329f84107a5d2b90c11d7c4b.safeframe.googlesyndication.com/",
    "sec-ch-ua": '" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"',
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": '"Windows"',
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "user-agent": "Mozilla/5.0 (Windows NT 8.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36",
}
response = requests.get('https://www.chrono24.com/enicar/sherpa--mod2067.htm?dosearch=true&query=Enicar+Sherpa&goal_suggest=1', headers=headers)
response.raise_for_status()
abrarmusa commented 2 weeks ago

it works locally but fails on digital ocean

abrarmusa commented 2 weeks ago
Screenshot 2024-08-27 at 2 23 09 PM
abrarmusa commented 2 weeks ago

when the same request is done from digital ocean:

{
    "error": "403 Client Error: Forbidden for url: https://www.chrono24.com/enicar/sherpa--mod2067.htm?dosearch=true&query=Enicar+Sherpa&goal_suggest=1"
}
irahorecka commented 2 weeks ago

Unfortunately, if the code snippet I provided still causes this issue, it is beyond the scope of this project. If the error is specific to chrono24, feel free to raise this issue again.