sonuprasad23 commited on
Commit
e690cc5
·
1 Parent(s): 6d44873

Project Uploaded

Browse files
Files changed (2) hide show
  1. api_server.py +36 -10
  2. final5.py +24 -16
api_server.py CHANGED
@@ -6,20 +6,29 @@ from flask import Flask, request, jsonify, send_from_directory
6
  from flask_cors import CORS
7
  from google_auth_oauthlib.flow import InstalledAppFlow
8
  from google.auth.transport.requests import Request
9
- from googleapiclient.discovery import build
10
  from googleapiclient.errors import HttpError
11
  from dotenv import load_dotenv
12
 
13
  load_dotenv()
14
 
15
- # Decode secrets at startup
 
 
 
 
 
 
 
16
  if 'CREDENTIALS_B64' in os.environ:
17
- with open('credentials.json', 'w') as f:
18
  f.write(base64.b64decode(os.environ['CREDENTIALS_B64']).decode('utf-8'))
19
 
20
  if 'FB_COOKIES_B64' in os.environ:
21
- with open('facebook_cookies.pkl', 'wb') as f:
22
  f.write(base64.b64decode(os.environ['FB_COOKIES_B64']))
 
 
23
 
24
  GROUPS_TXT = os.environ.get("GROUPS_TXT", "groups.txt")
25
  SCRAPE_OUTDIR = os.environ.get("SCRAPE_OUTDIR", "scraped")
@@ -165,18 +174,24 @@ def slugify(url: str) -> str:
165
 
166
  def build_gmail_service():
167
  creds = None
168
- if os.path.exists("token.pickle"):
169
- with open("token.pickle", "rb") as token: creds = pickle.load(token)
 
 
170
  if not creds or not creds.valid:
171
  if creds and creds.expired and creds.refresh_token:
172
  creds.refresh(Request())
173
  else:
174
- if not os.path.exists("credentials.json"):
 
175
  log("credentials.json missing; Gmail unavailable", "warn", "gmail")
176
  return None
177
- flow = InstalledAppFlow.from_client_secrets_file("credentials.json", GMAIL_SCOPES)
 
178
  creds = flow.run_local_server(port=0)
179
- with open("token.pickle", "wb") as token: pickle.dump(creds, token)
 
 
180
  try:
181
  return build("gmail", "v1", credentials=creds)
182
  except Exception as e:
