Spaces:
Sleeping
Sleeping
File size: 4,413 Bytes
407622b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | """
Web API Integration for Homeopathic Information
Using free public APIs and web scraping
"""
import requests
import json
from typing import Dict, Optional
import re
class WebMedicineSearch:
def __init__(self):
self.base_urls = {
"homeopathy_center": "https://homeopathycenter.org/homeopathic-remedies/",
"abchomeopathy": "http://www.abchomeopathy.com/r.php/",
"simillimum": "https://www.simillimum.com/education/the-lectures/materia-medica/"
}
def search_web(self, remedy_name: str) -> Dict:
"""
Search for remedy information from web sources
Returns combined information
"""
results = {
"name": remedy_name,
"sources": [],
"information": {},
"success": False
}
try:
# Try different sources
info = self._get_from_homeopathy_center(remedy_name)
if info:
results["sources"].append("homeopathy_center")
results["information"].update(info)
info = self._get_from_abchomeopathy(remedy_name)
if info:
results["sources"].append("abchomeopathy")
results["information"].update(info)
results["success"] = len(results["sources"]) > 0
except Exception as e:
results["error"] = str(e)
return results
def _get_from_homeopathy_center(self, remedy_name: str) -> Optional[Dict]:
"""Get information from homeopathycenter.org"""
try:
# Format URL
formatted_name = remedy_name.lower().replace(" ", "-")
url = f"{self.base_urls['homeopathy_center']}{formatted_name}"
# In a real implementation, this would make an HTTP request
# For now, return mock data
return {
"source": "Homeopathy Center",
"url": url,
"summary": f"Information about {remedy_name} from Homeopathy Center",
"key_points": [
"Common uses and indications",
"Modalities and symptoms",
"Potency recommendations"
]
}
except:
return None
def _get_from_abchomeopathy(self, remedy_name: str) -> Optional[Dict]:
"""Get information from ABC Homeopathy"""
try:
# Format for ABC Homeopathy
formatted_name = remedy_name.replace(" ", "+")
url = f"{self.base_urls['abchomeopathy']}{formatted_name}"
return {
"source": "ABC Homeopathy",
"url": url,
"summary": f"Remedy information from ABC Homeopathy",
"remedy_finder_link": url
}
except:
return None
def hybrid_search(self, query: str, use_internal: bool = True) -> Dict:
"""
Hybrid search combining internal DB and web
"""
results = {
"query": query,
"internal_results": [],
"web_results": [],
"combined_results": []
}
# Get internal results
if use_internal:
from medicine_searcher import searcher
internal = searcher.search_medicine(query, "internal")
results["internal_results"] = internal.get("results", [])
# Get web results
web = self.search_web(query)
if web.get("success"):
results["web_results"] = [web]
# Combine results
combined = []
# Add internal first
for item in results["internal_results"][:3]:
item["source"] = "Internal Database"
combined.append(item)
# Add web if available
if results["web_results"]:
combined.append({
"name": query,
"source": "Web Search",
"description": "Additional information available online",
"information": results["web_results"][0]["information"]
})
results["combined_results"] = combined
results["success"] = len(combined) > 0
return results
# Global instance
web_searcher = WebMedicineSearch() |