implemented hsitory logging accross sessions witha history.log file
remove unnecessary variable return an empty set if the file doesn't exist xrange is range in python3
This commit is contained in:
37
rtv/history.py
Normal file
37
rtv/history.py
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = ['load_history', 'save_history']
|
||||||
|
|
||||||
|
|
||||||
|
def history_path():
|
||||||
|
"""
|
||||||
|
Create the path to the history log
|
||||||
|
"""
|
||||||
|
HOME = os.path.expanduser('~')
|
||||||
|
XDG_CONFIG_HOME = os.getenv('XDG_CACHE_HOME', os.path.join(HOME, '.config'))
|
||||||
|
path = os.path.join(XDG_CONFIG_HOME, 'rtv', 'history.log')
|
||||||
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
def load_history():
|
||||||
|
"""
|
||||||
|
Load the history file into memory if it exists
|
||||||
|
"""
|
||||||
|
path = history_path()
|
||||||
|
if os.path.exists(path):
|
||||||
|
with open(path) as history_file:
|
||||||
|
return set([line.replace('\n', '') for line in history_file])
|
||||||
|
return set()
|
||||||
|
|
||||||
|
|
||||||
|
def save_history(history):
|
||||||
|
"""
|
||||||
|
Save the visited links to the history log
|
||||||
|
"""
|
||||||
|
path = history_path()
|
||||||
|
with open(path, 'w+') as history_file:
|
||||||
|
for i in range(200):
|
||||||
|
if not history:
|
||||||
|
break
|
||||||
|
history_file.write(history.pop() + '\n')
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import curses
|
import curses
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
|
import atexit
|
||||||
import requests
|
import requests
|
||||||
import praw
|
import praw
|
||||||
|
|
||||||
@@ -10,16 +10,22 @@ from .page import BasePage, Navigator, BaseController
|
|||||||
from .submission import SubmissionPage
|
from .submission import SubmissionPage
|
||||||
from .content import SubredditContent
|
from .content import SubredditContent
|
||||||
from .helpers import clean, open_browser, open_editor
|
from .helpers import clean, open_browser, open_editor
|
||||||
|
from .history import load_history, save_history
|
||||||
from .docs import SUBMISSION_FILE
|
from .docs import SUBMISSION_FILE
|
||||||
from .curses_helpers import (BULLET, UARROW, DARROW, GOLD, Color,
|
from .curses_helpers import (BULLET, UARROW, DARROW, GOLD, Color,
|
||||||
LoadScreen, show_notification, prompt_input)
|
LoadScreen, show_notification, prompt_input)
|
||||||
|
|
||||||
__all__ = ['opened_links', 'SubredditController', 'SubredditPage']
|
__all__ = ['history', 'SubredditController', 'SubredditPage']
|
||||||
|
|
||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
# Used to keep track of browsing history across the current session
|
history = load_history()
|
||||||
opened_links = set()
|
|
||||||
|
|
||||||
|
@atexit.register
|
||||||
|
def save_links():
|
||||||
|
global history
|
||||||
|
save_history(history)
|
||||||
|
|
||||||
|
|
||||||
class SubredditController(BaseController):
|
class SubredditController(BaseController):
|
||||||
@@ -96,8 +102,8 @@ class SubredditPage(BasePage):
|
|||||||
page.loop()
|
page.loop()
|
||||||
|
|
||||||
if data['url'] == 'selfpost':
|
if data['url'] == 'selfpost':
|
||||||
global opened_links
|
global history
|
||||||
opened_links.add(data['url_full'])
|
history.add(data['url_full'])
|
||||||
|
|
||||||
@SubredditController.register(curses.KEY_ENTER, 10, 'o')
|
@SubredditController.register(curses.KEY_ENTER, 10, 'o')
|
||||||
def open_link(self):
|
def open_link(self):
|
||||||
@@ -106,8 +112,8 @@ class SubredditPage(BasePage):
|
|||||||
url = self.content.get(self.nav.absolute_index)['url_full']
|
url = self.content.get(self.nav.absolute_index)['url_full']
|
||||||
open_browser(url)
|
open_browser(url)
|
||||||
|
|
||||||
global opened_links
|
global history
|
||||||
opened_links.add(url)
|
history.add(url)
|
||||||
|
|
||||||
@SubredditController.register('p')
|
@SubredditController.register('p')
|
||||||
def post_submission(self):
|
def post_submission(self):
|
||||||
@@ -168,7 +174,7 @@ class SubredditPage(BasePage):
|
|||||||
|
|
||||||
row = n_title + offset
|
row = n_title + offset
|
||||||
if row in valid_rows:
|
if row in valid_rows:
|
||||||
seen = (data['url_full'] in opened_links)
|
seen = (data['url_full'] in history)
|
||||||
link_color = Color.MAGENTA if seen else Color.BLUE
|
link_color = Color.MAGENTA if seen else Color.BLUE
|
||||||
attr = curses.A_UNDERLINE | link_color
|
attr = curses.A_UNDERLINE | link_color
|
||||||
text = clean(u'{url}'.format(**data))
|
text = clean(u'{url}'.format(**data))
|
||||||
|
|||||||
Reference in New Issue
Block a user