Building an Audio generator  for DEV.to blogposts
Tags: python, showdev, productivity
Published: 2021-03-06 04:24:06

<center>🎧 &nbsp;<b> LISTEN TO THIS ARTICLE</b></center>
{% spotify spotify:episode:5MUmWWNXt6DC9Yc4xPYyrB?si=787dHWMsReSKfM9DGbzb-g %}

I'm kind of a person who likes to listen rather than reading. And I feel my productivity increases when listening rather than reading. As dev.to don't have such audio feature(which will be helpful if they bring it natively).

So, I built a tool that takes input any dev.to blogpost URL and outputs Audio which you can also download.

Tool: https://audioblogs.herokuapp.com/

You can check the working of tool in the below video

{% youtube jYgMffIJHGY %}

<hr>

The workflow of this tool is to scrape the article/blogpost and generate audio using gTTS module.

<h4>Modules used:</h4>
<ul>
<li><b>Requests</b> - will allow us to send HTTP requests to get HTML pages</li>
<li><b>BeautifulSoup</b> - will help us parse the HTML pages</li>
<li><b>gTTS</b> - converts the text entered, into audio which can be saved as a mp3 file</li>
<li><b>Streamlit</b> - A Python library that makes it easy to create and share beautiful, custom web apps</li>
</ul>

Let's begin:

import all necessary modules

```python
import streamlit as st
import requests
from bs4 import BeautifulSoup
from gtts import gTTS 
```

Getting the contents of a webpage into a variable
```python
results = requests.get("https://dev.to/sunilaleti/building-a-pdf-locker-gui-application-4l67")
```

In order to make content easy to understand, we are using BeautifulSoup and the content is stored in soup variable
```python
soup = BeautifulSoup(results.text, "html.parser")
```
We use soup.find to get name of the blogpost and you can get class names by inspecting elements 
```python
Article=soup.find("div",{"class":"crayons-article__header__meta"}).find('h1').get_text()
```
To get name of the Author
```python
Author=soup.find("div",{"class":"crayons-article__subheader"}).find('a').get_text()
```
To get article content
```python
text=soup.find("div", {"id": "article-body"}).find_all(['p','h1','h2','h3','h4','h5','h6','ol','ul'])
```
With the content we also get HTML tags. So we need to remove all HTML tags.
```python
def remove_html_tags(text):
    for item in text:
        try:
            blog.append(item.get_text())
        except:
            pass
``` 
Now after scraping the content and cleaning, we need to create audio with the help of gTTS module.
gTTS module also supports other languages like French, Spanish etc..

```python
from gtts import gTTS 

language = 'en'
myobj = gTTS(text=Text, lang=language, slow=False) 
  
# Saving the converted audio in an mp3 file name "Audio"
myobj.save("Audio.mp3") 
```

This webapp is built using <a href="https://streamlit.io/">streamlit</a> and deployed in <a href="https://dashboard.heroku.com/apps">herokuapp</a>
 
<h3>Source Code:</h3>
```python
import streamlit as st
import requests
from bs4 import BeautifulSoup
from gtts import gTTS 
def app():
    st.set_page_config(page_title="Audio Blog",page_icon="🎧")
    st.title("Generate Audio for dev.to blogposts")
    url=st.text_area("Enter any DEV.TO blog url ").strip()
    if st.button("submit"):
        if len(url)!=0:
            with st.spinner('Miracles take time to happen \n Just kidding 😂 \n Generating audio..'):
                results = requests.get(url)
                soup = BeautifulSoup(results.text, "html.parser") 
                blog=[]
                try:
                    Article=soup.find("div",{"class":"crayons-article__header__meta"}).find('h1').get_text()
                    #st.write(Article)
                    Author=soup.find("div",{"class":"crayons-article__subheader"}).find('a').get_text()
                    #st.write(Author)
                    intro="This blogpost {article} is written by {author}".format(article = Article, author = Author)
                    blog.append(intro)
                    text=soup.find("div", {"id": "article-body"}).find_all(['p','h1','h2','h3','h4','h5','h6','ol','ul'])
                    def remove_html_tags(text):
                        for item in text:
                            try:
                                blog.append(item.get_text())
                            except:
                                pass

                    remove_html_tags(text)
                    Text=""
                    for ele in blog:  
                            Text +=ele+" "

                    myobj = gTTS(text=Text, lang='en', slow=False) 
                    
                    myobj.save("Audio.mp3")
                    audio_file = open('Audio.mp3', 'rb')
                    audio_bytes = audio_file.read()
                    st.success("Play or download the audio")
                    st.audio(audio_bytes, format='audio/mp3')
                except:
                    st.error("Enter a valid url")
        else:
            st.error("Enter a valid url")


if __name__ == "__main__":
	app()
```

lemme know if you have any issues and feel free to send pull request if you had anything to add

{% github aletisunil/AudioBlog no-readme %}


<a href="https://www.buymeacoffee.com/sunilaleti" target="_blank"><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1g0v3nxthxjw79prdacu.png"></a>