Ruby, SQL, and Facebook Graph API: Heckin Good Doge Memes

My Facebook is basically nothing but dog memes. Seriously.

The story is, I almost quit Facebook altogether after the Election because my feed became too toxic to consume any longer. Then the story is, I only managed to stay away from Facebook til about 11:30 the Morning of Day 1. I couldn’t handle the FOMO over what pets I’d miss.

So instead of quitting, I developed a hair-trigger “unfollow” finger for anything negative. After a week or two my feed become all animals. And having nothing but animals, my Facebook activity became something like multiple 10-minute streams daily of me sharing every dog video I saw. The last count I had was north of 2,500 shared posts since then, with almost zero personal content.

For my first Flatiron School blog post assignment, I figured I’d crunch some numbers about whose videos I share the most. I pulled the data from Facebook’s Graph API explorer, and saved my post data locally. Then I created a tiny SQL database so I could operate on posts individually.

Here’s the code:

require './config/environment.rb'
require './data.rb'

#Seed the database

data.each do |post|
  new_post = Post.create(story: post[:story])
  new_post[:source] = new_post.get_source

# The get_source method is actually in the Post model, and its purpose is to
# name the source of which video I shared, by cutting the name out of FB's
# default text of "Mark Bello shared #{source}'s' video"

# def get_source
#     self[:story].slice!("Mark Bello shared ")
#     self[:story].slice!(/'.+/)
#     self[:story]
# end

#Create a hash to operate on the sources
source_hash = {}

#Get a list of every unique source
all_sources ={|post| post.source}
unique_sources = all_sources.uniq

#Initialize each source as a key in the source hash
unique_sources.each do |source|
  source_hash[source.to_sym] ||= 1

#Increment the source's key for each video in the dataset
all_sources.each do |source|
  source_hash[source.to_sym] += 1

#Order the sources from most to least videos I shared
ordered_sources = source_hash.sort_by {|source| source[1]}.reverse

#Clear the terminal and print the greeting

puts ""
puts "----------------------"
puts ""

#Puts out the top 20 sources
counter = 0

while counter < 20
  puts "#{ordered_sources[counter][1]} Videos: #{ordered_sources[counter][0]}"
  counter += 1

#Puts out an extra space between the list and the command line

puts ""

  • 35 Videos: Bichinhos fofinhos pra quem ta cansado de política no feed
  • 33 Videos: Go Animals
  • 18 Videos: Doggo
  • 12 Videos: NTD Life
  • 12 Videos: Crispy Doggo Memes
  • 9 Videos: NTD Television
  • 8 Videos: NTD Funniest
  • 8 Videos: I Love Pitbulls
  • 7 Videos: The Dodo
  • 6 Videos: Animals in Predicaments
  • 5 Videos: Dogs World
  • 5 Videos: Pitt Bull
  • 5 Videos: Fat ass woofers
  • 5 Videos: Viral TRND
  • 5 Videos: Doggo News
  • 5 Videos: The Pet Collective
  • 5 Videos: Awesome Animal
  • 4 Videos: Pit Bulls Area
  • 4 Videos:
  • 4 Videos: Animal Lovers

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see

Mark Bello

Mark Bello