惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
IT之家
IT之家
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
V
V2EX
Last Week in AI
Last Week in AI
H
Help Net Security
The GitHub Blog
The GitHub Blog
S
SegmentFault 最新的问题
F
Fortinet All Blogs
I
InfoQ
宝玉的分享
宝玉的分享
A
About on SuperTechFans
MongoDB | Blog
MongoDB | Blog
Microsoft Azure Blog
Microsoft Azure Blog
Blog — PlanetScale
Blog — PlanetScale
B
Blog

blag

SQLite prefixes its temp files with `etilqs_` - blag Setsum - order agnostic, additive, subtractive checksum - blag Oldest recorded transaction - blag Replacing a cache service with a database - blag SQLite commits are not durable under default settings - blag PSA: SQLite WAL checksums fail silently and may lose data - blag Rickrolling Turso DB (SQLite rewrite in Rust) - blag Collection of insane and fun facts about SQLite - blag How bloom filters made SQLite 10x faster - blag In search of a faster SQLite - blag Galloping Search - blag Building a distributed log using S3 (under 150 lines of Go) - blag Zero Disk Architecture - blag PSA: Most databases do not do checksums by default - blag PSA: SQLite does not do checksums - blag Disaggregated Storage - a brief introduction - blag Why does SQLite (in production) have such a bad rep? - blag SQLite Slaps - blag Now - blag Learning C - blag Snapshot Testing - blag Win: contribution to libSQL (SQLite) codebase - blag Errata in Hekaton MVCC paper - blag Internet is wholesome: MVCC edition - blag It is becoming difficult for me to be productive in Python - blag MongoDB secondary only index - blag Introducing CaskDB – a project to teach you writing a key-value store - blag Recurse Center: Winter Break - blag Recurse Center Day 24: Hacking Go compiler to add a new keyword - blag Recurse Center Day 20: Django v4 upgrade (from v1) - blag
When is my Cake Day? - blag
2015-11-21 · via blag

kek

Reddit gives all the user info in a handy JSON at this URL: https://www.reddit.com/user/<username here>/about.json

example: https://www.reddit.com/user/spez/about.json

The created_utc field in data is the date of user’s registration aka Cake Day in unix epoch format (in UTC) and we can easily convert that to readable format:

>>> import time
>>> time.strftime("%D", time.gmtime(1118030400))
'06/06/05'

Using Python Requests, we can turn this into a handy function:

import time
import requests

def get_my_cake_day(username):
    url = "https://www.reddit.com/user/{}/about.json".format(username)
    r = requests.get(url)
    created_at = r.json()['data']['created_utc']
    return time.strftime("%D", time.gmtime(created_at))

Though above function will work, but soon it will start throwing HTTP 429 error i.e Too Many Requests. Thing is, Reddit doesn’t really like when someone tries to fetch the data like this. The requests are made directly on Reddit servers without using the API. Now if you have want to find cake day of hundreds of users, you cannot use this method.

Solution? Use Reddit’s API. In Python, we will use praw and prawoauth2. praw is a Python wrapper for Reddit’s API and prawoauth2 helps dealing with OAuth2.

Let’s start by installing praw:

pip install praw

Now we can convert the get_my_cake_day to praw version and get the user details like this:

import time
import praw

reddit_client = praw.Reddit(user_agent='my amazing cake day bot')

def get_my_cake_day(username):
    redditor = reddit_client.get_redditor(username)
    return time.strftime("%D", time.gmtime(redditor.created_utc))

Above code pretty much self explanatory. What if the user doesn’t exist or shadowbanned? In such cases, praw throws an exception: praw.errors.NotFound. Lets modify get_my_cake_day to catch this:

def get_my_cake_day(username):
    try:
        redditor = reddit_client.get_redditor(username)
        return time.strftime("%D", time.gmtime(redditor.created_utc))
    except praw.errors.NotFound:
        return 'User does not exist or shadowbanned'

This is better compared to earlier version and we will stop getting rate limit errors often. Also, praw will handle such cases and makes requests again to fetch the data. But what if we want to increase the limit?

The above requests are not authenticated, meaning Reddit does not recognise your app. However, if we register this app in Reddit and let Reddit know, then requests limits will increase. So to authenticate our app over Oauth2, we will use prawoauth2. Lets install it first:

pip install prawoauth2

Follow the simple steps here to register your app on Reddit. Once done, you will get app_token and app_secret. Then you need to get access_token and refresh_token. You could use this handy onetime.py script. For detailed instructions check the documentation of prawoauth2. You should never make app_token, app_secret, access_token and refresh_token public and never commit them to version control. Keep them always secret.

Here is the complete script using prawoauth2:

import time
import praw

from secret import (app_key, app_secret, access_token, refresh_token,
                    user_agent, scopes)

reddit_client = praw.Reddit(user_agent='my amazing cakeday bot')
oauth_helper = PrawOAuth2Mini(reddit_client, app_key=app_key,
                              app_secret=app_secret,
                              access_token=access_token,
                              refresh_token=refresh_token, scopes=scopes)


def get_my_cake_day(username):
    try:
        redditor = reddit_client.get_redditor(username)
        return time.strftime("%D", time.gmtime(redditor.created_utc))
    except praw.errors.NotFound:
        return 'User does not exists or shadowbanned'

Again, pretty much self explanatory. If your tokens are correct and once PrawOAuth2Mini is initialized properly, there will be no issues with the app and you will have twice as many requests as compared to unauthenticated version.

Want to see above app in action? Check this - kekday. The app is open source and released under MIT License.