#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import urllib.request
import urllib.parse
import json
import datetime
import re
import ssl

REMOTEOK_API_URL = "https://remoteok.com/api"
POST_ENDPOINT = "https://digvera.com/clientradar/save_project.php"

def clean_description(text):
    if not text:
        return ""
    text = re.sub(r"<[^>]+>", " ", text)
    text = re.sub(r"&[a-zA-Z]+;", " ", text)
    text = re.sub(r"http[s]?://\S+", " ", text)
    text = re.sub(r"\s+", " ", text).strip()
    return text[:250]

def extract_budget(text):
    match = re.search(r"\$\s?([\d,]+(?:\.\d{1,2})?)", text)
    if match:
        return "$" + match.group(1)
    return "Not specified"

def fetch_remoteok_jobs():
    try:
        context = ssl._create_unverified_context()
        with urllib.request.urlopen(REMOTEOK_API_URL, context=context) as response:
            data = json.loads(response.read().decode())

        if not data or not isinstance(data, list):
            return []

        jobs = []
        now = datetime.datetime.utcnow()
        for item in data[1:]:  # skip metadata in [0]
            date_str = item.get("date", "") or item.get("created_at", "")
            if not date_str:
                continue
            try:
                post_date = datetime.datetime.strptime(date_str[:10], "%Y-%m-%d")
                if (now - post_date).days > 1:
                    continue
            except:
                continue

            title = item.get("position", "No Title")
            link = item.get("url", "")
            raw_desc = item.get("description", "")
            description = clean_description(raw_desc)
            budget = extract_budget(description)

            job = {
                "title": title.strip(),
                "description": description,
                "budget": budget,
                "link": link.strip()
            }

            jobs.append(job)
        return jobs
    except Exception as e:
        log_error(f"Error fetching RemoteOK: {e}")
        return []

def send_to_server(job):
    try:
        data = urllib.parse.urlencode(job).encode("utf-8")
        req = urllib.request.Request(POST_ENDPOINT, data=data)
        with urllib.request.urlopen(req) as response:
            result = response.read().decode("utf-8")
        return result
    except Exception as e:
        log_error(f"Error sending job: {e}")
        return None

def log_error(message):
    with open("/home/digvputi/public_html/clientradar/error_log", "a") as log:
        log.write(f"[{datetime.datetime.now()}] {message}\n")

def main():
    jobs = fetch_remoteok_jobs()
    for job in jobs:
        send_to_server(job)

if __name__ == "__main__":
    main()
