Skip to content

Instantly share code, notes, and snippets.

@adamdavislee
Created September 23, 2017 17:00
Show Gist options
  • Save adamdavislee/98d942935c5f8adeb70bcee95a5964c4 to your computer and use it in GitHub Desktop.
Save adamdavislee/98d942935c5f8adeb70bcee95a5964c4 to your computer and use it in GitHub Desktop.
(ns adamdavislee.mpd.main
(:require [neko.activity
:refer [defactivity set-content-view!]]
[neko.debug :refer [*a]]
[neko.notify
:refer [toast fire notification]]
[neko.resource :as res]
[neko.context
:refer [get-service]]
[neko.threading :refer [on-ui]]
[clojure.xml :as xml]
[clojure.string :as str]
[clojure.java.io :as io]
[clojure.edn :as edn])
(:import android.widget.EditText
java.util.concurrent.TimeUnit
java.util.concurrent.Executors
java.util.Date
java.util.Calendar
android.app.AlarmManager
android.content.Context
android.content.Intent
android.app.PendingIntent
android.widget.Toast
android.content.BroadcastReceiver
java.text.SimpleDateFormat
android.os.SystemClock
android.app.IntentService))
(res/import-all)
(defn my-toast
[& messages]
(on-ui
(toast
(*a)
(apply str messages)
:short)))
(defn my-podcast-notify
[filename]
(on-ui
(fire
:id (notification
{:icon R$drawable/ic_launcher
:ticker-text "Finished Downloading!"
:content-title (str
"Downloaded "
(filename podcast))
:content-text (str
(mpddir)
(filename podcast))
:action [:activity "adamdavislee.mpd.Activity"]}))))
(defn mpddir
[]
(str
(android.os.Environment/getExternalStorageDirectory)
"/menlo-podcast-downloader/"))
(defn download-podcast
[podcast]
(future
(.mkdir (io/file (mpddir)))
(with-open
[in
(clojure.java.io/input-stream
(:url podcast))
out
(clojure.java.io/output-stream
(str
(mpddir)
"."
(filename podcast)))]
(clojure.java.io/copy in out))
(.renameTo
(clojure.java.io/file
(str
(mpddir)
"."
(filename podcast)))
(clojure.java.io/file
(str
(mpddir)
(filename podcast))))
(my-podcast-notify podcast)))
(defn filename
[podcast]
(str
(:day (:date podcast))
"-"
(:month (:date podcast))
"-"
(:year (:date podcast))
" - "
(str/replace
(:title podcast)
#" \| "
" - ")
".mp3"))
(defn items
[feed]
(as->
@feed
it
(it :content)
(first it)
(it :content)
(filter
#(some #{[:tag :item]} %)
it)))
(defn item-to-url
[item]
(->> item
:content (filter
#(= (first %) [:tag :enclosure]))
first
:attrs :url))
(defn item-to-title
[item]
(->> item
:content (filter
#(= (first %) [:tag :title]))
first
:content first))
(defn item-to-date
[item]
(->> item
:content (filter
#(= (first %) [:tag :pubDate]))
first
:content first))
(def parse-month-from-feed
{"jun" 6,
"sep" 9,
"feb" 2,
"jan" 1,
"apr" 4,
"nov" 11,
"mar" 3,
"dec" 12,
"oct" 10,
"may" 5,
"aug" 8,
"jul"
7})
(defn parse-date-from-feed
[date]
{:day (Integer.
(first (re-seq #"\d+" date)))
:month (parse-month-from-feed
(apply
str
(take
3
(.toLowerCase
((vec (re-seq #"\w+" date)) 2)))))
:year (Integer.
((vec (re-seq #"\d+" date)) 1))})
(defn parsed-items
[feed]
(filter
#(:url %)
(map
(fn [item]
{:url (item-to-url item)
:date ((comp
parse-date-from-feed
item-to-date)
item)
:title (item-to-title item)})
(items feed))))
(defn download-podcasts
[]
(dorun
(map
download-podcast
(filter
(fn [parsed-item]
(not (some
(set [(filename parsed-item)])
(vec
(.list
(clojure.java.io/file (mpddir)))))))
(parsed-items
(future
(xml/parse
"http://podcast.menlo.church/feed/")))))))
(defactivity
adamdavislee.mpd.Activity
:key :main (onCreate
[this bundle]
(.superOnCreate this bundle)
(res/import-all)
(on-ui
(set-content-view!
(*a)
[:linear-layout {:orientation :vertical :layout-width :fill :layout-height :wrap}
[:button {:text "Download All Podcasts"
:layout-width :fill :height 200
:layout-margin-top 100
:on-click (fn [_]
(my-toast
"Looking for New Podcasts...")
(download-podcasts))}]]))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment