init
This commit is contained in:
21
habr/api.v
Normal file
21
habr/api.v
Normal file
@ -0,0 +1,21 @@
|
||||
module habr
|
||||
|
||||
import net.http
|
||||
|
||||
pub struct Habr {
|
||||
baseurl string = $d('habr_baseurl', 'https://habr.com')
|
||||
}
|
||||
|
||||
pub fn Habr.new() Habr {
|
||||
return Habr{}
|
||||
}
|
||||
|
||||
pub fn (h Habr) get_article(id int) !string {
|
||||
response := http.get('${h.baseurl}/kek/v2/articles/${id}/') or { return err }
|
||||
return response.body
|
||||
}
|
||||
|
||||
pub fn (h Habr) get_article_comments(id int) !string {
|
||||
response := http.get('${h.baseurl}/kek/v2/articles/${id}/comments/') or { return err }
|
||||
return response.body
|
||||
}
|
65
habr/article.v
Normal file
65
habr/article.v
Normal file
@ -0,0 +1,65 @@
|
||||
module habr
|
||||
|
||||
import json
|
||||
import veb { RawHtml }
|
||||
|
||||
pub struct Article {
|
||||
pub:
|
||||
published_at string @[json: timePublished]
|
||||
title string @[json: titleHtml]
|
||||
text RawHtml @[json: textHtml]
|
||||
hubs []Hub
|
||||
tags []Tag
|
||||
}
|
||||
|
||||
pub fn Article.parse(input string) Article {
|
||||
return json.decode(Article, input) or { Article{} }
|
||||
}
|
||||
|
||||
pub struct Hub {
|
||||
pub:
|
||||
id string
|
||||
alias string
|
||||
title string @[json: titleHtml]
|
||||
}
|
||||
|
||||
pub struct Tag {
|
||||
pub:
|
||||
title string @[json: titleHtml]
|
||||
}
|
||||
|
||||
pub struct Comment {
|
||||
pub:
|
||||
id string
|
||||
parent_id string @[json: parentId]
|
||||
replies_ids []string @[json: children]
|
||||
level int
|
||||
author CommentAuthor
|
||||
message RawHtml
|
||||
}
|
||||
|
||||
pub struct CommentAuthor {
|
||||
pub:
|
||||
alias string
|
||||
}
|
||||
|
||||
struct CommentsMapped {
|
||||
mut:
|
||||
items map[string]Comment @[json: comments]
|
||||
}
|
||||
|
||||
pub struct Comments {
|
||||
pub mut:
|
||||
items []Comment
|
||||
mut:
|
||||
idx int
|
||||
}
|
||||
|
||||
pub fn Comments.parse(input string) Comments {
|
||||
mut comments := Comments{}
|
||||
mapped := json.decode(CommentsMapped, input) or { CommentsMapped{} }
|
||||
for _, v in mapped.items {
|
||||
comments.items << v
|
||||
}
|
||||
return comments
|
||||
}
|
12
habr/util.v
Normal file
12
habr/util.v
Normal file
@ -0,0 +1,12 @@
|
||||
module habr
|
||||
|
||||
import regex
|
||||
|
||||
pub fn get_id_from_url(url string) ?string {
|
||||
mut re := regex.regex_opt(r'/\d+/?$') or { return none }
|
||||
begin, end := re.find(url)
|
||||
if begin > 0 && end > 0 {
|
||||
return url[begin..end].trim('/')
|
||||
}
|
||||
return none
|
||||
}
|
Reference in New Issue
Block a user