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.
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).
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.
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>
======================
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)
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.
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.
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 ↓
Recommended Posts