Spaces:
Sleeping
Sleeping
Add .gitignore for environment variables and enhance app.py for better validation and error handling
Browse files- .gitignore +1 -0
- app.py +39 -7
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
*.env
|
app.py
CHANGED
|
@@ -4,16 +4,34 @@ import json
|
|
| 4 |
import os
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
-
# Load environment variables from .env file
|
| 8 |
load_dotenv()
|
| 9 |
-
organisationNumber = os.getenv("organisationNumber")
|
| 10 |
-
token = os.getenv("token")
|
| 11 |
-
postApiUrl = os.getenv("postApiUrl")
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def post_to_linkedin(name, job_post):
|
| 15 |
"""Post to LinkedIn via their API"""
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
if not name.strip() or not job_post.strip():
|
| 18 |
return "β Please fill in both fields", gr.update(visible=False)
|
| 19 |
|
|
@@ -38,17 +56,27 @@ def post_to_linkedin(name, job_post):
|
|
| 38 |
}
|
| 39 |
|
| 40 |
try:
|
|
|
|
| 41 |
response = requests.post(
|
| 42 |
-
|
| 43 |
headers=headers_dict,
|
| 44 |
-
json=data
|
|
|
|
| 45 |
)
|
| 46 |
|
|
|
|
|
|
|
| 47 |
if response.status_code == 201:
|
| 48 |
return "β
Post published successfully to LinkedIn!", gr.update(visible=True)
|
| 49 |
else:
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
return f"β Error: {str(e)}", gr.update(visible=False)
|
| 54 |
|
|
@@ -57,6 +85,10 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 57 |
gr.Markdown("# π FeedHire LinkedIn Job Poster")
|
| 58 |
gr.Markdown("Post job listings directly to your LinkedIn organization page")
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
with gr.Row():
|
| 61 |
with gr.Column():
|
| 62 |
name_input = gr.Textbox(
|
|
|
|
| 4 |
import os
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
+
# Load environment variables from .env file (for local development)
|
| 8 |
load_dotenv()
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Get environment variables and strip whitespace
|
| 11 |
+
organisationNumber = os.getenv("organisationNumber", "").strip()
|
| 12 |
+
token = os.getenv("token", "").strip()
|
| 13 |
+
postApiUrl = os.getenv("postApiUrl", "").strip()
|
| 14 |
+
|
| 15 |
+
# Validate environment variables on startup
|
| 16 |
+
missing_vars = []
|
| 17 |
+
if not organisationNumber:
|
| 18 |
+
missing_vars.append("organisationNumber")
|
| 19 |
+
if not token:
|
| 20 |
+
missing_vars.append("token")
|
| 21 |
+
if not postApiUrl:
|
| 22 |
+
missing_vars.append("postApiUrl")
|
| 23 |
+
|
| 24 |
+
if missing_vars:
|
| 25 |
+
print(f"β οΈ WARNING: Missing environment variables: {', '.join(missing_vars)}")
|
| 26 |
+
print("Please check your Hugging Face Spaces Secrets configuration")
|
| 27 |
|
| 28 |
def post_to_linkedin(name, job_post):
|
| 29 |
"""Post to LinkedIn via their API"""
|
| 30 |
|
| 31 |
+
# Check for missing environment variables
|
| 32 |
+
if not organisationNumber or not token or not postApiUrl:
|
| 33 |
+
return f"β Configuration Error: Missing environment variables. Please check Spaces Secrets.", gr.update(visible=False)
|
| 34 |
+
|
| 35 |
if not name.strip() or not job_post.strip():
|
| 36 |
return "β Please fill in both fields", gr.update(visible=False)
|
| 37 |
|
|
|
|
| 56 |
}
|
| 57 |
|
| 58 |
try:
|
| 59 |
+
print(f"Posting to: {postApiUrl}") # Debug log
|
| 60 |
response = requests.post(
|
| 61 |
+
postApiUrl,
|
| 62 |
headers=headers_dict,
|
| 63 |
+
json=data,
|
| 64 |
+
timeout=30 # Add timeout
|
| 65 |
)
|
| 66 |
|
| 67 |
+
print(f"Response status: {response.status_code}") # Debug log
|
| 68 |
+
|
| 69 |
if response.status_code == 201:
|
| 70 |
return "β
Post published successfully to LinkedIn!", gr.update(visible=True)
|
| 71 |
else:
|
| 72 |
+
error_msg = f"β Error {response.status_code}: {response.text}"
|
| 73 |
+
print(error_msg) # Debug log
|
| 74 |
+
return error_msg, gr.update(visible=False)
|
| 75 |
|
| 76 |
+
except requests.exceptions.Timeout:
|
| 77 |
+
return "β Error: Request timed out. Please try again.", gr.update(visible=False)
|
| 78 |
+
except requests.exceptions.RequestException as e:
|
| 79 |
+
return f"β Network Error: {str(e)}", gr.update(visible=False)
|
| 80 |
except Exception as e:
|
| 81 |
return f"β Error: {str(e)}", gr.update(visible=False)
|
| 82 |
|
|
|
|
| 85 |
gr.Markdown("# π FeedHire LinkedIn Job Poster")
|
| 86 |
gr.Markdown("Post job listings directly to your LinkedIn organization page")
|
| 87 |
|
| 88 |
+
# Show configuration status
|
| 89 |
+
if missing_vars:
|
| 90 |
+
gr.Markdown(f"### β οΈ Configuration Error\nMissing Secrets: {', '.join(missing_vars)}")
|
| 91 |
+
|
| 92 |
with gr.Row():
|
| 93 |
with gr.Column():
|
| 94 |
name_input = gr.Textbox(
|