OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Api return empty data response after deploy on replit

  • Thread starter Thread starter captainblack
  • Start date Start date
C

captainblack

Guest
I have a simple telegram bot which fetch data from an api and sends data to telegram as message. Api url works on any browser without any request and it works on postman successfully. My code runs and works truely on replit editor but it returns empty data when I deploy on replit. When I examine the logs, I see that the API returns 200. But the data object is empty. I messed around with headers and even added cookies etc., but no data is being returned. Why could it be?

This is api url which I call : https://www.mexc.com/api/operateactivity/sun_shines/list?kol=false

enter image description here

And this is my codes;

Code:
import requests
import time from telegram 
import Bot
import logging from flask import Flask

logging.basicConfig(level=logging.DEBUG, 
                format='%(asctime)s %(levelname)s %(message)s',
                handlers=[logging.FileHandler("app.log"), logging.StreamHandler()])

bot_token = '****'
chat_id = '****'
bot = Bot(token=bot_token)

headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like 
 Gecko) Chrome/125.0.0.0 Safari/537.36',
'accept': 'application/json',
'accept-language': 'tr,en;q=0.9',
'referer': 'https://www.mexc.com/',
'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive'
}

def fetch_token_symbols():
url = 'https://www.mexc.com/api/operateactivity/sun_shines/list?kol=false'
try:
    logging.debug(f"Request URL: {url}")
    logging.debug(f"Request Headers: {headers}")

    response = requests.get(url, headers=headers)
    response.raise_for_status()  # Raise an error for bad status codes
    logging.debug(f"API Response Status Code: {response.status_code}")
    logging.debug(f"API Response Headers: {response.headers}")

    data = response.json()
    logging.debug(f"API Response Data: {data}")

    if 'data' not in data:
        logging.error("API response does not contain 'data' key")
        return []

    token_symbols = [{
        'profitCurrency': item.get('profitCurrency', ''),
        'profitCurrencyFullName': item.get('profitCurrencyFullName', ''),
        'currencyType': item.get('currencyType', ''),
        'contractAddress': item.get('contractAddress', '')
    } for item in data.get('data', [])]

    return token_symbols
except requests.exceptions.RequestException as e:
    logging.error(f"Request failed: {e}")
    return []
except ValueError as e:
    logging.error(f"Response parsing failed: {e}")
    return []

def send_token_list():
current_tokens = fetch_token_symbols()

if not current_tokens:
    error_message = "API'den geçerli token listesi alınamadı."
    bot.send_message(chat_id=chat_id, text=error_message)
    logging.error(error_message)
    return

messages = [
    f"Token Sembolü: {token['profitCurrency']}\n"
    f"Full Name: {token['profitCurrencyFullName']}\n"
    f"Currency Type: {token['currencyType']}\n"
    f"Contract Address: {token['contractAddress']}"
    for token in current_tokens
]

message = "Güncellenmiş Token Listesi:\n\n" + "\n\n".join(messages)
bot.send_message(chat_id=chat_id, text=message)
logging.info("Güncellenmiş liste gönderildi.")

def start_periodic_task():
  while True:
    send_token_list()
    time.sleep(60)  # 1 minute delay 

app = Flask(__name__)

@app.route('/')
def home():
  return "Application is running!"

if __name__ == "__main__":
  import threading
threading.Thread(target=start_periodic_task, daemon=True).start()
app.run(host='0.0.0.0', port=5000)
<p>I have a simple telegram bot which fetch data from an api and sends data to telegram as message. Api url works on any browser without any request and it works on postman successfully. My code runs and works truely on replit editor but it returns empty data when I deploy on replit. When I examine the logs, I see that the API returns 200. But the data object is empty. I messed around with headers and even added cookies etc., but no data is being returned. Why could it be?</p>
<p>This is api url which I call : <a href="https://www.mexc.com/api/operateactivity/sun_shines/list?kol=false" rel="nofollow noreferrer">https://www.mexc.com/api/operateactivity/sun_shines/list?kol=false</a></p>
<p><a href="https://i.sstatic.net/tCYoNJly.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/tCYoNJly.png" alt="enter image description here" /></a></p>
<p>And this is my codes;</p>
<pre><code>import requests
import time from telegram
import Bot
import logging from flask import Flask

logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(message)s',
handlers=[logging.FileHandler("app.log"), logging.StreamHandler()])

bot_token = '****'
chat_id = '****'
bot = Bot(token=bot_token)

headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like
Gecko) Chrome/125.0.0.0 Safari/537.36',
'accept': 'application/json',
'accept-language': 'tr,en;q=0.9',
'referer': 'https://www.mexc.com/',
'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive'
}

def fetch_token_symbols():
url = 'https://www.mexc.com/api/operateactivity/sun_shines/list?kol=false'
try:
logging.debug(f"Request URL: {url}")
logging.debug(f"Request Headers: {headers}")

response = requests.get(url, headers=headers)
response.raise_for_status() # Raise an error for bad status codes
logging.debug(f"API Response Status Code: {response.status_code}")
logging.debug(f"API Response Headers: {response.headers}")

data = response.json()
logging.debug(f"API Response Data: {data}")

if 'data' not in data:
logging.error("API response does not contain 'data' key")
return []

token_symbols = [{
'profitCurrency': item.get('profitCurrency', ''),
'profitCurrencyFullName': item.get('profitCurrencyFullName', ''),
'currencyType': item.get('currencyType', ''),
'contractAddress': item.get('contractAddress', '')
} for item in data.get('data', [])]

return token_symbols
except requests.exceptions.RequestException as e:
logging.error(f"Request failed: {e}")
return []
except ValueError as e:
logging.error(f"Response parsing failed: {e}")
return []

def send_token_list():
current_tokens = fetch_token_symbols()

if not current_tokens:
error_message = "API'den geçerli token listesi alınamadı."
bot.send_message(chat_id=chat_id, text=error_message)
logging.error(error_message)
return

messages = [
f"Token Sembolü: {token['profitCurrency']}\n"
f"Full Name: {token['profitCurrencyFullName']}\n"
f"Currency Type: {token['currencyType']}\n"
f"Contract Address: {token['contractAddress']}"
for token in current_tokens
]

message = "Güncellenmiş Token Listesi:\n\n" + "\n\n".join(messages)
bot.send_message(chat_id=chat_id, text=message)
logging.info("Güncellenmiş liste gönderildi.")

def start_periodic_task():
while True:
send_token_list()
time.sleep(60) # 1 minute delay

app = Flask(__name__)

@app.route('/')
def home():
return "Application is running!"

if __name__ == "__main__":
import threading
threading.Thread(target=start_periodic_task, daemon=True).start()
app.run(host='0.0.0.0', port=5000)
</code></pre>
 

Latest posts

Top