Skip to content
This repository has been archived by the owner on Aug 30, 2019. It is now read-only.

Commit

Permalink
Little adjustments to Unlogged behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloromeira committed May 6, 2018
1 parent fa20b04 commit 4a07097
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
9 changes: 9 additions & 0 deletions onegram/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
from .utils import jsearch
from .constants import URLS, GRAPHQL_URL
from .constants import QUERY_HASHES, JSPATHS
from .exceptions import NotSupportedError


@sessionaware
def user_info(session, username=None):
if session.unlogged and not username:
raise NotSupportedError('You must provide an user at Unlogged state')
return _info(session, username=username or session.username)

@sessionaware
Expand All @@ -16,14 +19,20 @@ def post_info(session, post):

@sessionaware
def followers(session, user=None):
if session.unlogged and not user:
raise NotSupportedError('You must provide an user at Unlogged state')
yield from _iter_user(session, user)

@sessionaware
def following(session, user=None):
if session.unlogged and not user:
raise NotSupportedError('You must provide an user at Unlogged state')
yield from _iter_user(session, user)

@sessionaware
def posts(session, user=None):
if session.unlogged and not user:
raise NotSupportedError('You must provide an user at Unlogged state')
yield from _iter_user(session, user)

@sessionaware
Expand Down
27 changes: 14 additions & 13 deletions onegram/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ def current_module_name(self):
def cookies(self):
return self._requests.cookies


@property
def unlogged(self):
return isinstance(self, Unlogged)


def __init__(self, custom_settings={}):
self.settings = _load_settings(custom_settings)

Expand Down Expand Up @@ -149,17 +155,7 @@ def __init__(self, username=None, password=None, custom_settings={}):
super(Login, self).__init__(custom_settings)

self.username = self.settings.get('USERNAME')


def enter_contexts(self):
yield from super(Login, self).enter_contexts()
try:
self._login()
except AuthException as e:
self.logger.error(e)
self.close()
raise e

self.on_open.subscribe(self._login)

def _login(self):
kw = {}
Expand All @@ -176,7 +172,12 @@ def _login(self):

response = self._requests.post(URLS['login'], **kw)
response.raise_for_status()
check_auth(json.loads(response.text))
try:
check_auth(json.loads(response.text))
except AuthException as e:
self.logger.error(e)
self.close()
raise e
self.user_id = self.cookies.get('ds_user_id')


Expand All @@ -194,7 +195,7 @@ def __init__(self, custom_settings={}):

def request(self, *a, **kw):
fn = self.current_function_name
if fn not in supported:
if fn not in Unlogged.supported:
msg = f'"{fn}" is not supported at Unlogged state'
raise NotSupportedError(msg)

Expand Down
9 changes: 7 additions & 2 deletions onegram/utils/ratelimit.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ def __init__(self, session):
if self.persist_enabled:
persist_dir = session.settings.get('RATE_PERSIST_DIR',
Path('.onegram/rates'))
self.persist_path = (Path(persist_dir) /
f'{self.session.username}.json')

if session.unlogged:
filename = '~unlogged.json'
else:
filename = f'{self.session.username}.json'

self.persist_path = Path(persist_dir) / filename
self.load()


Expand Down

0 comments on commit 4a07097

Please sign in to comment.