Spaces:
Sleeping
Sleeping
feat: add missing linkedin_service.py for Telegram CEO service
Browse files
app/services/linkedin_service.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""LinkedIn Page Posting Service."""
|
| 2 |
+
|
| 3 |
+
import httpx
|
| 4 |
+
from loguru import logger
|
| 5 |
+
from app.config.settings import settings
|
| 6 |
+
|
| 7 |
+
class LinkedInPoster:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.access_token = settings.linkedin_access_token
|
| 10 |
+
self.org_id = settings.linkedin_organization_id or "urn:li:organization:100135805"
|
| 11 |
+
self.endpoint = "https://api.linkedin.com/v2/ugcPosts"
|
| 12 |
+
|
| 13 |
+
async def post_job(self, job_id: str, title: str, company_category: str, location: str, skills: str) -> tuple[str, str]:
|
| 14 |
+
"""
|
| 15 |
+
Posts the masked job announcement to LinkedIn Page.
|
| 16 |
+
"""
|
| 17 |
+
if not self.access_token:
|
| 18 |
+
logger.warning("LinkedIn posting skipped: LINKEDIN_ACCESS_TOKEN is not configured.")
|
| 19 |
+
return "", "LINKEDIN_ACCESS_TOKEN tidak dikonfigurasi"
|
| 20 |
+
|
| 21 |
+
skills_str = ", ".join(skills) if isinstance(skills, list) else skills
|
| 22 |
+
job_url = f"https://briga.id/job?id={job_id}"
|
| 23 |
+
|
| 24 |
+
text = (
|
| 25 |
+
f"🌱 Ada lowongan sebagai {title} di {company_category} area {location}.\n\n"
|
| 26 |
+
f"Skill yang dibutuhkan: {skills_str}\n\n"
|
| 27 |
+
f"Apakah profil kamu sudah siap untuk posisi ini?\n"
|
| 28 |
+
f"🔍 Cek kesiapan & analisa skill gap gratis → {job_url}\n\n"
|
| 29 |
+
f"#GreenJobs #Sustainability #KarirHijau #brigaid"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
payload = {
|
| 33 |
+
"author": self.org_id,
|
| 34 |
+
"lifecycleState": "PUBLISHED",
|
| 35 |
+
"specificContent": {
|
| 36 |
+
"com.linkedin.ugc.ShareContent": {
|
| 37 |
+
"shareCommentary": { "text": text },
|
| 38 |
+
"shareMediaCategory": "ARTICLE",
|
| 39 |
+
"media": [{
|
| 40 |
+
"status": "READY",
|
| 41 |
+
"description": { "text": f"Analisa kecocokan profil Anda secara gratis dengan lowongan {title} di {company_category}." },
|
| 42 |
+
"originalUrl": job_url,
|
| 43 |
+
"title": { "text": f"Analisa Kesiapan Profil: {title}" }
|
| 44 |
+
}]
|
| 45 |
+
}
|
| 46 |
+
},
|
| 47 |
+
"visibility": { "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC" }
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
headers = {
|
| 51 |
+
"Authorization": f"Bearer {self.access_token}",
|
| 52 |
+
"Content-Type": "application/json",
|
| 53 |
+
"X-Restli-Protocol-Version": "2.0.0"
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
async with httpx.AsyncClient(timeout=30.0) as client:
|
| 57 |
+
try:
|
| 58 |
+
response = await client.post(self.endpoint, json=payload, headers=headers)
|
| 59 |
+
if response.status_code in [200, 201]:
|
| 60 |
+
res_json = response.json()
|
| 61 |
+
post_urn = res_json.get("id", "")
|
| 62 |
+
post_url = f"https://www.linkedin.com/feed/update/{post_urn}" if post_urn else ""
|
| 63 |
+
logger.info(f"LinkedIn posting success: URN {post_urn}")
|
| 64 |
+
return post_url, ""
|
| 65 |
+
else:
|
| 66 |
+
err_msg = f"LinkedIn API Error {response.status_code}: {response.text}"
|
| 67 |
+
logger.error(err_msg)
|
| 68 |
+
return "", err_msg
|
| 69 |
+
except Exception as e:
|
| 70 |
+
err_msg = f"Koneksi/Exception Error: {str(e)}"
|
| 71 |
+
logger.error(err_msg)
|
| 72 |
+
return "", err_msg
|