Introduction
In this tutorial, we will be building a Flask app to search through all the social links of a person. We will be an API from RapidAPI which you can get from here.
Table of Contents
Install modules
To begin we will first need to download a few modules, open your terminal and run the commands shown below.
pip3 install Flask
pip3 install requests
File structure
Create a folder called flask-social-links-search which will contain our project files. Inside create a folder called templates and insert a file called index.html
Import modules
Inside the flask-social-links-search create a file called app.py and import the following modules as shown below.
from flask import Flask, render_template, request
import requests
Initialize
Now let’s initialize our application, to do this add the code below.
app = Flask(__name__)
Create view
Now let’s create a view to handle the search functionality. Here is the code below.
@app.route('/',methods=['GET','POST'])
def index():
if request.method == "POST":
name = request.form['name']
url = "https://social-links-search.p.rapidapi.com/search-social-links"
querystring = {f"query": {name},"social_networks":"facebook,tiktok,instagram,snapchat,twitter,youtube,linkedin,github,pinterest"}
headers = {
"X-RapidAPI-Key": "YOUR API KEY",
"X-RapidAPI-Host": "social-links-search.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers, params=querystring).json()
facebook = response['data']['facebook']
instagram = response['data']['instagram']
twitter = response['data']['twitter']
linkedin = response['data']['linkedin']
github = response['data']['github']
youtube = response['data']['youtube']
pinterest = response['data']['pinterest']
tiktok = response['data']['tiktok']
snapchat = response['data']['snapchat']
return render_template("index.html",
facebook=facebook,
instagram=instagram,
twitter=twitter,
linkedin=linkedin,
github=github,
youtube=youtube,
pinterest=pinterest,
tiktok=tiktok,
snapchat=snapchat)
return render_template("index.html")
if __name__ == "__main__":
app.run(debug=True)
The above code first asks for a name from the user and then the name is passed to the API above which extracts the name on various social media platforms and then renders the results to a html template. To get the API key for your project sign up on GitHub and subscribe to the API here.
Create template
In the index.html file we created above add the code below.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<style>
div {
text-align: center;
}
.results {
text-decoration: none;
}
</style>
<div>
<h2>Social Links Search </h2>
</div>
<div>
<form method="POST">
Name : <input type="text" name="name" placeholder="Enter the name of the artist" required>
<input type="submit" value="Submit">
</form>
</div>
<div class="results">
{% if facebook %}
<h2>Facebook:</h2>
{% for link in facebook %}
<a href="{{ link }}">{{ link }}</a><br>
{% endfor %}
{% endif %}
{% if instagram %}
<h2>Instagram:</h2>
{% for link in instagram %}
<a href="{{ link }}">{{ link }}</a><br>
{% endfor %}
{% endif %}
{% if twitter %}
<h2>Twitter:</h2>
{% for link in twitter %}
<a href="{{ link }}">{{ link }}</a><br>
{% endfor %}
{% endif %}
{% if linkedin %}
<h2>LinkedIn:</h2>
{% for link in linkedin %}
<a href="{{ link }}">{{ link }}</a><br>
{% endfor %}
{% endif %}
{% if github %}
<h2>Github:</h2>
{% for link in github %}
<a href="{{ link }}">{{ link }}</a><br>
{% endfor %}
{% endif %}
{% if youtube %}
<h2>Youtube:</h2>
{% for link in youtube %}
<a href="{{ link }}">{{ link }}</a><br>
{% endfor %}
{% endif %}
{% if pinterest %}
<h2>Pinterest:</h2>
{% for link in pinterest %}
<a href="{{ link }}">{{ link }}</a><br>
{% endfor %}
{% endif %}
{% if tiktok %}
<h2>TikTok:</h2>
{% for link in tiktok %}
<a href="{{ link }}">{{ link }}</a><br>
{% endfor %}
{% endif %}
{% if snapchat %}
<h2>Snapchat:</h2>
{% for link in snapchat %}
<a href="{{ link }}">{{ link }}</a><br>
{% endfor %}
{% endif %}
</div>
</body>
</html>
Run
Now let’s run our application, open your terminal and run the commands shown below.
python3 app.py
Results
When the command above is executed successfully your application should be available at the address [ http://127.0.0.1:5000 ]. Open the address in the browser and you there you have you application as shown below.
There you have it, thanks for reading. Happy Coding.