go-readability/main.go

62 lines
1.4 KiB
Go
Raw Normal View History

package main
/*
#include <stdlib.h>
typedef struct {
char* title;
char* author;
int length;
char* excerpt;
char* site_name;
char* image_url;
char* favicon_url;
char* content; // HTML content
char* text_content; // text content
char* language;
char* published_time;
} CArticle;
*/
import "C"
import (
"time"
"unsafe"
"github.com/go-shiori/go-readability"
)
//export Parse
func Parse(url string) (result C.CArticle, errorMsg *C.char) {
article, err := readability.FromURL(url, 30*time.Second)
if err != nil {
return C.CArticle{}, C.CString(err.Error())
}
return C.CArticle{
title: C.CString(article.Title),
author: C.CString(article.Byline),
length: C.int(article.Length),
excerpt: C.CString(article.Excerpt),
site_name: C.CString(article.SiteName),
image_url: C.CString(article.Image),
favicon_url: C.CString(article.Favicon),
content: C.CString(article.Content),
text_content: C.CString(article.TextContent),
}, nil
}
//export FreeArticle
func FreeArticle(article C.CArticle) {
C.free(unsafe.Pointer(article.title))
C.free(unsafe.Pointer(article.author))
C.free(unsafe.Pointer(article.excerpt))
C.free(unsafe.Pointer(article.site_name))
C.free(unsafe.Pointer(article.image_url))
C.free(unsafe.Pointer(article.favicon_url))
C.free(unsafe.Pointer(article.content))
C.free(unsafe.Pointer(article.text_content))
}
func main() {
}