Skip to content

Commit

Permalink
Using SQL Database
Browse files Browse the repository at this point in the history
  • Loading branch information
viperadnan-git committed Apr 4, 2021
1 parent afb0d6f commit 6e3b549
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pyrogram==1.0.1
feedparser
apscheduler
pickledb
psycopg2-binary
sqlalchemy==1.3.23
18 changes: 8 additions & 10 deletions rss.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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()
21 changes: 21 additions & 0 deletions sql/__init__.py
Original file line number Diff line number Diff line change
@@ -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))
36 changes: 36 additions & 0 deletions sql/db.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 6e3b549

Please sign in to comment.