Akwbw commited on
Commit
bcc3a55
·
verified ·
1 Parent(s): a990a46

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ import requests
3
+ from bs4 import BeautifulSoup
4
+ import uvicorn
5
+
6
+ app = FastAPI(title="Unlimited Temp SMS API")
7
+
8
+ # --- CORE LOGIC (Scraper) ---
9
+
10
+ def scrape_receive_sms_free():
11
+ # Source: receive-sms-free.cc
12
+ url = "https://receive-sms-free.cc"
13
+ headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
14
+
15
+ try:
16
+ response = requests.get(url, headers=headers, timeout=10)
17
+ soup = BeautifulSoup(response.text, 'html.parser')
18
+
19
+ numbers = []
20
+ # Finding numbers on homepage
21
+ for link in soup.find_all('a'):
22
+ href = link.get('href')
23
+ text = link.get_text().strip()
24
+
25
+ if href and href.startswith("/Receive-SMS-") and text.startswith("+"):
26
+ full_link = url + href
27
+ country = "US" # Defaulting for homepage generic
28
+ if "UK" in href: country = "UK"
29
+ if "India" in href: country = "IN"
30
+
31
+ numbers.append({
32
+ "country": country,
33
+ "number": text,
34
+ "link": full_link,
35
+ "origin": "receive-sms-free.cc"
36
+ })
37
+ return numbers
38
+ except Exception as e:
39
+ print(f"Error scraping: {e}")
40
+ return []
41
+
42
+ def get_sms_messages(link):
43
+ headers = {'User-Agent': 'Mozilla/5.0'}
44
+ try:
45
+ response = requests.get(link, headers=headers, timeout=10)
46
+ soup = BeautifulSoup(response.text, 'html.parser')
47
+
48
+ messages = []
49
+ # Specific parsing for receive-sms-free.cc
50
+ rows = soup.find_all('div', class_='row')
51
+ for row in rows:
52
+ # Simple heuristic checking
53
+ text_content = row.get_text().strip()
54
+ if "min" in text_content or "sec" in text_content:
55
+ parts = list(filter(None, text_content.split('\n')))
56
+ if len(parts) >= 2:
57
+ messages.append({
58
+ "sender": parts[0].strip(),
59
+ "message": parts[-1].strip()
60
+ })
61
+ return messages
62
+ except:
63
+ return [{"sender": "System", "message": "Could not fetch messages. Try again."}]
64
+
65
+ # --- API ENDPOINTS ---
66
+
67
+ @app.get("/")
68
+ def home():
69
+ return {"status": "Online", "msg": "Use /countries or /numbers to get data"}
70
+
71
+ @app.get("/countries")
72
+ def get_countries():
73
+ # Listing generic supported countries
74
+ return ["US", "UK", "IN", "CA", "FR"]
75
+
76
+ @app.get("/numbers")
77
+ def get_all_numbers():
78
+ data = scrape_receive_sms_free()
79
+ if not data:
80
+ return {"error": "Source site is slow, try refreshing"}
81
+ return data
82
+
83
+ @app.get("/numbers/{country_code}")
84
+ def get_country_numbers(country_code: str):
85
+ all_nums = scrape_receive_sms_free()
86
+ filtered = [n for n in all_nums if n['country'] == country_code.upper()]
87
+ return filtered
88
+
89
+ @app.get("/messages")
90
+ def check_messages(number: str):
91
+ # User needs to provide the full number, we search for its link
92
+ all_nums = scrape_receive_sms_free()
93
+ target = next((item for item in all_nums if item["number"] == number), None)
94
+
95
+ if target:
96
+ msgs = get_sms_messages(target['link'])
97
+ return {"number": number, "messages": msgs}
98
+ else:
99
+ return {"error": "Number not found in active list"}