-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
227 lines (192 loc) · 6.62 KB
/
app.py
File metadata and controls
227 lines (192 loc) · 6.62 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
from flask import Flask, request, jsonify
import swisseph as swe
import datetime
import math
import os
from functools import wraps
app = Flask(__name__)
# Try multiple possible paths for ephemeris files
possible_paths = [
os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ephe'),
'/app/ephe',
'./ephe',
os.path.join(os.getcwd(), 'ephe')
]
ephe_path = None
for path in possible_paths:
if os.path.exists(path) and any(fname.endswith('.se1') for fname in os.listdir(path)):
ephe_path = path
break
if ephe_path is None:
print("Error: Could not find ephemeris files in any of these locations:")
for path in possible_paths:
print(f"- {path}")
if os.path.exists(path):
print(f" Contents: {os.listdir(path)}")
else:
print(" Directory does not exist")
else:
print(f"Found ephemeris files in: {ephe_path}")
print(f"Contents: {os.listdir(ephe_path)}")
swe.set_ephe_path(ephe_path or './ephe')
# Verify ephemeris files are accessible
try:
# Test calculation to verify ephemeris files
test_jd = swe.julday(2000, 1, 1, 0)
swe.calc_ut(test_jd, swe.SUN)
except Exception as e:
print(f"Error initializing Swiss Ephemeris: {e}")
print(f"Looking for files in: {ephe_path}")
print(f"Directory contents: {os.listdir(ephe_path) if os.path.exists(ephe_path) else 'Directory not found'}")
# Get API key from environment variable
API_KEY = os.environ.get('API_KEY')
if not API_KEY:
raise ValueError("API_KEY environment variable must be set")
def require_api_key(f):
@wraps(f)
def decorated_function(*args, **kwargs):
api_key = request.headers.get('X-API-Key')
if not api_key or api_key != API_KEY:
return jsonify({"error": "Invalid or missing API key"}), 401
return f(*args, **kwargs)
return decorated_function
PLANETS = {
"Sun": swe.SUN,
"Moon": swe.MOON,
"Mercury": swe.MERCURY,
"Venus": swe.VENUS,
"Mars": swe.MARS,
"Jupiter": swe.JUPITER,
"Saturn": swe.SATURN,
"Uranus": swe.URANUS,
"Neptune": swe.NEPTUNE,
"Pluto": swe.PLUTO,
"North Node": swe.MEAN_NODE,
"Chiron": swe.CHIRON
}
SIGNS = [
"Aries", "Taurus", "Gemini", "Cancer",
"Leo", "Virgo", "Libra", "Scorpio",
"Sagittarius", "Capricorn", "Aquarius", "Pisces"
]
ELEMENTS = {
"Fire": ["Aries", "Leo", "Sagittarius"],
"Earth": ["Taurus", "Virgo", "Capricorn"],
"Air": ["Gemini", "Libra", "Aquarius"],
"Water": ["Cancer", "Scorpio", "Pisces"]
}
MODALITIES = {
"Cardinal": ["Aries", "Cancer", "Libra", "Capricorn"],
"Fixed": ["Taurus", "Leo", "Scorpio", "Aquarius"],
"Mutable": ["Gemini", "Virgo", "Sagittarius", "Pisces"]
}
ASPECTS = {
"Conjunction": 0,
"Sextile": 60,
"Square": 90,
"Trine": 120,
"Opposition": 180
}
def get_sign(degree):
sign_num = int(degree / 30)
degree_in_sign = degree % 30
return SIGNS[sign_num], round(degree_in_sign, 2)
def get_house(degree, houses):
for i in range(len(houses)):
next_house = houses[(i + 1) % 12]
if next_house < houses[i]: # Handle house crossing 0°
if degree >= houses[i] or degree < next_house:
return i + 1
elif houses[i] <= degree < next_house:
return i + 1
return 1
def get_element_modal_dist(positions):
elemental = {"Fire": 0, "Earth": 0, "Water": 0, "Air": 0}
modal = {"Cardinal": 0, "Fixed": 0, "Mutable": 0}
for planet_data in positions.values():
sign = planet_data["sign"]
for element, signs in ELEMENTS.items():
if sign in signs:
elemental[element] += 1
for modality, signs in MODALITIES.items():
if sign in signs:
modal[modality] += 1
return elemental, modal
def normalize_angle(angle):
return angle % 360
def get_aspects(positions):
results = []
planets = list(positions.keys())
for i in range(len(planets)):
for j in range(i + 1, len(planets)):
p1, p2 = planets[i], planets[j]
# Skip aspects with North Node and Chiron
if p1 in ["North Node", "Chiron"] or p2 in ["North Node", "Chiron"]:
continue
lon1 = positions[p1]["degree"] + (SIGNS.index(positions[p1]["sign"]) * 30)
lon2 = positions[p2]["degree"] + (SIGNS.index(positions[p2]["sign"]) * 30)
angle = abs(lon1 - lon2)
angle = min(angle, 360 - angle)
for aspect_name, aspect_angle in ASPECTS.items():
orb = abs(angle - aspect_angle)
if orb <= 6: # 6° orb
results.append({
"type": aspect_name,
"between": [p1, p2],
"orb": round(orb, 2)
})
return results
@app.route("/", methods=["GET"])
@require_api_key
def home():
return "Swiss Ephemeris API is running!"
@app.route("/chart", methods=["POST"])
@require_api_key
def generate_chart():
data = request.json
birth_date = data["birth_date"] # format: YYYY-MM-DD
birth_time = data["birth_time"] # format: HH:MM
place = data["birth_place"]
year, month, day = map(int, birth_date.split("-"))
hour, minute = map(int, birth_time.split(":"))
# Convert to UT
timezone = place["timezone"]
ut_hour = hour - timezone + minute / 60.0
jd_ut = swe.julday(year, month, day, ut_hour)
# Calculate houses first
lat = place["latitude"]
lon = place["longitude"]
houses, ascmc = swe.houses_ex(jd_ut, lat, lon, b'P') # Placidus
ascendant = ascmc[0]
house_cusps = [normalize_angle(h) for h in houses]
# Calculate planet positions with the new format
positions = {}
# Add Ascendant first
asc_sign, asc_degree = get_sign(ascendant)
positions["Ascendant"] = {
"sign": asc_sign,
"degree": asc_degree
}
# Calculate planet positions
for name, pid in PLANETS.items():
result, _ = swe.calc_ut(jd_ut, pid)
lon = normalize_angle(result[0])
sign, degree = get_sign(lon)
house = get_house(lon, house_cusps)
positions[name] = {
"sign": sign,
"degree": degree,
"house": house
}
# Calculate aspects
aspects = get_aspects(positions)
# Calculate distributions
elemental_dist, modal_dist = get_element_modal_dist(positions)
return jsonify({
**positions,
"aspects": aspects,
"elemental_distribution": elemental_dist,
"modal_distribution": modal_dist
})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 5000)))