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

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

  1. dataflow_Apache Beam,Google Cloud Dataflow和使用Python创建自定义模板

    dataflow 阿帕奇光束 (Apache Beam) Apache Beam(Batch + Stream) is a unified programming model that defines ...

  2. python 文件格式转换_数据分析:基于Python的自定义文件格式转换系统

    ( 白宁超 2018年7月16日14:47:41 ) 导读:随着大数据的快速发展,自然语言处理.数据挖掘.机器学习技术应用愈加广泛.针对大数据的预处理工作是一项庞杂.棘手的工作.首先数据采集和存储,尤 ...

  3. python创建自定义函数is_number()来判断一个字符是否是数字

    主要使用错误异常处理try:except:,和float(s)以及unicodedata.numeric(s)函数来处理 def is_number(s):try:float(s) # 如果能转换fl ...

  4. python pytorch自定义_PyTorch使用自定义模块创建数据模型

    还有另一种查找预测的方法.在上一节中, 我们使用forward()和实现线性模型来找到预测.此方法非常有效且可靠.很容易理解和实施. 在"定制模块"中, 我们使用类创建一个定制模块 ...

  5. python做bi系统_如何使用Python创建可视化对象

    早前,Power BI就已经支持使用Python创建可视化对象了,当你遇到自定义程度较高的可视化对象时,Python就大大的派上了用场:那么我们如何使用呢?接下来小悦就为各位伙伴们介绍一下吧~ 首先, ...

  6. 独家 | 使用TensorFlow 2创建自定义损失函数

    作者:Arjun Sarkar 翻译:陈之炎 校对:欧阳锦 本文约1900字,建议阅读8分钟 本文带你学习使用Python中的wrapper函数和OOP来编写自定义损失函数. 标签:TensorFlo ...

  7. 使用tolua++编译pkg,从而创建自定义类让Lua脚本使用

    2019独角兽企业重金招聘Python工程师标准>>> 在Lua第三篇中介绍了,如何在cocos2dx中使用Lua创建自定义类供Lua脚本调用使用,当时出于Himi对Lua研究不够深 ...

  8. python使用del保留字定义一个函数-python中自定义函数的保留字是

    基本使用(推荐学习:Python视频教程)def function_name(parameters): expressions Python使用def开始函数定义,紧接着是函数名,括号内部为函数的参数 ...

  9. [转]利用ASP.NET 2.0创建自定义Web控件(1)

    原址:http://hi.baidu.com/sjbh/blog/item/cc58fd1bd35d3ad2ad6e7593.html   简介 从使用基本的文本编辑器到创作标记页面,Web 开发已经 ...

最新文章

  1. Java 编程技巧之数据结构
  2. Java的List遍历
  3. react 组件传值
  4. insert into select 优化_数据库优化总结
  5. 基于注意力机制的seq2seq网络
  6. 树莓派2 安装linux系统安装教程,安装Ubuntu MATE 15.04 for 树莓派2
  7. 图片显示不出时显示默认图片
  8. 差分编码解析以及FPGA实现
  9. 使用 Sun Jimi 进行图像格式转换
  10. Bitwise Operation
  11. 希尔伯特《几何学基础》的章节目录
  12. C++数据结构实验--图的基本操作
  13. Android device supports but apk only supports armeabi,x86,x86_64
  14. [Hgame CTF]easyenc
  15. 陪审团的人选(Python)
  16. 在软件工程领域,搞科研的这十年!
  17. bzoj千题计划126:bzoj1038: [ZJOI2008]瞭望塔
  18. 以太坊开发测试(6) 运行《区块链技术进阶与实战》Score 电子积分系统
  19. 也谈Scrum Master的职业发展路径
  20. 2021高考甘肃师大附中成绩查询,西北师大附中2019年高考喜报 西北师大附中2019年高考成绩...

热门文章

  1. 【spring】使用eclipse在没网时编写配置文件无法获取提示 解决方法
  2. Angular 文件上传与下载
  3. 《深入理解计算机系统》第七章——链接知识点总结
  4. 多线程1(进程、[创建]线程与生命周期)
  5. python xlrd读取excel所有数据_python读取excel进行遍历/xlrd模块操作
  6. python数据库建表_mysql数据表如何创建
  7. H5 画布解决跨域问题,画布保存为图片显示在页面上
  8. this和that的区别和原理
  9. 数据千万条,备份第一条,数据找不回,老板两行泪
  10. windows 自动化目录大纲(各企业架构不一样,按需选择)