-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(exporter): PDF and MD support with txtpaper
- Loading branch information
Showing
4 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package txtpaper | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"time" | ||
|
||
"github.com/ncarlier/readflow/pkg/exporter" | ||
"github.com/ncarlier/readflow/pkg/model" | ||
) | ||
|
||
const txtpaperURL = "https://txtpaper.com/api/v1/" | ||
|
||
// TxtpaperExporter convert an article using txtpaper service | ||
type TxtpaperExporter struct { | ||
format string | ||
} | ||
|
||
func newTxtpaperExporter(format string) func(downloader exporter.Downloader) (exporter.ArticleExporter, error) { | ||
return func(downloader exporter.Downloader) (exporter.ArticleExporter, error) { | ||
return &TxtpaperExporter{ | ||
format: format, | ||
}, nil | ||
} | ||
} | ||
|
||
// Export an article using txtpaper service | ||
func (exp *TxtpaperExporter) Export(ctx context.Context, article *model.Article) (*model.FileAsset, error) { | ||
form := url.Values{} | ||
form.Add("byline", *article.URL) | ||
form.Add("content", *article.HTML) | ||
form.Add("format", exp.format) | ||
|
||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second) | ||
defer cancel() | ||
req, err := http.NewRequestWithContext(ctx, "POST", txtpaperURL, strings.NewReader(form.Encode())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | ||
res, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer res.Body.Close() | ||
|
||
if res.StatusCode >= 400 { | ||
return nil, fmt.Errorf("invalid txtpaper response: %d", res.StatusCode) | ||
} | ||
|
||
body, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &model.FileAsset{ | ||
Data: body, | ||
ContentType: res.Header.Get("Content-Type"), | ||
Name: strings.TrimRight(article.Title, ". ") + "." + exp.format, | ||
}, nil | ||
} | ||
|
||
func init() { | ||
exporter.Register("pdf", newTxtpaperExporter("pdf")) | ||
exporter.Register("md", newTxtpaperExporter("md")) | ||
exporter.Register("mobi", newTxtpaperExporter("mobi")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters