Skip to content

Instantly share code, notes, and snippets.

@JonasBa
Created January 23, 2019 08:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonasBa/8ebed8b9e69bd1e8366577af7dcf8f2c to your computer and use it in GitHub Desktop.
Save JonasBa/8ebed8b9e69bd1e8366577af7dcf8f2c to your computer and use it in GitHub Desktop.
AlgoliaHN - Reddit ranking script
from algoliasearch import algoliasearch
from datetime import datetime, timedelta
from math import log
# reddit hot score computing tools, courtesy of Mika S!
epoch = datetime(1970, 1, 1)
oldest_article = 1160418111
def bucket_date(date):
res = date - timedelta(days=date.weekday())
return res.replace(hour=0, minute=0, second=0, microsecond=0)
def epoch_seconds(date):
td = date - epoch
return td.days * 86400 + td.seconds + (float(td.microseconds) / 1000000)
def hot(score, date):
try:
order = log(score, 10)
except:
order = 0
seconds = epoch_seconds(bucket_date(date)) - oldest_article
return round(order + seconds / 45000, 7)
# Algolia tools
client = algoliasearch.Client('UJ5WYC0L7X', 'd693b2435c0d9429ce25e4fbd4ec6d41')
index = client.init_index('Item_production')
client2 = algoliasearch.Client('RSBCBF0EG8', '521ea9434ec24fec6767b6f40e8e8356')
index2 = client2.init_index('HN_Item_production')
to_transfer = []
total = 0
batch_size = 10000
# and there we go
res = index.browse_all({'query': '', 'attributesToRetrieve': ['*']})
for hit in res:
hit['hot'] = hot(hit.get('points', 1), datetime.fromtimestamp(hit['created_at_i']))
to_transfer.append(hit)
if len(to_transfer) % batch_size == 0:
index2.partial_update_objects(to_transfer)
total += batch_size
print('Updated %s products (total: %s)' % (batch_size, total))
to_transfer = []
if len(to_transfer) > 0:
index2.partial_update_objects(to_transfer)
total += len(to_transfer)
print('Updated %s products (total: %s)' % (len(to_transfer), total))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment