From 43ce535b4ff3abe762da0a1095777f542c23aa8f Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sun, 5 Jan 2020 20:51:31 -0800 Subject: [PATCH] Add argument --offline to redmash, points to offline_reading files. Previously, the files generated by offline_reading were difficult to use because there was no index file and the filenames are just ids. So now, the redmash can act as an index! --- timesearch/__init__.py | 7 +++++++ timesearch/redmash.py | 19 +++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/timesearch/__init__.py b/timesearch/__init__.py index 1794878..ee39eb2 100644 --- a/timesearch/__init__.py +++ b/timesearch/__init__.py @@ -185,6 +185,12 @@ redmash: --html: Write HTML files instead of plain text. + --offline: + The links in the mash will point to the files generated by + offline_reading. That is, `../offline_reading/fullname.html` instead + of `http://redd.it/id`. This will NOT trigger offline_reading to + generate the files now, so you must run that tool separately. + -st 50 | --score_threshold 50: Only mash posts with at least this many points. Applies to ALL mashes! @@ -382,6 +388,7 @@ p_redmash.add_argument('--html', dest='html', action='store_true') p_redmash.add_argument('--score', dest='do_score', action='store_true') p_redmash.add_argument('--sub', dest='do_subreddit', action='store_true') p_redmash.add_argument('--title', dest='do_title', action='store_true') +p_redmash.add_argument('--offline', dest='offline', action='store_true') p_redmash.add_argument('-r', '--subreddit', dest='subreddit', default=None) p_redmash.add_argument('-st', '--score_threshold', dest='score_threshold', default=0) p_redmash.add_argument('-u', '--user', dest='username', default=None) diff --git a/timesearch/redmash.py b/timesearch/redmash.py index f448769..b7d1ec1 100644 --- a/timesearch/redmash.py +++ b/timesearch/redmash.py @@ -15,7 +15,7 @@ LINE_FORMAT_HTML = ''' '''.replace('\n', '') TIMESTAMP_FORMAT = '%Y %b %d' -#The time format. +# The time format. # "%Y %b %d" = "2016 August 10" # See http://strftime.org/ @@ -51,6 +51,7 @@ def redmash( do_subreddit=False, do_flair=False, html=False, + offline=False, score_threshold=0, ): if not common.is_xor(subreddit, username): @@ -61,7 +62,7 @@ def redmash( else: database = tsdb.TSDB.for_user(username, do_create=False) - kwargs = {'html': html, 'score_threshold': score_threshold} + kwargs = {'html': html, 'offline': offline, 'score_threshold': score_threshold} wrote = None if do_all or do_date: @@ -100,6 +101,7 @@ def redmash_worker( orderby, score_threshold=0, html=False, + offline=False, ): cur = database.sql.cursor() statement = 'SELECT * FROM submissions WHERE score >= {threshold} ORDER BY {order}' @@ -132,8 +134,12 @@ def redmash_worker( else: timestamp = '' - short_link = 'https://redd.it/%s' % item.idstr[3:] - author = item.author + if offline: + link = f'../offline_reading/{submission.idstr}.html' + else: + link = f'https://redd.it/{submission.idstr[3:]}' + + author = submission.author if author.lower() == '[deleted]': author_link = '#' else: @@ -147,11 +153,11 @@ def redmash_worker( id=submission.idstr, numcomments=submission.num_comments, score=submission.score, - link=short_link, + link=link, subreddit=submission.subreddit, timestamp=timestamp, title=submission.title.replace('\n', ' '), - url=submission.url or short_link, + url=submission.url or link, ) line += '\n' mash_handle.write(line) @@ -174,5 +180,6 @@ def redmash_argparse(args): do_subreddit=args.do_subreddit, do_flair=args.do_flair, html=args.html, + offline=args.offline, score_threshold=common.int_none(args.score_threshold), )