找回密码
 FreeOZ用户注册
查看: 3546|回复: 10
打印 上一主题 下一主题

[language study] BBC podcast 下载&播放脚本分享(Linux用户专用)

[复制链接]
跳转到指定楼层
1#
发表于 30-11-2014 12:40:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?FreeOZ用户注册

x
本帖最后由 anull 于 1-12-2014 15:11 编辑

鄙人Linux程序员, 最近打算考雅思, 敢情听力是个弱点, 想狂练,于是想到BBC。

虽说 http://www.bbc.co.uk/podcasts 上面可以在线听。但鄙人觉得甚是不方便, 于是
乎写了个简单脚本(见附件)

首先是把你喜欢听的节目写到 "~/.bbc" 文件, 如鄙人的收听列表如下:
  http://www.bbc.co.uk/podcasts/series/thought
  http://www.bbc.co.uk/podcasts/series/tae
  http://www.bbc.co.uk/podcasts/series/6min
  http://www.bbc.co.uk/podcasts/series/6min_vocab
  http://www.bbc.co.uk/podcasts/series/6min_gram
  http://www.bbc.co.uk/podcasts/series/globalnews
  http://www.bbc.co.uk/podcasts/series/cfocus
  http://www.bbc.co.uk/podcasts/series/hitlist
  http://www.bbc.co.uk/podcasts/series/archers
  http://www.bbc.co.uk/podcasts/series/ft
  http://www.bbc.co.uk/podcasts/series/forum
  http://www.bbc.co.uk/podcasts/series/together

之后, 你可运行  “bbc download" 或 "bbc d" 下载。有些节目是每天更新,有
些是每周更新。运行这个脚本会把它们都下下来。

再之后, 你就可以运行 "bbc play" 或 "bbc p" 来播放了。 每运行一次就播放
一个你之前下载过的节目。 再运行就播放下一个节目。

如此这般, 只要把 "bbc download" 放在后端运行, 比如设置成每天运行一次,
它就会把最新的节目都下下来。 你只要执行 “bbc p" 就可以一一收听它们了。
对鄙人来讲, 甚是方便。

bbc

1.5 KB, 下载次数: 15

评分

参与人数 3威望 +150 收起 理由
ubuntuhk + 50 你太有才了!
yearshappy + 50 赞一个!
大叶树 + 50 你太有才了!

查看全部评分

回复  

使用道具 举报

2#
发表于 30-11-2014 13:18:44 | 只看该作者
试下去。。。好主意
回复  

使用道具 举报

3#
 楼主| 发表于 30-11-2014 13:50:57 | 只看该作者
大叶树 发表于 30-11-2014 14:18
试下去。。。好主意

没想到你也玩Linux啊
回复  

使用道具 举报

4#
发表于 1-12-2014 10:47:41 | 只看该作者
挺好的

我写了个.net C#的程序,这样就可以在windows 下运行

using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using HtmlAgilityPack;

namespace DownloadBbc {
    internal class Program {
        private static void DownloadFile(string mp3) {
            if (string.IsNullOrWhiteSpace(mp3))
                return;

            var fileName = Path.GetFileName(mp3);

            if (File.Exists(fileName))
                return;

            using (var webClient = new WebClient()) {
                webClient.DownloadFile(mp3, fileName);
            }
        }

        private static async Task RunAsync(string url) {
            using (var client = new HttpClient()) {
                var response = await client.GetAsync(url);
                if (response.IsSuccessStatusCode) {
                    var html = await response.Content.ReadAsStringAsync();
                    var page = new HtmlDocument {OptionAutoCloseOnEnd = false, OptionFixNestedTags = true};
                    page.LoadHtml(html);

                    var mp3s =
                        from mp3 in page.DocumentNode.Descendants("a")
                        where mp3.GetAttributeValue("href", "").EndsWith(".mp3")
                        select mp3.GetAttributeValue("href", "");

                    foreach (var mp3 in mp3s) {
                        Console.WriteLine(mp3);
                        DownloadFile(mp3);
                    }
                }
            }
        }

        private static void Main(string[] args) {
            string line;

            // Read the file and display it line by line.
            var file = new StreamReader("bbc.txt");
            while ((line = file.ReadLine()) != null) {
                Console.WriteLine(line);
                RunAsync(line).Wait();
            }

            file.Close();
        }
    }
}

评分

参与人数 1威望 +50 收起 理由
ubuntuhk + 50 你太有才了!

查看全部评分

回复  

使用道具 举报

5#
发表于 1-12-2014 11:08:24 | 只看该作者
anull的shell script编程能力很强,赞!
回复  

使用道具 举报

6#
发表于 1-12-2014 11:43:06 | 只看该作者
赞!我也在想,要是能写个程序,能够自动去填写NSW州担保申请的页面,是不是胜算会大很多。。。
回复  

使用道具 举报

7#
 楼主| 发表于 1-12-2014 13:01:26 | 只看该作者
ubuntuhk 发表于 1-12-2014 12:08
anull的shell script编程能力很强,赞!

