From 6e3b549d4fbc626bbcc1bbc8325de56d1294f87b Mon Sep 17 00:00:00 2001 From: viperadnan-git Date: Sun, 4 Apr 2021 13:23:40 +0530 Subject: [PATCH] Using SQL Database --- README.md | 1 + requirements.txt | 3 ++- rss.py | 18 ++++++++---------- sql/__init__.py | 21 +++++++++++++++++++++ sql/db.py | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 11 deletions(-) create mode 100644 sql/__init__.py create mode 100644 sql/db.py diff --git a/README.md b/README.md index bfbfac8..9446516 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ A bot to post messages to Telegram Groups or Channels from rss feed. - `BOT_TOKEN` - Get it by creating a Telegram bot on [BotFather](https://t.me/BotFather) - `FEED_URL` - URL of RSS Feed - `LOG_CHANNEL` - ID of the Telegram Channel where messages are to be posted. +- `DATABASE_URL` - Here is a full [guide](https://github.com/SpEcHiDe/NoPMsBot/wiki/How-to-Install-Database-%3F). For Heroku, just add the `Heroku Postgres` add-on. - `INTERVAL` - Checking Interval in seconds. (optional) - `MAX_INSTANCES` - Max instances to be used while checking rss feed. (optional) diff --git a/requirements.txt b/requirements.txt index 240ba47..aa9c863 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ pyrogram==1.0.1 feedparser apscheduler -pickledb +psycopg2-binary +sqlalchemy==1.3.23 \ No newline at end of file diff --git a/rss.py b/rss.py index 6d95327..83af8fd 100644 --- a/rss.py +++ b/rss.py @@ -1,6 +1,6 @@ import os -import pickledb # You can use any other database too. Use SQL if you are using Heroku Postgres. import feedparser +from sql import db from time import sleep, time from pyrogram import Client, filters from pyrogram.errors import FloodWait @@ -23,22 +23,20 @@ check_interval = int(os.environ.get("INTERVAL", 5)) max_instances = int(os.environ.get("MAX_INSTANCES", 5)) -db = pickledb.load('rss.db', True) -if db.get("feed_url") == None: - db.set("feed_url", "*") -app = Client("rss-bot", api_id=api_id, api_hash=api_hash, bot_token=bot_token) +if db.get_link("feed_url") == None: + db.update_link("feed_url", "*") + +app = Client(":memory:", api_id=api_id, api_hash=api_hash, bot_token=bot_token) def check_feed(): FEED = feedparser.parse(feed_url) entry = FEED.entries[0] - if entry.id != db.get("feed_url"): - + if entry.id != db.get_link("feed_url"): # ↓ Edit this message as your needs. message = f"**{entry.title}**\n```{entry.link}```" - try: app.send_message(log_channel, message) - db.set("feed_url", entry.id) + db.update_link("feed_url", entry.id) except FloodWait as e: print(f"FloodWait: {e.x} seconds") sleep(e.x) @@ -52,4 +50,4 @@ def check_feed(): scheduler = BackgroundScheduler() scheduler.add_job(check_feed, "interval", seconds=check_interval, max_instances=max_instances) scheduler.start() -app.run() +app.run() \ No newline at end of file diff --git a/sql/__init__.py b/sql/__init__.py new file mode 100644 index 0000000..3d7c032 --- /dev/null +++ b/sql/__init__.py @@ -0,0 +1,21 @@ +import os +from sqlalchemy import create_engine +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker, scoped_session + +DATABASE_URL = os.environ.get("DATABASE_URL") + +def start() -> scoped_session: + engine = create_engine(DATABASE_URL) + BASE.metadata.bind = engine + BASE.metadata.create_all(engine) + return scoped_session(sessionmaker(bind=engine, autoflush=False)) + + +try: + BASE = declarative_base() + SESSION = start() +except AttributeError as e: + # this is a dirty way for the work-around required for #23 + print("DATABASE_URL is not configured. Features depending on the database might have issues.") + print(str(e)) \ No newline at end of file diff --git a/sql/db.py b/sql/db.py new file mode 100644 index 0000000..d221d95 --- /dev/null +++ b/sql/db.py @@ -0,0 +1,36 @@ +from sqlalchemy import Column, String, Numeric, Boolean +from sql import SESSION, BASE + +class database(BASE): + __tablename__ = "database" + website = Column(String, primary_key=True) + link = Column(String) + + def __init__(self, website, link): + self.website = website + self.link = link + + +database.__table__.create(checkfirst=True) + + +def get_link(website): + try: + return SESSION.query(database).get(website) + except: + return None + finally: + SESSION.close() + + +def update_link(website, link): + adder = SESSION.query(database).get(website) + if adder: + adder.link = link + else: + adder = database( + website, + link + ) + SESSION.add(adder) + SESSION.commit() \ No newline at end of file