Making Art By Judging Reddit

About the project

Is the Raspberry Pi 4 powerful enough to judge Reddit? This project is all about answering the important questions.

Items used in this project

Hardware components

Raspberry Pi 3 Model B Raspberry Pi 3 Model B x 1
Screen Screen x 1
Frame Frame x 1

Software apps and online services

Pushshift.io Pushshift.io
Microsoft Azure Microsoft Azure
Raspberry Pi Raspbian Raspberry Pi Raspbian

Story

Overview

Below a quick overview of the content.

  • Introduction and showcase video
  • Fetching the latest Reddit comment
  • Scoring the comment
  • From score to colour
  • Result

Introduction and showcase video

We've all heard about Reddit, and how powerful the new Raspberry Pi is. Let's combine them to answer the age old question, "Can a Pi judge Reddit?"

Project Video

Fetching the latest Reddit comment

To start of we're going to fetch the latest Reddit comment. Pushshift.io is exactly what we need. With a simple API call we can fetch the latest comment.

url = "https://api.pushshift.io/reddit/search"   
querystring = {"sort":"desc","sort_type":"retrieved_on","limit":"1","lang":"eng"}   
response = requests.request("GET", url, params=querystring)   
d = json.loads(response.text)
comment = str(d['data'][0]['body'])

And that's it for this part, we're fetching the latest comment and can watch them roll in.

Reddit Comments

Scoring the comment

We now want to know how positive or negative our comment is.

To make our lives easier we're going to use an Azure API, the sentiment analyses.

This API will receive a text and rate it with a number between 0 and 1.

A super positive text will get a score close to 1 and a very negative close to 0.

Before we can use any of this AI magic we need to do some mandatory setup.

With that, our cognitive services is all ready and we can start using it.

Here's the base call:

url = "https://westeurope.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment"
  
payload = '{"documents": [{"text":"' + comment + '",  "language": "en", "id":"1"}]}'
  
headers = {
    'ocp-apim-subscription-key': "<Your Key Here>",    
    'content-type': "text/json; charset=utf-8",    '
    cache-control': "no-cache"}
  
response = requests.request("POST", 
                            url, 
                            data=payload.encode('utf-8'), 
                            headers=headers)
  
d = json.loads(response.text)
score = float(d['documents'][0]['score'])
  

Some example comment with their score:

Comments with their score

From score to colour

Almost there, we can start creating some beautiful art.

To limit the API calls we divide the screen in 16 sub screens, each scored comment is represented in a pixel block of 30x30 per sub screen.

To get the colour right we start with red or RGB(255, colour, colour). We calculate the colour placeholder by multiplying the score with 255. Meaning a 1 (positive) will create RGB(255, 255, 255), or white.A negative comment, or 0 will give us red, and that's RGB(255, 0, 0).

The last part is assigning this colour to the pixel block of each sub screen and moving to the next one, completing the logic.

You can find the complete code in de 'Code' section.

From comment to art

Result

And that's it! We've got a very stylish piece of dynamic art, made by judging Reddit comments.

It also answers our question, the new Pi can judge Reddit, just not sure how well...

Looking neat

Code

Code

Credits

Photo of 8BitsAndAByte

8BitsAndAByte

We’re Dane & Nicole, two makers that create tremendously terrible tech, which we happily share with you on our channel! https://www.youtube.com/c/8bitsandabyte

   

Leave your feedback...