Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature add mp3 #22

Merged
merged 2 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.xml
config.json
*.mp3
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
MDRSS is a markdown to RSS converter written in GO. With this tool, you can write articles in a local folder and have them automatically formatted to an RSS compliant XML file. As a result, you don't have to write articles on your website first and have them be read by an RSS reader. Moreover, MDRSS automatically takes care of publication dates, categories (next update), formatting, etc.

### Getting started
You can install the binary using `go install github.com/TimoKats/mdrss@latest` (make sure your GOPATH is set correctly!). After this you can add your configuration in `~/.mdrss/config.json`. This is a JSON file with a number of settings. You can use this template to setup your configuration. Note, the most recent update also allows users to specify their own config path, which you can define like so.

```shell
mdrss --config other/path/to/config update
```
You can install the binary using `go install github.com/TimoKats/mdrss@latest` (make sure your GOPATH is set correctly!). After this you can add your configuration in `~/.mdrss/config.json`. This is a JSON file with a number of settings. You can use this template to setup your configuration.

```JSON
{
Expand All @@ -19,7 +15,18 @@ mdrss --config other/path/to/config update
}
```

In your input folder, you can add an article by creating a `.md` file. **Note, if your filename is prefixed with `draft-` it will not be included in the RSS file**. Finally, you can type `mdrss ls` to view the articles ready for publishing and `mdrss update` to create the RSS feed. Note, the title of the RSS articles are based on markdown headers. E.g. format the first line of the markdown articles as so: `# this is a title`.
Note, the most recent update also allows users to specify their own config path, which you can define like so.
```shell
mdrss --config other/path/to/config update
```

In your input folder, you can add an article by creating a `.md` file. **Note, if your filename is prefixed with `draft-` it will not be included in the RSS file**. Finally, you can type `mdrss ls` to view the articles ready for publishing and `mdrss update` to create the RSS feed. Note, the title of the RSS articles are based on markdown headers. E.g. format the first line of the markdown articles as so: `# this is a title`.

Mdrss also supports the usage of RSS enclosures (relevant for podcasts). To create an enclosure link: create a regular markdown link to the audio file with the identifier as "audio/mpeg".

```markdown
[audio/mpeg](https://link.to.mp3/file.mp3)
```

### Publishing the RSS file
You can submit your RSS file to a public website, or put it somewhere easily available on a public drive, apache server, etc. Note, there is a chance I will make a function for this on my website so that people who can't host RSS feeds themselves can still publish their feeds. Please let me know if you're interested in such a service.
Expand All @@ -29,7 +36,6 @@ List of features that are on the planning. Feel free to send suggestions here.
* Display source code from markdown more correctly.
* For non textual RSS readers: render images.
* Ordered lists
* RSS enclosures for podcasts

### Why RSS?
* No ads/trackers/cookies.
Expand Down
37 changes: 37 additions & 0 deletions lib/filesize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package lib

import (
"unicode"
"strings"
"os/exec"
"errors"
)

func extractInt(text string) string {
filteredString := ""
for _, character := range text {
if unicode.IsDigit(character) {
filteredString += string(character)
}
}
if len(filteredString) == 0 {
return "1"
} else {
return filteredString
}
}

func FileSizeUrl(url string) (string, error) {
cmd := exec.Command("curl", "-sIL", url)
cmdOutput, cmdErr := cmd.Output()
if cmdErr != nil {
return "1", errors.New("Issues when running curl to get filesize.")
} else {
for _, line := range strings.Split(string(cmdOutput), "\n") {
if strings.Contains(line, "content-length") {
return extractInt(line), nil
}
}
}
return "1", errors.New("No content-length found for url")
}
15 changes: 14 additions & 1 deletion lib/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ func convertMarkdownLink(text string, markdownLinks *regexp.Regexp) string {
return "<p>" + markdownLinks.ReplaceAllString(text, "<a href='$2'>$1</a>") + "</p>"
}

func convertMarkdownEnclosure(text string, markdownLinks *regexp.Regexp) string {
url := markdownLinks.ReplaceAllString(text, "$2")
size, fileSizeErr := FileSizeUrl(url)
if fileSizeErr != nil {
Error.Println(fileSizeErr)
return ""
}
return "<enclosure " + markdownLinks.ReplaceAllString(text, "url='$2' type='$1' length='") + size + "' />"
}

func convertMarkdownList(text string) string {
if !markdownListActive {
markdownListActive = true
Expand All @@ -42,6 +52,9 @@ func convertMarkdownList(text string) string {
func ConvertMarkdownToRSS(text string) string {
markdownLinks := regexp.MustCompile(`\[(.*)\]\((.*)\)`)
markdownLists := regexp.MustCompile(`^(\s*)(-|\*|\+)(.*)`)
if markdownLinks.Match([]byte(text)) && strings.Contains(text, "audio/mpeg") {
return convertMarkdownEnclosure(text, markdownLinks)
}
if markdownLinks.Match([]byte(text)) {
return convertMarkdownLink(text, markdownLinks)
}
Expand All @@ -59,7 +72,7 @@ func GetArticles(config Config) ([]Article, error) {
articles := []Article{}
if fileErr == nil {
for _, file := range RawArticles {
if !file.IsDir() && !strings.HasPrefix(file.Name(), "draft-") {
if !file.IsDir() && !strings.HasPrefix(file.Name(), "draft-") && strings.HasSuffix(file.Name(), ".md") {
var article Article
fileInfo, _ := file.Info()
article.Filename = file.Name()
Expand Down
18 changes: 18 additions & 0 deletions mdrss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ func TestConvertLinks(t *testing.T) {
}
}

func TestEnclosures(t *testing.T) {
got, fileSizeErr := mdrss.FileSizeUrl("https://file-examples.com/storage/fec2f2681466d1d67a0683d/2017/11/file_example_MP3_700KB.mp3")
want := "733645"
if got != want {
t.Errorf("got %v, wanted %v", got, want)
}
if fileSizeErr != nil {
t.Errorf("not good %v", fileSizeErr)
}
}

func TestFakeEnclosures(t *testing.T) {
_, fileSizeErr := mdrss.FileSizeUrl("hello.mp3")
if fileSizeErr == nil {
t.Errorf("Should give Error, but no.")
}
}

func TestBasics(t *testing.T) {
config := testConfig()
articles, _ := mdrss.GetArticles(config)
Expand Down
2 changes: 2 additions & 0 deletions test/another-article.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

Hi all. I've added my public PGP key to the files section of this website. It's linked to my main e-mail address (hello[at]timokats[dot]xyz).

[audio/mpeg](https://www.native-instruments.com/fileadmin/ni_media/producer/koresoundpack/fm8transientattacks/audio/1_FM8ROXXS.mp3)

Note, for me it's not a hard requirement to use it when sending me an email. In fact, it's mainly there for those that prefer/require encrypted correspondence when contacting me.

Timo
Expand Down
Loading