I wrote a CLI tool in Go language to view Qiita's tag feed with CLI

Read Zenn's article (Go language package to get feed) and then gofeed I found out that there is a convenient tool called .com / mmcdole / gofeed) and wanted to touch it, so I made a simple tool that just displays the Qiita tag feed on the CLI.

qiita-tag-feed-reader-cli - CLI reader of Qiita tag feed.

How to use qiita-tag-feed-reader-cli

The explanation is the same as README at the link above, but it is as follows. is.

# install
go get github.com/shinshin86/qiita-tag-feed-reader-cli

#Display the feed by randomly specifying tags
qiita-tag-feed-reader-cli

#Display the feed of the specified tag(in this case"Go"But"go"ButOK)
qiita-tag-feed-reader-cli Go

#Of course, you can also use Japanese
qiita-tag-feed-reader-cli security

I wanted to touch gofeed completely, and it is a tool that I made for the purpose of studying Go itself, but unexpectedly it became a tool that can be used in a little free time such as during build, so I will report it here as well. It depends on what you received. (Because it is related to Qiita above all)

The source itself is very small, so I think it's best to read the source code directly to find out what you're doing. However, for the time being, I am writing to Qiita, and I will write down the technical points (although it can not be said).

I will not explain about gofeed

In addition, it seems that the chabudai is turned over after talking so far, but the usage of gofeed itself is gofeed README. I will omit it because it is a level that can be understood by looking at the Zenn post posted above.

I used to write a code to parse the acquired feed with Go myself, but I thought it would be convenient if there was gofeed because I could skip all the processing at once. An easy-to-use tool.

Display order unique to CLI

As you can see by actually hitting the command, the information is displayed in the following order. This is because it is used on the CLI, so the line of sight moves from bottom to top.

・
・
・
----------------------------------------------------
<feed items>
-----------------------
<feed items>
-----------------------
<feed items>
-----------------------
<feed title>
<feed type> <feed version>
======================

Information about feeds

I also wanted to use gofeed, so I'm getting a tag feed. For this feed URL, I referred to the Qiita post below.

