Displaying Hexo Blog View Counts on AWTRIX

Why I Wanted to Build This App

Back in 2021, when I was revamping my desk setup, I wanted to add a clock. After looking around, I ended up getting an AWTRIX Pro mini.

There are plenty of interesting apps in the AWTRIX App Store — things like GithubFollowers, Bilibili, and so on. I installed GithubFollowers, but after a while I noticed the number was stuck at 7. Pretty embarrassing, honestly. It barely registered emotionally, so I uninstalled it.

That got me thinking — what if I could see the total view count of my blog (the sum of views across all posts)?

How It Works

This was my first time developing an AWTRIX app, and I was pretty clueless at first. Reading the official documentation is the fastest way to get started — if you’re interested, check out Programming (blueforcer.de).

My blog is built with Hexo, and view counting is handled by LeanCloud.

LeanCloud provides a REST API — see the docs here: Storage REST API Guide - LeanCloud Docs

Step 1: Fetch the Data

1
2
3
4
5
6
7
Sub App_startDownload(jobNr As Int)
Select jobNr
Case 1
App.Download(App.get("API")&"/1.1/classes/Counter")
App.Header = CreateMap("X-LC-Id":App.get("AppId"), "X-LC-Key":App.get("AppKey"))
End Select
End Sub

One thing to watch out for here is the case sensitivity of keys in App.get(). I stumbled on this myself.

Step 2: Process the Data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Sub App_evalJobResponse(Resp As JobResponse)
Try
If Resp.success Then
Select Resp.jobNr
Case 1
Dim parser As JSONParser
parser.Initialize(Resp.ResponseString)
Dim root As Map = parser.NextObject
Dim results As List = root.Get("results")
total_view = 0
For Each postView As Map In results
total_view = total_view + postView.Get("time")
Next
End Select
End If
Catch
App.throwError(LastException)
End Try
End Sub

total_view is a global variable — remember to reset it to zero.

Step 3: Display the Output

1
2
3
Sub App_genFrame
App.genSimpleFrame(total_view,1720,True,False,Null,True)
End Sub

Full source code: https://github.com/chengqing-su/awtrix-hexo-leancloud-counter

If you need a pre-compiled JAR, grab it here: https://github.com/chengqing-su/awtrix-hexo-leancloud-counter/releases/download/v1.0.0/HexoLeanCloud.tar.gz

Experience

Watching the number change over the past few days has actually given me more motivation to write and maintain my blog.