Guoqi Sun

Home

Archive

CamLife

Links

About

Quickly Build a Douban Book/Movie Poster WebAPI Using Flask

2023-01-09

New year, new look. Recently, I’ve been tinkering with my new blog, preparing to give it a good makeover.

In the old site, there was a Douban Book/Movie display page, which I thought was pretty good.

So I wanted to add this element to the new website as well. On the other hand, it can also promote more reading and more movie watching.

Initial Exploration

Emmmm, how to introduce it? Directly develop a plugin? I haven’t learned how to develop a halo plugin yet❌

Let’s search on GitHub first to see if there are any related projects? Good idea✅

So I found this repository Portal

The repository was created three years ago. Let’s see if the code can run normally!

Sure enough… there is a bug, hahaha!

Fixing the Bug

Print the status code first to diagnose where the bug is.

Run it!

418, it’s the first time I’ve encountered this status code, Google it!

I’m a teapot, hahaha, IT technology is full of fun.

Ah, I don’t quite understand… Although the predecessor wrote the UA list, they didn’t add it to the headers when making the request. No wonder Douban detected you as a crawler, haha.

Let’s correct it!

Perfectly running!

But…

New Requirement

Do I have to run this code to generate a poster every time I share a book/movie?

Why not directly develop an interface so that it can be called directly by adding code like this?

![](https://douban.sunguoqi.com?params=DOUBANURL)

Technical Exploration

The requirement exists, let’s start exploring the implementation plan.

Make the Python script into an API?

We just need to create a web application. When the user accesses the specified route with the correct parameters, it can trigger the corresponding logic and finally return the data.

So what technology should we use to write this web application?

Aha, there are ready-made web frameworks, such as Spring Boot, Django, Flask…

Spring Boot?

JavaWeb, I’m not familiar with Java, haven’t done actual development, it might be a bit difficult to get started❌

Django?

I have self-studied Django for a while, it’s relatively easy to get started, but Django is “too complete”, with built-in backend, database ORM mapping, template mechanism, form handling, sessions… Using it for this script might be a bit cumbersome, feels like overkill⏱️

Flask?

I’ve heard that Flask is relatively lightweight. Let’s check the official website. Wow, five lines to output hello world, I think it works, let’s go with it✅

Official Development

1. Failed Attempt

I originally wanted to develop it on Windows using VScode and then upload it to the server, but configuring the development environment took a long time.

First, I encountered problems when writing Flask_App to the Windows system environment variables. Second, I had issues running Flask on VSCode

Forget it, let’s not configure it, let’s go directly to the server.

2. Hello World

Easily configured the environment on Ubuntu, then typed in the code (copy-paste)

from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"

Hello, World! It’s so elegant🎉

3. Writing Route Logic

After some intense operations, I added the following code to the original code, and it was done!

...
import flask
from flask import send_file
def main(url):
...
return api_img_path
@app.route('/',methods=['get'])
def img():
url = flask.request.args.get('url')
img = main(url)
return send_file(img)

Of course, there was actually a lot of intense debugging during this period🥹

Mission Accomplished

Github: https://github.com/sun0225SUN/DoubanPoster