-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
archive/ted/cmd/order: Add ordered version of injection.
- Loading branch information
Showing
1 changed file
with
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// order is a tool which satisfies a TED talk addiction in an ordered fashion. | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
|
||
"github.com/mewmew/playground/archive/ted" | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
for _, path := range flag.Args() { | ||
talks, err := parse(path) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
order(talks) | ||
} | ||
} | ||
|
||
// order outputs one wget script for downloading TED talks per release month. | ||
func order(talks []ted.Talk) { | ||
var prev string | ||
var w io.Writer | ||
for _, talk := range talks { | ||
date := talk.Date.Format("2006-01") | ||
if prev == "" || prev != date { | ||
f, err := os.Create(fmt.Sprintf("%s.sh", date)) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
err = f.Chmod(0755) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
defer f.Close() | ||
w = f | ||
} | ||
fmt.Fprintf(w, "wget -O \"%s - %s (%s) [%v].mp4\" %s\n", date, talk.Title, talk.Event, talk.Duration, talk.Download) | ||
prev = date | ||
} | ||
} | ||
|
||
// parse parses the provided JSON file of TED talks. | ||
func parse(path string) (talks []ted.Talk, err error) { | ||
buf, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = json.Unmarshal(buf, &talks) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return talks, nil | ||
} |