@@ -251,7 +266,18 @@ def stream_process_lines(args: List[str], env: Optional[Dict[str, str]] = None,
251
  return rc
252
 
253
  def call_final5_for_group(group_url: str, out_json: str, analysis_json: str, recipients: List[str]) -> Dict[str, Any]:
254
- args = [PYTHON_BIN, FINAL5_PATH, "--group", group_url, "--out", out_json, "--analysis-out", analysis_json, "--recipients", ",".join(recipients), "--sender", SENDER_EMAIL, "--headless"]
 
 
 
 
 
 
 
 
 
 
 
255
  if GEMINI_KEYS: args.extend(["--gemini-keys", ",".join(GEMINI_KEYS)])
256
  env = os.environ.copy()
257
  env["PYTHONUNBUFFERED"] = "1"
 
6
  from flask_cors import CORS
7
  from google_auth_oauthlib.flow import InstalledAppFlow
8
  from google.auth.transport.requests import Request
9
+ from googleapiclien t.discovery import build
10
  from googleapiclient.errors import HttpError
11
  from dotenv import load_dotenv
12
 
13
  load_dotenv()
14
 
15
+ # --- MODIFICATION START ---
16
+ # Define a writable directory
17
+ WRITABLE_DIR = "/tmp"
18
+ CREDENTIALS_PATH = os.path.join(WRITABLE_DIR, "credentials.json")
19
+ COOKIES_PATH = os.path.join(WRITABLE_DIR, "facebook_cookies.pkl")
20
+ TOKEN_PATH = os.path.join(WRITABLE_DIR, "token.pickle")
21
+
22
+ # Decode secrets at startup into the /tmp directory
23
  if 'CREDENTIALS_B64' in os.environ:
24
+ with open(CREDENTIALS_PATH, 'w') as f:
25
  f.write(base64.b64decode(os.environ['CREDENTIALS_B64']).decode('utf-8'))
26
 
27
  if 'FB_COOKIES_B64' in os.environ:
28
+ with open(COOKIES_PATH, 'wb') as f:
29
  f.write(base64.b64decode(os.environ['FB_COOKIES_B64']))
30
+ # --- MODIFICATION END ---
31
+
32
 
33
  GROUPS_TXT = os.environ.get("GROUPS_TXT", "groups.txt")
34
  SCRAPE_OUTDIR = os.environ.get("SCRAPE_OUTDIR", "scraped")
 
174
 
175
  def build_gmail_service():
176
  creds = None
177
+ # --- MODIFICATION START ---
178
+ if os.path.exists(TOKEN_PATH):
179
+ with open(TOKEN_PATH, "rb") as token: creds = pickle.load(token)
180
+ # --- MODIFICATION END ---
181
  if not creds or not creds.valid:
182
  if creds and creds.expired and creds.refresh_token:
183
  creds.refresh(Request())
184
  else:
185
+ # --- MODIFICATION START ---
186
+ if not os.path.exists(CREDENTIALS_PATH):
187
  log("credentials.json missing; Gmail unavailable", "warn", "gmail")
188
  return None
189
+ flow = InstalledAppFlow.from_client_secrets_file(CREDENTIALS_PATH, GMAIL_SCOPES)
190
+ # --- MODIFICATION END ---
191
  creds = flow.run_local_server(port=0)
192
+ # --- MODIFICATION START ---
193
+ with open(TOKEN_PATH, "wb") as token: pickle.dump(creds, token)
194
+ # --- MODIFICATION END ---
195
  try:
196
  return build("gmail", "v1", credentials=creds)
197
  except Exception as e:
 
266
  return rc
267
 
268
  def call_final5_for_group(group_url: str, out_json: str, analysis_json: str, recipients: List[str]) -> Dict[str, Any]:
269
+ # --- MODIFICATION START ---
270
+ args = [
271
+ PYTHON_BIN, FINAL5_PATH,
272
+ "--group", group_url,
273
+ "--out", out_json,
274
+ "--analysis-out", analysis_json,
275
+ "--recipients", ",".join(recipients),
276
+ "--sender", SENDER_EMAIL,
277
+ "--cookies-file", COOKIES_PATH, # Pass the correct path to the subprocess
278
+ "--headless"
279
+ ]
280
+ # --- MODIFICATION END ---
281
  if GEMINI_KEYS: args.extend(["--gemini-keys", ",".join(GEMINI_KEYS)])
282
  env = os.environ.copy()
283
  env["PYTHONUNBUFFERED"] = "1"
final5.py CHANGED
@@ -22,6 +22,14 @@ from googleapiclient.errors import HttpError
22
  import google.generativeai as genai
23
  from google.api_core.exceptions import ResourceExhausted
24
 
 
 
 
 
 
 
 
 
25
  def get_args():
26
  p = argparse.ArgumentParser(description="Scrape one FB group, analyze, and email alerts.")
27
  p.add_argument("--group", required=True)
@@ -29,7 +37,10 @@ def get_args():
29
  p.add_argument("--analysis-out", required=True)
30
  p.add_argument("--recipients", default="")
31
  p.add_argument("--sender", default=os.environ.get("SENDER_EMAIL", ""))
32
- p.add_argument("--cookies-file", default=os.environ.get("FB_COOKIES_FILE","facebook_cookies.pkl"))
 
 
 
33
  p.add_argument("--max-scrolls", type=int, default=int(os.environ.get("MAX_SCROLLS","5")))
34
  p.add_argument("--scroll-pause", type=float, default=float(os.environ.get("SCROLL_PAUSE","3")))
35
  p.add_argument("--gemini-keys", default="")
@@ -43,10 +54,12 @@ GMAIL_SCOPES = [
43
 
44
  def build_gmail_service():
45
  creds = None
46
- if os.path.exists("token.pickle"):
 
47
  try:
48
- with open("token.pickle", "rb") as token:
49
  creds = pickle.load(token)
 
50
  except Exception:
51
  creds = None
52
  if not creds or not creds.valid:
@@ -56,13 +69,17 @@ def build_gmail_service():
56
  except Exception:
57
  creds = None
58
  if not creds:
59
- if not os.path.exists("credentials.json"):
60
- print("[GMAIL] credentials.json missing; Gmail unavailable")
 
61
  return None
62
- flow = InstalledAppFlow.from_client_secrets_file("credentials.json", GMAIL_SCOPES)
 
63
  creds = flow.run_local_server(port=0)
64
- with open("token.pickle", "wb") as token:
 
65
  pickle.dump(creds, token)
 
66
  try:
67
  svc = build("gmail", "v1", credentials=creds)
68
  _ = svc.users().getProfile(userId="me").execute()
@@ -445,15 +462,6 @@ def main():
445
 
446
  if __name__ == "__main__":
447
  try:
448
- # Decode secrets from environment variables and write to files
449
- if 'CREDENTIALS_B64' in os.environ:
450
- with open('credentials.json', 'w') as f:
451
- f.write(base64.b64decode(os.environ['CREDENTIALS_B64']).decode('utf-8'))
452
-
453
- if 'FB_COOKIES_B64' in os.environ:
454
- with open('facebook_cookies.pkl', 'wb') as f:
455
- f.write(base64.b64decode(os.environ['FB_COOKIES_B64']))
456
-
457
  main()
458
  except Exception as e:
459
  print("Unhandled error:")
 
22
  import google.generativeai as genai
23
  from google.api_core.exceptions import ResourceExhausted
24
 
25
+ # --- MODIFICATION START ---
26
+ # Define the same writable directory
27
+ WRITABLE_DIR = "/tmp"
28
+ CREDENTIALS_PATH = os.path.join(WRITABLE_DIR, "credentials.json")
29
+ TOKEN_PATH = os.path.join(WRITABLE_DIR, "token.pickle")
30
+ # --- MODIFICATION END ---
31
+
32
+
33
  def get_args():
34
  p = argparse.ArgumentParser(description="Scrape one FB group, analyze, and email alerts.")
35
  p.add_argument("--group", required=True)
 
37
  p.add_argument("--analysis-out", required=True)
38
  p.add_argument("--recipients", default="")
39
  p.add_argument("--sender", default=os.environ.get("SENDER_EMAIL", ""))
40
+ # --- MODIFICATION START ---
41
+ # Update the default path for the cookies file
42
+ p.add_argument("--cookies-file", default=os.path.join(WRITABLE_DIR, "facebook_cookies.pkl"))
43
+ # --- MODIFICATION END ---
44
  p.add_argument("--max-scrolls", type=int, default=int(os.environ.get("MAX_SCROLLS","5")))
45
  p.add_argument("--scroll-pause", type=float, default=float(os.environ.get("SCROLL_PAUSE","3")))
46
  p.add_argument("--gemini-keys", default="")
 
54
 
55
  def build_gmail_service():
56
  creds = None
57
+ # --- MODIFICATION START ---
58
+ if os.path.exists(TOKEN_PATH):
59
  try:
60
+ with open(TOKEN_PATH, "rb") as token:
61
  creds = pickle.load(token)
62
+ # --- MODIFICATION END ---
63
  except Exception:
64
  creds = None
65
  if not creds or not creds.valid:
 
69
  except Exception:
70
  creds = None
71
  if not creds:
72
+ # --- MODIFICATION START ---
73
+ if not os.path.exists(CREDENTIALS_PATH):
74
+ print(f"[GMAIL] {CREDENTIALS_PATH} missing; Gmail unavailable")
75
  return None
76
+ flow = InstalledAppFlow.from_client_secrets_file(CREDENTIALS_PATH, GMAIL_SCOPES)
77
+ # --- MODIFICATION END ---
78
  creds = flow.run_local_server(port=0)
79
+ # --- MODIFICATION START ---
80
+ with open(TOKEN_PATH, "wb") as token:
81
  pickle.dump(creds, token)
82
+ # --- MODIFICATION END ---
83
  try:
84
  svc = build("gmail", "v1", credentials=creds)
85
  _ = svc.users().getProfile(userId="me").execute()
 
462
 
463
  if __name__ == "__main__":
464
  try:
 
 
 
 
 
 
 
 
 
465
  main()
466
  except Exception as e:
467
  print("Unhandled error:")