Expand Cut Tags

No cut tags
cbrachyrhynchos: (Default)
[personal profile] cbrachyrhynchos

News came into my inbox yesterday that imzy is shutting down. Some people are moving to imzy @ Dreamwidth. I have mixed feelings about this because while I think Dreamwidth has a much better community model than tumblr or twitter, the interface is still the old livejournal template with crappy scaling to mobile.

But, I decided I might as well look at how to syndicate content from bigeekfan to my long-disused cbrachyrhynchos@dreamwidth page. It turns out that it's not that difficult since Dreamwidth supports posting by email with markdown support. So the process is:

  1. load my markdown files
  2. translate absolute paths to full urls
  3. pull the title from the YAML header
  4. separate the YAML header from the body
  5. send the email

Here's the code (identifying information removed).

#lang racket
(require net/smtp)
(require net/head)
(require openssl)

(define smtp-pass "")
(define smtp-user "")
(define smtp-server "")
(define smtp-port 465)
(define base-url "https://www.bigeekfan.com/")
(define dreamwidth-address "user+pin@post.dreamwidth.org")

;; load the file into a string
(define (load-file filename)
  (file->string filename))

;;; create the email header using the recipient list 
;;; and the subject 
(define (create-header recipient-list subject)
  (standard-message-header smtp-user

;;; split the body into a list of lines
(define (email-body html-string)
  (string-split html-string "\n"))

;;; send an email message to recipient-list with the subject
;;; and the body.
(define (send-message recipient-list subject body-string)
   (create-header recipient-list subject)
   (create-email-body body-string)
   #:port-no smtp-port
   #:auth-user smtp-user
   #:auth-passwd smtp-pass
   #:tcp-connect ssl-connect))

;; clean up relative links
(define (clean-relative-links string base-url)
  (string-replace string "](https://www.bigeekfan.com/"
                  (string-join (list "](" base-url) "")))

;; pull the title using regex
(define (get-title string)
  (for/first ([line (string-split string "\n")]
              #:when (regexp-match? #rx"title:" line))
    (second (regexp-match #rx"\"(.*)\"" line))))

;; get the the position of the start of the body of the post
(define (get-body-index string)
  (cdr (first (regexp-match-positions #rx"\n---\n\n" string))))

;; get the body
(define (get-body string)
  (substring string (get-body-index string)))

;;; combine all of the above functions to create an email body
;;; as a list of strings including the !markdown directive for
;;; dreamwidth. 
(define (create-email-body string)
  (append (list "!markdown" "") (email-body (get-body (clean-relative-links string base-url)))))

;;; send a hugo markdown file to recipient list
(define (send-markdown-file recipient-list filename)
  (define text (load-file filename))
  (define subject (get-title text))
  (send-message recipient-list subject text))

;;; command line argument parsing
(define filename (make-parameter #f))

(when (< 0 (vector-length (current-command-line-arguments)))
  (filename (last (vector->list (current-command-line-arguments)))))

;;; if there's a command-line option, send the file.
(if (filename)
    (send-markdown-file (list smtp-user dreamwidth-address) (filename))
    (display "No file."))

This will probably need some modification to work with other markdown blogs. You'll want to use an app password if you're hard-coding the smtp password into the script.


cbrachyrhynchos: (Default)

July 2017

234 5678
91011 1213 1415
16 17 1819202122
23 2425 26272829

Most Popular Tags

Style Credit

Page generated Jul. 28th, 2017 07:01 pm
Powered by Dreamwidth Studios