多谢! Linux爱好者而已
回复  

使用道具 举报

8#
发表于 2-12-2014 11:30:00 | 只看该作者
请问windows下的怎么用阿?
回复  

使用道具 举报

9#
发表于 2-12-2014 12:52:46 | 只看该作者
Venkey 发表于 2-12-2014 12:30
请问windows下的怎么用阿?

要用visual studio, sharpdevelop,或者命令行编译下,我不愿意提供二进制编译文件,以免被人说放病毒啦
回复  

使用道具 举报

10#
发表于 2-12-2014 12:53:56 | 只看该作者
本帖最后由 DDD888 于 2-12-2014 20:09 编辑

刚学golang,试着写一个,练练手啦

只要装了go,应该在windows,linux下可以运行 我只测试了windows 7, ubuntu linux 14.10

package main

import (
        "bufio"
        "fmt"
        "io"
    "io/ioutil"
        "log"
        "net/http"
        "os"
        "path/filepath"
        "regexp"       
        "strings"
)

func init() {
        log.SetFlags(log.Lshortfile)
}

func getFileName(url string) (string){
        fName := filepath.Base(url)
        extName := filepath.Ext(url)
        bName := fName[:len(fName)-len(extName)]
        return bName
}

func downloadFromUrl(url string) {
        tokens := strings.Split(url, "/")
        fileName := tokens[len(tokens)-1]
        fmt.Println("Downloading", url, "to", fileName)

        // TODO: check file existence first with io.IsExist
        output, err := os.Create(fileName)
        if err != nil {
                fmt.Println("Error while creating", fileName, "-", err)
                return
        }
        defer output.Close()

        response, err := http.Get(url)
        if err != nil {
                fmt.Println("Error while downloading", url, "-", err)
                return
        }
        defer response.Body.Close()

        n, err := io.Copy(output, response.Body)
        if err != nil {
                fmt.Println("Error while downloading", url, "-", err)
                return
        }

        fmt.Println(n, "bytes downloaded.")
}

func downloadUrl(regularExpression *regexp.Regexp, websiteUrl string) {
    response, err := http.Get(websiteUrl)
    if err != nil {
        fmt.Printf("%s", err)
        os.Exit(1)
    } else {
        defer response.Body.Close()
        contents, err := ioutil.ReadAll(response.Body)
        if err != nil {
            fmt.Printf("%s", err)
            os.Exit(1)
        }
               
                var htmlContents = string(contents)
               
                matches := regularExpression.FindAllString(htmlContents, -1)
                for _, mp3Url := range matches {
                        fmt.Println(mp3Url)

                        var mp3FileName = getFileName(mp3Url)
                        if _, err := os.Stat(mp3FileName); os.IsNotExist(err) {
                                //fmt.Printf("no such file or directory: %s", filename)
                                downloadFromUrl(mp3Url)
                        }               
                }
    }
}

func main() {
        // Open an input file, exit on error.
        inputFile, err := os.Open("bbc.txt")
        if err != nil {
                log.Fatal("Error opening input file:", err)
        }

        var regularExpression = regexp.MustCompile("http://downloads.bbc.co.uk/podcasts/[^.]*.mp3")
        // Closes the file when we leave the scope of the current function,
        // this makes sure we never forget to close the file if the
        // function can exit in multiple places.
        defer inputFile.Close()

        scanner := bufio.NewScanner(inputFile)

        // scanner.Scan() advances to the next token returning false if an error was encountered
        for scanner.Scan() {
                var websiteUrl = scanner.Text()
                fmt.Println(websiteUrl)
                downloadUrl(regularExpression, websiteUrl)
        }

        // When finished scanning if any error other than io.EOF occured
        // it will be returned by scanner.Err().
        if err := scanner.Err(); err != nil {
                log.Fatal(scanner.Err())
        }
}
回复  

使用道具 举报

11#
发表于 11-12-2014 13:28:42 | 只看该作者
Here is the source code to concurrent download url and mp3 files in golang.

downloadMp3FromUrl.go

722 Bytes, 下载次数: 2

downloadUrlContent.go

945 Bytes, 下载次数: 0

getFileNameFromUrl.go

144 Bytes, 下载次数: 0

getHomeDir.go

235 Bytes, 下载次数: 1

getHomeDir_test.go

451 Bytes, 下载次数: 0

getHtmlFromUrl.go

363 Bytes, 下载次数: 0

getHtmlFromUrl_test.go

779 Bytes, 下载次数: 1

init.go

79 Bytes, 下载次数: 0

isFileExists.go

200 Bytes, 下载次数: 0

isFileExists_test.go

1.3 KB, 下载次数: 0

main.go

1.11 KB, 下载次数: 2

bbc.txt

584 Bytes, 下载次数: 0

回复  

使用道具 举报

您需要登录后才可以回帖 登录 | FreeOZ用户注册

本版积分规则

小黑屋|手机版|Archiver|FreeOZ论坛

GMT+10, 20-4-2024 05:02 , Processed in 0.050249 second(s), 28 queries , Gzip On, Redis On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表