Esempio n. 1
0
def add_user(db_name, table_name="users", message=None):
    if not table_name and not message:
        raise AttributeError(
            "send too function table_name and bot message instance")

    db = DATABASE(db_name)
    read = db.read(table_name=table_name)
    try:
        new_id = read[-1][0] + 1
    except:
        new_id = 0

    uids = get_all_subscribers_id(db_name, table_name)

    data = (new_id, message.chat.id, message.from_user.username,
            message.from_user.first_name, message.from_user.last_name)

    if not str(data[1]) in uids:
        db.insert(table_name=table_name, args=data)
Esempio n. 2
0
def update_DB(db_name, categories, table_name="films", fah=EMPTY_FUNCTION, fhh=EMPTY_FUNCTION):
	try:
		db = DATABASE(db_name)
		tm = d.datetime.now()

		upd = db.read(table_name="updates")
		read = db.read(table_name=table_name)

		try:
			start_index = read[-1][0]+1
		except:
			start_index = 0

		try:
			film_info_index = db.read(table_name="film_info")[-1][0]+1
		except:
			film_info_index = 0

		try:
			id = upd[-1][0]
		except:
			id = 0

		try:
			lmfi = upd[-1][1]
		except:
			lmfi = int(str(tm.year)+str(tm.month)+str(tm.day)+str(tm.hour)+str(tm.minute))

		if lmfi > int(str(tm.year)+str(tm.month)+str(tm.day+UPDATE_TIME)+str(tm.hour)+str(tm.minute)):
			return "db is up to date"

		have_films = [x[1] for x in read]
		cat_keys = list(categories.keys())
		urls = forep("https://armfilm.co/hy/CTG/page/ID/", repl="CTG", count=cat_keys)

		for i, url in enumerate(urls):
			films = load(url=url, repl="ID", count=categories[cat_keys[i]])
			for film in films:
				if not film['name'] in have_films:
					args = [
						start_index,
						film['name'],
						film['image'],
						film['link'],
						film['categori']
					]
					film_info = (
						film_info_index,
						start_index,
						dumps(load_film_info(film['link']))
					)

					db.insert(table_name="film_info", args=film_info)
					db.insert(table_name=table_name, args=args)
					
					start_index+=1
					film_info_index+=1
					have_films.append(film["name"])
					fah(film)
				else:
					fhh(film)

		db.insert("updates", args=(id+1, int(str(tm.year)+str(tm.month)+str(tm.day)+str(tm.hour)+str(tm.minute))))
		
		db.close()
	except Exception as e:
		log(str(e), type="ERROR")