Converting Twitter Tweets to Audio Streams with Python

Converting Twitter Tweets to Audio Streams with Python

Goal

I wanted to grab public Twitter feeds and parse the tweets into audio streams (as mp3's.)  It occurred to me this might be a fun and interesting code project.  

Language and Modules

I chose to use Python for this task and used two libraries outside the Standard, these being:

  • gTTS
  • bs4

gTTS is a Google text to speech library, it can be installed with pip:

pip install gtts

bs4 (Beautiful Soup) is a HTML parser and can be installed with pip:

pip install bs4

Code

The code itself is fairly simple.  The flow of execution starts with the menu method, which prints out some statements to the user and collects raw input for the twitter feed you want to scrape and convert to audio.

The next method in play is twtterAccountFeed, which takes the feed name as a parameter and appends it to the Twitter base url.  

I import the sys module, only for handling an exit code on the script. If a supplied twitter feed doesn't exist, I kill the script with exit code 1. 

Once the Twitter feed is setup I pass it into a method handling the BeautifulSoup parsing, the parseTweets method, which takes two parameters: 

  • html (the url we'll pull the HTML from, to parse)
  • feed (the supplied Twitter handle - used for naming the mp3)

At this point I iterate over the HTML looking for div's with the class of js-tweet-text-container (where the text of the tweets is.)  

I made use of a Stack Overflow article on a regular expression to strip out all the URLs  (otherwise it would read out the URLs.)  This cleaned up version of the tweets is thrown into a list for safe keeping.

The list is later converted to a string using ''.join(dialogue) where dialogue is the list name.  At this point we have our text string of the tweets of the feed in question.  Now we need to pass it to gTTS.

Since I'm leveraging a module/library to handle the TTS, this part is very simple:

gTTS is setup with the parameter value for text, being the string of 'cleaned up' tweets and the value for lang is set to English.

After that, I simply output the result of the TTS to a mp3 file using the gTTS 'save' method:

And that's it - the script can also be found on github:

Demo

To view or add a comment, sign in

More articles by Brian Warner

  • OSINT MCP & the Local LLM for Free

    I was reading a great article on Medium via OSINT Team regarding MCP. It’s a great article on MCP.

  • ElasticSearch & LLM to identify Anomalies

    This article discusses a proof of concept using ElasticSearch & LM Studio. The purpose behind this is to leverage a…

    1 Comment
  • Creating a Static Code Analysis Tool

    This year I started a push into secure code analysis. Utilizing a lot of material gained from Offensive Securities…

  • Threat Matching with Elastic Rules

    Using Elastic matching rules, with their Detection Engine, we can easily set up threat intelligence, which alerts on…

  • EDR [Wazuh]

    EDR (Endpoint Detection and Response) is part of a security toolset to ensure protection of computers (endpoints) in a…

    4 Comments
  • Monitoring QA Environments with ELK

    I haven't written an article in awhile and thought I'd share some work I've been doing with the ELK (Elasticsearch…

  • Suricata and ELK

    Playing around with Suricata and ELK on my home network, I decided to do a quick write up on installing an IDS on…

  • JavaFX with Scene Builder: Making a Desktop App

    While Qt was a great solution for desktop application development, I wanted to check out another framework that was…

  • Qt Port Scanner: Adding Concurrency

    Getting into Qt has been pretty great. I started learning it with a simple personal project - a port scanner, which I…

    1 Comment
  • First Qt App - Simple Port Scanner

    Whenever I start a new language, or framework, I like to attempt a port scanner. Making a port scanner is a simple…

Others also viewed

Explore content categories