Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
refactor: Improve login and using accounts part
Browse files Browse the repository at this point in the history
* Disable account by default if no login cookies were found
* Move account checking to requires_login decorator

Signed-off-by: yshalsager <ysh-alsager@hotmail.com>
  • Loading branch information
yshalsager committed Feb 7, 2021
1 parent d9b410a commit 211e81e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
3 changes: 2 additions & 1 deletion facebook_rss/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
path: Path = Path(__package__).absolute()
config_path: Path = path.parent
cookies_file_name: str = "session"
local_cookies: Optional[Path] = Path(config_path / cookies_file_name) if Path(config_path / cookies_file_name) else None
local_cookies: Optional[Path] = Path(config_path / cookies_file_name) if Path(
config_path / cookies_file_name).exists() else None
4 changes: 0 additions & 4 deletions facebook_rss/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import asyncio
from sys import exit as exit_

from facebook_rss import local_cookies
from facebook_rss.main import run_api
from facebook_rss.tasks.login import login_and_get_cookies

Expand All @@ -26,7 +25,4 @@
exit_(1)
asyncio.run(login_and_get_cookies(args.email, args.password))
else:
if not local_cookies.exists():
print("Either you have not logged in yet or the provided cookies file doesn't exists!")
exit_(1)
asyncio.run(run_api(development_mode=args.development))
9 changes: 9 additions & 0 deletions facebook_rss/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import uvicorn
from fastapi import FastAPI

from facebook_rss import local_cookies
from facebook_rss.config import get_settings
from facebook_rss.routes.profile import profile_router

api = FastAPI()
api.include_router(profile_router)


@api.on_event("startup")
async def startup_event():
settings = get_settings()
if not local_cookies:
settings.USE_ACCOUNT = False


async def run_api(development_mode=False):
if development_mode:
uvicorn.run("facebook_rss.main:api", host="127.0.0.1", port=8081, reload=True, reload_dirs=['facebook_rss/'])
Expand Down
9 changes: 3 additions & 6 deletions facebook_rss/routes/profile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List

from fastapi import APIRouter, Response, Depends, HTTPException
from fastapi import APIRouter, Response, Depends
from playwright.async_api import Page

from facebook_rss.browser.browser import get_browser
Expand All @@ -10,20 +10,17 @@
from facebook_rss.models.post import Post
from facebook_rss.routes import CommonQueryParams
from facebook_rss.rss.rss_generator import RSSGenerator
from facebook_rss.utils.decorators import cached
from facebook_rss.utils.decorators import cached, requires_login

profile_router = r = APIRouter()


@r.get("/profile/{profile}")
@requires_login
@cached
async def get_profile(profile: str, commons: CommonQueryParams = Depends(),
browser=Depends(get_browser), db=Depends(get_db),
settings: Settings = Depends(get_settings)):
if not settings.USE_ACCOUNT:
raise HTTPException(
status_code=403,
detail="You cannot access Facebook profiles without enabling USE_ACCOUNT option and logged in.")
page: Page = await browser.new_page()
profile_page = await ProfilePage.create(page, profile)
posts: List[Post] = await profile_page.get_posts(full=commons.full, limit=commons.limit)
Expand Down
15 changes: 14 additions & 1 deletion facebook_rss/utils/decorators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from functools import wraps

from fastapi import Response
from fastapi import Response, HTTPException

from facebook_rss.db.curd import get_feed, update_feed, add_feed
from facebook_rss.utils.misc import is_expired_timestamp
Expand All @@ -22,6 +22,19 @@ async def wrapper(*args, **kwargs):
return wrapper


def requires_login(func):
@wraps(func)
async def wrapper(*args, **kwargs):
if not kwargs['settings'].USE_ACCOUNT:
raise HTTPException(
status_code=403,
detail="You cannot access Facebook profiles without enabling USE_ACCOUNT option and logged in.")
value = await func(*args, **kwargs)
return value

return wrapper


class Singleton:
def __init__(self, cls):
self._wrapped = cls
Expand Down

0 comments on commit 211e81e

Please sign in to comment.