reddit_如何使用Python创建自定义Reddit通知系统

reddit

by Kelsey Wang

王凯西

如何使用Python创建自定义Reddit通知系统 (How to make a custom Reddit notification system with Python)

Don’t you just love automated emails? I know I do. I mean, who doesn’t enjoy waking up to 236 new messages from Nike, Ticketmaster, and Adobe Creative Cloud every morning? What a fantastic way to start my day! ??

您不只是喜欢自动发送的电子邮件吗? 我知道 我的意思是,谁不喜欢每天早晨从耐克,Ticketmaster和Adobe Creative Cloud唤醒多达236条新消息? 美好的一天开始了! ??

Anyway, today I’ll be showing you how to drown your inbox in more clutter, for God-knows-what reason. We’re going to be using Python to create a custom Reddit email-notification system. That means we’ll be writing a script that looks for Reddit posts matching some keywords and then emails us when such posts appear.

无论如何,今天,我将向您展示如何因神所知而更加混乱地淹没您的收件箱。 我们将使用Python创建自定义Reddit电子邮件通知系统。 这意味着我们将编写一个脚本,查找与某些关键字匹配的Reddit帖子,然后在出现此类帖子时通过电子邮件发送给我们。

There are a few reasons that you might be doing this. Maybe you’re really excited about some topic on Reddit. Maybe you’re trying to discover a new karma-farming technique because Internet points are important to you. Maybe you want to send annoying emails to your friends. Or maybe you just want more emails in your inbox to deal with your crippling loneliness. Oops, sorry — went too far. Let’s get started.

您可能会出于某些原因这样做。 也许您对Reddit上的某个话题感到非常兴奋。 也许您正在尝试发现一种新的业力养殖技术,因为互联网点对您很重要。 也许您想发送烦人的电子邮件给您的朋友。 或者,也许您只希望收件箱中有更多电子邮件来应对您的残酷孤独感。 糟糕,抱歉-太过分了。 让我们开始吧。

通过Reddit看 (Looking through Reddit)

Reddit has a nice API that you can do a lot with. To make things even easier, we will be using PRAW, the Python Reddit API Wrapper.

Reddit有一个不错的API ,您可以做很多事情。 为了使事情变得更加简单,我们将使用PRAW (Python Reddit API包装器)。

You’ll need a Reddit account first. Once you have one, go here to create an app. Name it anything, and make sure “script” is selected. As per the docs, you can just put http://localhost:8080 for your redirect URI.

您首先需要一个Reddit帐户。 拥有一个应用程序后,请转到此处创建一个应用程序。 对其进行命名,并确保选择“脚本”。 根据文档,您只需将http://localhost:8080用作重定向URI。

Now, you’re ready to start that nifty script! In the code below, I look through a subreddit, picking out posts that match my needs.

现在,您准备开始该漂亮的脚本! 在下面的代码中, 我浏览了一个subreddit,挑选出符合我需要的帖子。

I consider a post a match if it is relevant enough and if it is popular enough. More specifically, the post is relevant enough when it has a keyword_count that’s not -1 (I’ll explain this below) and popular enough when it has a weighted_score greater than a predefined MIN_RELEVANT_WEIGHTED_SCORE. The weighted score simply factors in the score of the post and the number of comments on the post. Anyway, this is what best fit my needs, so feel free to better define what a match means to you.

如果相关性足够高且受欢迎程度高,我认为该职位是一场比赛 。 更具体地说,如果该帖子的keyword_count不为-1(我将在下面解释),则该帖子足够相关,而当weighted_score大于预定义的MIN_RELEVANT_WEIGHTED_SCORE时,该帖子足够受欢迎。 加权分数只是考虑帖子的分数和帖子评论的数量。 无论如何,这是最适合我需要的东西,所以随时可以更好地定义匹配对您的意义。

