Пример #1
0
def delete_subscriber(chat, db_name, table_name="users"):
	if not chat:
		raise AttributeError("params id and chat is none")

	db = DATABASE(db_name)
	db.delete(table_name=table_name, where=" chatid = {}".format(chat))
	db.close()
	return True
Пример #2
0
def load_films(db_name,
               table_name="films",
               only_load=False,
               count=10,
               find=None,
               find_perc=None,
               random=False,
               category=None):
    try:
        db = DATABASE(db_name)

        read = db.read(table_name=table_name)
        try:
            len_db = read[-1][0]
        except:
            len_db = 0

        have_films = [(x[1], x[0]) for x in read]
        cats = split_by_category(read)

        if random:
            if category:
                return sample(cats[category], count)
            else:
                return sample(have_films, count)

        if only_load:
            if len_db > (only_load + count) and len_db > only_load:
                return have_films[only_load:only_load + count]
            elif len_db > only_load:
                return have_films[only_load:None]

        if find:
            return db.read(table_name=table_name,
                           where="name LIKE '%{}%'".format(find))

        if find_perc:
            finded = []
            for film in read:
                if ratio(find_perc[0], film[1]) > int(find_perc[1]):
                    if not category:
                        finded.append(film)
                        continue
                    elif category == film[-1]:
                        finded.append(film)

            return finded

        db.close()

        return have_films[0:count]

    except Exception as e:
        log(str(e), type="ERROR")
Пример #3
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")
Пример #4
0
def get_all_subscribers_id(db_name, table_name="users"):
	db = DATABASE(db_name)
	users = db.read(table_name=table_name)	
	db.close()
	return tuple([user[1] for user in users])
Пример #5
0
def get_all_subscribers(db_name, table_name="users"):
	db = DATABASE(db_name)
	users = db.read(table_name=table_name)
	db.close()
	return users
Пример #6
0
def user_exist(db_name, table_name="users", chat_id=None):
	db = DATABASE(db_name)
	user = db.read(table_name=table_name, where=" chat_id = '{}'".format(chat_id))[0]
	db.close()
	return (len(user) > 0)