# MonitorIndeed.py
import psycopg2
import pytz
from parserIndeed import getIndeedVacancies, parseIndeedVacancies
import time
from datetime import datetime, timedelta
import http.client
import json
from sqlinsert import insertVacancy, checkVacancy, updateVacancy
import sys


def main(host="localhost", database="easydb", user="postgres"):

    if None in [host, database, user]:  # if some configs are missing  throw exception

        raise Exception("Check script arguments or run without them")

    SQL_connection = psycopg2.connect(
        host=host,
        database=database,
        user=user,
    )
    SQL_connection.autocommit = True
    cur = SQL_connection.cursor()
    for vacancy in parseIndeedVacancies(getIndeedVacancies()):
        if checkVacancy(cur, vacancy):
            updateVacancy(cur, vacancy)
        else:
            insertVacancy(cur, vacancy)


if __name__ == "__main__":
    if len(sys.argv) == 1:
        main()
    else:
        main(sys.argv[1], sys.argv[2], sys.argv[3])