Now, I promised you I would talk about the keyword_count party going on. Spoiler: it’s not really a party. I just devised this simple way of assessing relevancy: there are required terms and secondary terms. A post is relevant if and only if all the required terms are in the title, and at least X number of secondary terms are in the title (where X is some predefined number). Again, this part can be re-imagined in infinitely different ways, but this is just what I did.

现在,我向您保证,我将谈论正在进行的keyword_count派对。 剧透:这不是一场派对。 我只是设计了一种简单的评估相关性的方法:有必要的术语和次要术语。 且仅当标题中包含所有必需术语且标题中至少包含X个辅助术语(其中X是一些预定义的数字)时,该帖子才有意义。 同样,可以用无限不同的方式重新想象这部分,但这就是我所做的。

Now we have everything to comb through our subreddit and tease out the good stuff about conspiracies or whatever. Cool. So, like my homie Ariana says, “thank u, next.”

现在,我们可以梳理所有内容,梳理一下关于串谋之类的好东西。 凉。 因此,就像我的亲爱的阿丽亚娜(Ariana)说的:“谢谢,接下来。”

电子邮件通知 (Emailing notifications)

Time to start spamming. In the code below, I’m using smtplib (the Simple Mail Transfer Protocol client) to help me send my emails. I then craft the beautiful email with HTML, using the info from Reddit that we got above to populate it. And the best (or worst?) part is, if you want to notify everyone you know about the latest and greatest Reddit posts, you can simply add more email addresses to the email_list.

是时候开始发送垃圾邮件了。 在下面的代码中,我正在使用smtplib (简单邮件传输协议客户端)来帮助我发送电子邮件。 然后,我使用上面提供的Reddit信息填充HTML,用HTML编写精美的电子邮件。 最好的(或最糟的?)部分是,如果您想将Reddit的最新和最email_list帖子通知给您认识的所有人,则只需在email_list添加更多电子邮件地址即可。

Important side note: make sure the email you use to send the emails have less secure app access enabled if it’s a Gmail address, or this will not work.

重要的旁注:请确保您用于发送的电子邮件 如果这是Gmail地址,则电子邮件启用的安全性应用访问权限较低 ,否则将无法使用。

使它永远运行 (Make it run forever)

If you don’t have time to continually browse Reddit, you don’t have time to continually run this script. I used Heroku Scheduler to run this script every 10 minutes, as suggested by this Stack Overflow answer. It’s pretty easy to follow: add in a few additional files and a dummy web server, push to Heroku, add the Heroku Scheduler add-on, and BAM! You’re set until you run out of free dyno-hours. ??

如果您没有时间继续浏览Reddit,则没有时间继续运行此脚本。 根据堆栈溢出答案的建议,我使用Heroku Scheduler每10分钟运行一次此脚本。 这很容易遵循:添加一些其他文件和一个虚拟Web服务器,推送到Heroku,添加Heroku Scheduler附加组件,以及BAM! 您将一直处于设置状态,直到用尽了免费的动态小时。 ??

Is this the best solution? No. But is it sufficient for my purposes? Yep. If you know of a similarly trivial way to do this, please let me know!

这是最好的解决方案吗? 否。但是这足以满足我的目的吗? 是的 如果您知道执行此操作的类似方法,请告诉我!

结论 (In conclusion)

That’s pretty much all to this project. This GitHub repo contains all my code. Because of all the work that literally everyone else has already done, it’s quite a simple task to build this custom Reddit notification system. Gotta love the ✨magic✨ of software development.

这几乎就是这个项目的全部。 这个GitHub仓库包含了我所有的代码。 由于实际上每个人都已经完成了所有工作,因此构建此自定义Reddit通知系统是一项非常简单的任务。 一定喜欢软件开发的“魔术”。

If you made it all the way down to here, please comment “North Dakota is the top producer of barley in the USA” in the box below.

如果一直到这里,请在下面的框中评论“北达科他州是美国最大的大麦生产国”。

Thanks for reading!

谢谢阅读!

翻译自: https://www.freecodecamp.org/news/make-a-custom-reddit-notification-system-with-python-4dd560667b35/

reddit

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值