[Qiita Article / User / Organization / Tag Feed URL (XML / ATOM URL of the user or tag you want to follow)-Tag Feed URL](https://qiita.com/KEINOS/items/f0a5bce2fa9cfec85f8b#%E3% 82% BF% E3% 82% B0% E3% 83% 95% E3% 82% A3% E3% 83% BC% E3% 83% 89% E3% 81% AE-url)

Processing to remove the HTML tag of the acquired HTML

The process of removing HTML tags is implemented by referring to the code below. (In fact, it's almost the same except that it supports HTML comments ...) https://gist.github.com/g10guang/04f11221dadf1ed019e0d3cf3e82caf3

The source code of HTML tag removal actually written in qiita-tag-feed-reader-cli is shown below.

func removeHTMLTag(html string) string {
	const pattern = `(<\/?[a-zA-A!-]+?[^>]*\/?>)*`
	r := regexp.MustCompile(pattern)
	groups := r.FindAllString(html, -1)

	// Replace the long string first
	sort.Slice(groups, func(i, j int) bool {
		return len(groups[i]) > len(groups[j])
	})

	for _, group := range groups {
		if strings.TrimSpace(group) != "" {
			html = strings.ReplaceAll(html, group, "")
		}
	}
	return html
}

When I was implementing it while looking at the reference code, I thought it was true, as you can see in the comments in the code above, the strings are being replaced in order from the longest.

This is because if you extract tags with this regular expression, you can get the following two patterns of tags as the acquired tag patterns.

</span></div>
</span>

When replacing HTML with strings.ReplaceAll, if</ span>is replaced first,</ span> </ div>will be left behind, so replace from a long string. I try to do. This will successfully remove all HTML tags.

Actually, I also need a test code to prove this behavior properly, but I wanted to get it into shape quickly, so I put it sideways. Since it's a big deal, I'll add the test code at a later date. → Addition: A test was added.

About the part that randomly acquires the tag of Qiita

This is not a technical point, but if you run this tool with no arguments, it will try to randomly select a Qiita tag and display the feed. For this Qiita tag, refer to the code below and get the tags in order of popularity.

[(Using Qiita API v2) How to get Qiita tag information via API --Sample code to get without specifying tags](https://qiita.com/kojiro-s/items/3862cf968d807497eef8#%E3%82 % BF% E3% 82% B0% E3% 82% 92% E6% 8C% 87% E5% AE% 9A% E3% 81% 97% E3% 81% AA% E3% 81% 84% E3% 81% A7 % E5% 8F% 96% E5% BE% 97% E3% 81% 99% E3% 82% 8B% E3% 82% B5% E3% 83% B3% E3% 83% 97% E3% 83% AB% E3 % 82% B3% E3% 83% BC% E3% 83% 89)

As you may already know from the source code, I manage 100 popular tags at the time of implementation in a Go file and call them at runtime. Therefore, it is in the form of ** randomly selected from the 100 popular tags at the time when I created this tool.

Of course, it is possible to select 100 tags in real time at the time of execution, but since it was originally the purpose of trying gofeed, it is completely horizontal here. Also, I think that the upper limit will come soon if the Qiita API is not authenticated, so I also felt that I did not want to consider that.

end

So, if you like, please use it. For me, the simplicity of this tool goes well with a little free time, and it has become a tool that can kill time unexpectedly. By all means, such as waiting time for build.

When I actually used it ↓ qiita-tag-feed-reader-cli demo gif

Recommended Posts

I wrote a CLI tool in Go language to view Qiita's tag feed with CLI
I made a CLI tool to convert images in each directory to PDF
I made a kind of simple image processing tool in Go language.
I want to work with a robot in python.
I wrote you to watch the signal with Go
I wrote a program quickly to study DI with Python ①
I wrote it in Go to understand the SOLID principle
I wrote a script to get a popular site in Japan
I tried to divide with a deep learning language model
[5th] I tried to make a certain authenticator-like tool with python
Created a package to support AWS Lambda development in Go language
[2nd] I tried to make a certain authenticator-like tool with python
Created gomi, a trash can tool for rm in Go language
[3rd] I tried to make a certain authenticator-like tool with python
I wrote a script to get you started with AtCoder fast!
I wrote a function to load a Git extension script in Python
Post to slack in Go language
I wrote a script to extract a web page link in Python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
I tried to make a mechanism of exclusive control with Go
I wrote a code to convert quaternions to z-y-x Euler angles in Python
I made a tool to automatically browse multiple sites with Selenium (Python)
(Matplotlib) I want to draw a graph with a size specified in pixels
I made a tool to convert Jupyter py to ipynb with VS Code
I made a tool that makes decompression a little easier with CLI (Python3)
A note I looked up to make a command line tool in Python
How to make a request to bitFlyer Lightning's Private API in Go language
I want to print in a comprehension
I tried a functional language with Python
I created a password tool in Python.
Function to extract the maximum and minimum values ​​in a slice with Go
I created a tool to correct GPS data with Mapbox Map Matching API (Mapbox Map Matching API)
I got an error when trying to run Hello World in Go language
I tried to make a castle search API with Elasticsearch + Sudachi + Go + echo
I tried to implement what seems to be a Windows snipping tool in Python
A memo that I wrote a quicksort in Python
I want to create a window in Python
I tried to integrate with Keras in TFv1.1
I want to make a game with Python
I wrote a class in Python3 and Java
Clone with a specific branch / tag in GitPython
I wrote a design pattern in kotlin Prototype
I made a tool to compile Hy natively
I played with Diamond, a metrics collection tool
I made a tool to get new articles
I want to write to a file with Python
Create a web server in Go language (net/http) (2)
I tried to build an environment with WSL + Ubuntu + VS Code in a Windows environment
I got stuck when trying to specify a relative path with relative_to () in python
I want to save a file with "Do not compress images in file" set in OpenPyXL
How to test the current time with Go (I made a very thin library)
I tried to create a class to search files with Python's Glob method in VBA
I wrote Python code to create a table (view) dependency diagram (PlantUML) from SQL
How to convert / restore a string with [] in python
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I tried to create a table only with Django
Try to make a Python module in C language
I wrote a design pattern in kotlin Factory edition
Try to make a command standby tool with python