Skip to content

Instantly share code, notes, and snippets.

@alexisrobert
Created June 4, 2011 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexisrobert/1008489 to your computer and use it in GitHub Desktop.
Save alexisrobert/1008489 to your computer and use it in GitHub Desktop.
Fetch the original video file's URL on Megavideo (premium accounts) without getting through the Flash app.
/*
* Fetch the original video file's URL on Megavideo (premium accounts) without getting
* through the Flash app.
*
* Copyright (c) 2011 Alexis ROBERT <alexis.robert@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* Usage :
* Create a config.json file in the current directory like the following :
* {"username": "blah", "password": "blah"}
*
* Then, execute ./fetch-megavideo VIDEOID
*
* VIDEOID is the ?v= part of Megavideo links.
*/
package main
import "http"
import "xml"
import "fmt"
import "os"
import "json"
import "path"
import "flag"
type Data struct {
XMLName xml.Name "user"
User string "attr"
Downloadurl string "attr"
}
type Config struct {
Username string
Password string
}
// Get the url from a specific videoid given an userid.
func download(videoid string, userid string) string {
c := new(http.Client)
r, _, e := c.Get("http://www.megavideo.com/xml/player_login.php?u=" + userid + "&v=" + videoid + "&password=")
if e != nil {
panic(e)
}
result := Data{User: "", Downloadurl: ""}
e = xml.Unmarshal(r.Body, &result)
if e != nil {
panic(e)
}
url, e := http.URLUnescape(result.Downloadurl)
if e != nil {
panic(e)
}
if len(url) == 0 {
fmt.Fprintf(os.Stderr, "Video not found !\n")
os.Exit(1)
}
return url
}
// Open and parses config file
func openconfig(path string) Config {
f, e := os.Open(path)
if e != nil {
panic(e)
}
d := json.NewDecoder(f)
config := Config{}
d.Decode(&config)
return config
}
// Logs the user and fetch the cookie which contains the user's userid
func login(username string, password string) string {
c := new(http.Client)
r, e := c.PostForm("http://www.megavideo.com/?c=login", map[string]string{
"login": "1", "redir": "1", "username": username, "password": password})
if e != nil {
panic(e)
}
if len(r.SetCookie) > 1 {
fmt.Fprintf(os.Stderr, "Error : more than one cookie defined. Maybe the protocol changed.")
os.Exit(1)
}
return r.SetCookie[0].Value
}
func printUsage() {
fmt.Fprintf(os.Stderr, "Usage: %s [videoid]\n", os.Args[0])
flag.PrintDefaults()
}
func main() {
trailer := flag.Bool("p", false, "Outputs \"url\" instead of url")
flag.Usage = printUsage
flag.Parse()
configpath, _ := path.Split(os.Args[0])
if flag.NArg() == 0 {
flag.Usage()
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "Logging ...\n")
config := openconfig(path.Join(configpath, "config.json"))
userid := login(config.Username, config.Password)
url := download(flag.Arg(0), userid)
if *trailer == true {
url = "\""+url+"\""
}
fmt.Printf("%s\n", url)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment