neddit

the frontend of the frontpage of the internet  https://neddit.ce9e.org
git clone https://git.ce9e.org/neddit.git

commit
c4b185644129efe85d53883d19f49cec47d8cede
parent
b835252fd9ed70233c601931a9c4c365cf7b64c8
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2023-07-15 05:42
tabs to spaces

Diffstat

M neddit.py 110 ++++++++++++++++++++++++++++++------------------------------

1 files changed, 55 insertions, 55 deletions


diff --git a/neddit.py b/neddit.py

@@ -10,97 +10,97 @@ from flask import abort
   10    10 
   11    11 app = Flask(__name__)
   12    12 env = jinja2.Environment(
   13    -1 	loader=jinja2.FileSystemLoader('templates'),
   14    -1 	autoescape=jinja2.select_autoescape(default=True),
   -1    13     loader=jinja2.FileSystemLoader('templates'),
   -1    14     autoescape=jinja2.select_autoescape(default=True),
   15    15 )
   16    16 
   17    17 
   18    18 def relative_datetime(value):
   19    -1 	dt = datetime.fromtimestamp(value)
   20    -1 	delta = datetime.now() - dt
   21    -1 	if delta.days > 365:
   22    -1 		return '%i years ago' % (delta.days // 365)
   23    -1 	elif delta.days > 0:
   24    -1 		return '%i days ago' % delta.days
   25    -1 	elif delta.seconds > 3600:
   26    -1 		return '%i hours ago' % (delta.seconds // 3600)
   27    -1 	elif delta.seconds > 60:
   28    -1 		return '%i minutes ago' % (delta.seconds // 60)
   29    -1 	else:
   30    -1 		return '%i seconds ago' % delta.seconds
   -1    19     dt = datetime.fromtimestamp(value)
   -1    20     delta = datetime.now() - dt
   -1    21     if delta.days > 365:
   -1    22         return '%i years ago' % (delta.days // 365)
   -1    23     elif delta.days > 0:
   -1    24         return '%i days ago' % delta.days
   -1    25     elif delta.seconds > 3600:
   -1    26         return '%i hours ago' % (delta.seconds // 3600)
   -1    27     elif delta.seconds > 60:
   -1    28         return '%i minutes ago' % (delta.seconds // 60)
   -1    29     else:
   -1    30         return '%i seconds ago' % delta.seconds
   31    31 env.filters['dt'] = relative_datetime
   32    32 
   33    33 
   34    34 def format_number(value):
   35    -1 	if value > 100000000:
   36    -1 		return '%im' % (value // 1000000)
   37    -1 	elif value > 1000000:
   38    -1 		return '%.1fm' % (value / 1000000)
   39    -1 	if value > 100000:
   40    -1 		return '%ik' % (value // 1000)
   41    -1 	elif value > 1000:
   42    -1 		return '%.1fk' % (value / 1000)
   43    -1 	else:
   44    -1 		return str(value)
   -1    35     if value > 100000000:
   -1    36         return '%im' % (value // 1000000)
   -1    37     elif value > 1000000:
   -1    38         return '%.1fm' % (value / 1000000)
   -1    39     if value > 100000:
   -1    40         return '%ik' % (value // 1000)
   -1    41     elif value > 1000:
   -1    42         return '%.1fk' % (value / 1000)
   -1    43     else:
   -1    44         return str(value)
   45    45 env.filters['k'] = format_number
   46    46 
   47    47 
   48    48 def normalize_link(value):
   49    -1 	if value.startswith('https://www.reddit.com'):
   50    -1 		value = value[22:]
   51    -1 	return value
   -1    49     if value.startswith('https://www.reddit.com'):
   -1    50         value = value[22:]
   -1    51     return value
   52    52 env.filters['link'] = normalize_link
   53    53 
   54    54 
   55    55 def dash_resize(url):
   56    -1 	return re.sub('DASH_[0-9]*', 'DASH_240', url)
   -1    56     return re.sub('DASH_[0-9]*', 'DASH_240', url)
   57    57 env.filters['dash_resize'] = dash_resize
   58    58 
   59    59 
   60    60 def fetch(url, **params):
   61    -1 	app.logger.debug('fetch %s' % url)
   62    -1 	r = requests.get(
   63    -1 		url,
   64    -1 		headers={'User-agent': 'neddit'},
   65    -1 		params={**params, 'raw_json': 1},
   66    -1 	)
   67    -1 	if r.status_code != 200:
   68    -1 		abort(r.status_code)
   69    -1 	return r.json()
   -1    61     app.logger.debug('fetch %s' % url)
   -1    62     r = requests.get(
   -1    63         url,
   -1    64         headers={'User-agent': 'neddit'},
   -1    65         params={**params, 'raw_json': 1},
   -1    66     )
   -1    67     if r.status_code != 200:
   -1    68         abort(r.status_code)
   -1    69     return r.json()
   70    70 
   71    71 
   72    72 @functools.lru_cache()
   73    73 def fetch_subreddit(name):
   74    -1 	return fetch('https://www.reddit.com/r/%s/about.json' % name)
   -1    74     return fetch('https://www.reddit.com/r/%s/about.json' % name)
   75    75 
   76    76 
   77    77 @app.route('/<path:path>/')
   78    78 def generic(path):
   79    -1 	context = {
   80    -1 		'params': request.args,
   81    -1 	}
   -1    79     context = {
   -1    80         'params': request.args,
   -1    81     }
   82    82 
   83    -1 	if path.startswith('r/'):
   84    -1 		subreddit = path.split('/')[1]
   85    -1 		context['subreddit'] = fetch_subreddit(subreddit)
   -1    83     if path.startswith('r/'):
   -1    84         subreddit = path.split('/')[1]
   -1    85         context['subreddit'] = fetch_subreddit(subreddit)
   86    86 
   87    -1 	a = fetch('https://www.reddit.com/%s.json' % path, **request.args)
   88    -1 	if isinstance(a, list):
   89    -1 		tpl = env.get_template('comments.html')
   90    -1 		context['post'] = a[0]['data']['children'][0]
   91    -1 		context['comments'] = a[1]
   92    -1 	else:
   93    -1 		tpl = env.get_template('listing.html')
   94    -1 		context.update(a)
   -1    87     a = fetch('https://www.reddit.com/%s.json' % path, **request.args)
   -1    88     if isinstance(a, list):
   -1    89         tpl = env.get_template('comments.html')
   -1    90         context['post'] = a[0]['data']['children'][0]
   -1    91         context['comments'] = a[1]
   -1    92     else:
   -1    93         tpl = env.get_template('listing.html')
   -1    94         context.update(a)
   95    95 
   96    -1 	return tpl.render(**context)
   -1    96     return tpl.render(**context)
   97    97 
   98    98 
   99    99 @app.route('/')
  100   100 def root():
  101    -1 	return generic('hot')
   -1   101     return generic('hot')
  102   102 
  103   103 
  104   104 @app.route('/favicon.ico')
  105   105 def favicon():
  106    -1 	return '', 404
   -1   106     return '', 404