Athspi commited on
Commit
8a2b2ef
·
verified ·
1 Parent(s): 19902da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -18
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app.py - Flask Backend with Working spoo.me URL Shortening
2
  from flask import Flask, request, jsonify, send_from_directory, make_response
3
  import google.generativeai as genai
4
  from dotenv import load_dotenv
@@ -39,7 +39,7 @@ You are a helpful AI assistant named Athspi. When responding:
39
  genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
40
  model = genai.GenerativeModel('gemini-2.5-flash', system_instruction=system_instruction)
41
 
42
- # In-memory session storage (use Redis in production)
43
  chat_sessions = {}
44
 
45
  def convert_markdown_to_html(text):
@@ -73,31 +73,50 @@ def generate_audio(text):
73
  return filename
74
 
75
  def shorten_url_with_spoo_me(url):
76
- """Shorten URL using spoo.me API - FIXED VERSION"""
77
  try:
 
 
 
 
 
78
  payload = {
79
- "url": url
 
 
 
 
80
  }
81
  headers = {
 
82
  "Content-Type": "application/x-www-form-urlencoded"
83
  }
 
84
  response = requests.post(
85
- "https://spoo.me/",
86
- data=payload,
87
- headers=headers,
88
  timeout=10
89
  )
90
-
91
  if response.status_code == 200:
92
- # spoo.me returns plain text, not JSON!
93
- short_url = response.text.strip()
94
- # Ensure it's a valid URL
95
- if short_url.startswith('http'):
96
- return short_url
97
- return f"https://spoo.me/{short_url}"
98
- else:
99
- print(f"spoo.me error: {response.status_code} - {response.text}")
100
- return None
 
 
 
 
 
 
 
 
101
  except Exception as e:
102
  print(f"Request failed: {str(e)}")
103
  return None
@@ -121,7 +140,7 @@ def chat():
121
  response = chat_session.send_message(user_message)
122
  full_text = response.text
123
 
124
- # Auto-detect any URL and shorten it using spoo.me
125
  url_match = re.search(r'https?://[^\s<>"{}|\\^`\[\]]+', user_message)
126
  if url_match:
127
  original_url = url_match.group(0)
 
1
+ # app.py - Flask Backend with Correct spoo.me API Integration
2
  from flask import Flask, request, jsonify, send_from_directory, make_response
3
  import google.generativeai as genai
4
  from dotenv import load_dotenv
 
39
  genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
40
  model = genai.GenerativeModel('gemini-2.5-flash', system_instruction=system_instruction)
41
 
42
+ # In-memory session storage
43
  chat_sessions = {}
44
 
45
  def convert_markdown_to_html(text):
 
73
  return filename
74
 
75
  def shorten_url_with_spoo_me(url):
76
+ """Shorten URL using spoo.me API with full options"""
77
  try:
78
+ # Clean the URL
79
+ clean_url = url.strip()
80
+ if not clean_url.startswith(('http://', 'https://')):
81
+ clean_url = 'https://' + clean_url
82
+
83
  payload = {
84
+ "url": clean_url,
85
+ "alias": "", # Auto-generate
86
+ "password": "", # No password
87
+ "max-clicks": "", # No limit
88
+ "block-bots": "false" # Don't block bots
89
  }
90
  headers = {
91
+ "Accept": "application/json",
92
  "Content-Type": "application/x-www-form-urlencoded"
93
  }
94
+
95
  response = requests.post(
96
+ "https://spoo.me/",
97
+ data=payload,
98
+ headers=headers,
99
  timeout=10
100
  )
101
+
102
  if response.status_code == 200:
103
+ try:
104
+ # Try JSON first
105
+ result = response.json()
106
+ short_url = result.get("url") or result.get("shorturl")
107
+ if short_url and short_url.startswith('http'):
108
+ return short_url
109
+ except ValueError:
110
+ # If not JSON, treat as plain text
111
+ short_url = response.text.strip()
112
+ if short_url.startswith('http'):
113
+ return short_url
114
+ elif len(short_url) > 1:
115
+ return f"https://spoo.me/{short_url}"
116
+
117
+ print(f"spoo.me error: {response.status_code} - {response.text}")
118
+ return None
119
+
120
  except Exception as e:
121
  print(f"Request failed: {str(e)}")
122
  return None
 
140
  response = chat_session.send_message(user_message)
141
  full_text = response.text
142
 
143
+ # Auto-detect any URL and shorten it
144
  url_match = re.search(r'https?://[^\s<>"{}|\\^`\[\]]+', user_message)
145
  if url_match:
146
  original_url = url_match.group(0)