Skip to content

Interesting API Introduction

Info

Author: Echo, published on November 26, 2021, reading time: about 6 minutes, WeChat official account article link:

1 Preface

Friends, another beautiful weekend has arrived, did you have a happy week? Were you praised? Have you figured out what to do to enjoy the weekend? Today, let's start with these questions and talk about the romance and happiness that data science brings us.

2 Two Silly Yet Interesting APIs

Maybe you are tired after working/studying for a week; maybe it's winter and the cold and work make you unhappy; maybe you've been sitting for too long and eating too much, after all, every day is a cheating day, the soreness of your shoulders and the heaviness of your fat make you a little tired. So, let me introduce you to the first silly API, which can automatically generate funny and romantic sayings to make you happy.

Example of funny sayings

Every weekend, I always think about what to do. I don't know if you have the same problem. If so, the following boredAPI may be able to help you. Its API name is self-explanatory. Whenever you are bored, it can generate some activities for you to refer to. These include the content, type, number of participants, cost, link (if any), and feasibility. For example, I randomly generated some activities in the morning when I was bored, and now I'm doing the fifth thing it recommended to me :)

Example of recommended activities

Of course, you can also modify it according to your own preferences. Here is a reference to the activity parameters for boredAPI. The official documentation is also attached here. You can limit the type, the number of participants, the cost, and the feasibility according to your needs, making it more tailored to your needs. For example, on the day when I am determined to learn something, I limit the activities to education type, and the example is as follows. We are amazed to find that learning activities are really almost zero-cost, can be started at any time, and can be done alone. There is no excuse for not learning anymore...

Example of learning activities
Reference to activity parameters

3 Code Implementation

API (Application Programming Interface) is an interface for applications. It allows us to implement various interesting functions with just a few simple lines of code without having to reinvent the wheel. In the following demo, we call two APIs to randomly generate funny sayings and recommended activities. All you need to do is run it again. The code is as follows:

import requests
import json
import random
import time
import pandas as pd

user_agents = [
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 OPR/26.0.1656.60',
    'Opera/8.0 (Windows NT 5.1; U; en)',
    'Mozilla/5.0 (Windows NT 5.1; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.50',
    'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.50',
    'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0',
    'Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36',
    'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
    'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 ',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36',
    "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36",
    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14",
    "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)"
]

def getHeaders():
    user_agent = user_agents[random.randint(0, len(user_agents)-1)]
    headers = {
        'User-Agent': user_agent
    }
    return headers

# Feeling tired, listen to some sweet words of encouragement
sweet_words = []
url1 = 'https://chp.shadiao.app/api.php'
for i in range(5):
    time.sleep(random.uniform(0.1,1))
    response1 = requests.get(url1, headers=getHeaders())
    sweet_words.append(response1.text)
truth = pd.DataFrame(sweet_words,columns=['Words of Encouragement'])
truth

# See what to do on the weekend
todo = []
#generate random activity
url2 = 'http://www.boredapi.com/api/activity/'
#generate a specific type of activity randomly. The following example is for education type.
# url2 = 'http://www.boredapi.com/api/activity?type=education'
for i in range(5):
    time.sleep(random.uniform(0.1,1))
    response2 = requests.get(url2, headers=getHeaders())
    item = json.loads(response2.text)
    todo.append((item["activity"],item["type"],item["participants"],item["price"],item["link"],item["accessibility"]))
todo_list = pd.DataFrame(todo,columns=['Things to Do','Category','Number of Participants','Price (0-1)','Link','Feasibility (0-1)'])
todo_list['Feasibility (0-1)'] = 1-todo_list['Feasibility (0-1)']
todo_list

4 Conclusion

The two APIs introduced today are very easy to use, as


Viewed times

Comments