Skip to content

Instantly share code, notes, and snippets.

@shurizzle
Created July 17, 2011 23:22
Show Gist options
  • Save shurizzle/1088225 to your computer and use it in GitHub Desktop.
Save shurizzle/1088225 to your computer and use it in GitHub Desktop.
Megavideo simple link generator (just for utility)
require 'cgi'
require 'webrick'
require 'net/http'
class Index < WEBrick::HTTPServlet::AbstractServlet
INDEX = DATA.read
def do_GET(request, response)
response.status = 200
response['Content-Type'] = "text/html"
response.body = INDEX
end
end
class LinkGenerator < WEBrick::HTTPServlet::AbstractServlet
def do_POST(request, response)
response.status = 200
response['Content-Type'] = 'text/plain'
response.body = generate(request)
end
def do_GET(request, response)
response.status = 200
response['Content-Type'] = 'text/plain'
response.body = 'No link given.'
end
def generate(request)
return 'No link given.' unless request.query['link']
link = request.query['link']
if link !~ /^[a-z0-9]{8}$/i
link = link.match(/\?((d|v)=[a-zA-Z0-9]{8})/)[0]
if link.start_with?('?d')
link = Net::HTTP.get(URI.parse("http://www.megavideo.com/#{link}")).match(/\?v=([a-zA-Z0-9]{8})&/)[1]
else
link = link[-8..-1]
end
else
return 'Invalid link.'
end
res = Net::HTTP.post_form(URI.parse('http://www.megavideo.com/?c=login&next='), {
'login' => '1',
'next' => '',
'username' => '******',
'password' => '******'
})
URI.encode(CGI.unescape(Net::HTTP.get(URI.parse("http://www.megavideo.com/xml/player_login.php?u=%s&v=%s" % [
res['set-cookie'].match(/user=([^;]+);/)[1],
link
].map {|x| CGI.escape(x) })).match(/downloadurl="(.+?)" /)[1]))
rescue
return 'Invalid link.'
end
end
class PlaylistGenerator < WEBrick::HTTPServlet::AbstractServlet
def do_GET(request, response)
response.status = 200
response['Content-Type'] = 'text/plain'
response.body = 'Link not given.'
end
def do_POST(request, response)
response.status = 200
if request.query['link']
response['Content-Type'] = 'audio/x-scpls'
response.body = <<EOF
[playlist]
numberofentries=1
File1=#{request.query['link']}
Title1=#{URI.unescape(request.query['link'].split('/').last.gsub(/\.[^\.]+$/, ''))}
Version=2
EOF
else
response['Content-Type'] = 'text/plain'
response.body = 'No link given.'
end
end
end
server = WEBrick::HTTPServer.new(Port: 8000, BindAddress: '0.0.0.0')
server.mount '/', Index
server.mount '/generate', LinkGenerator
server.mount '/playlist.pls', PlaylistGenerator
trap(:INT) { server.shutdown }
server.start
__END__
<html>
<head>
<title>Megavideo Generator</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/prototype/1.7/prototype.js"></script>
<style>
html, body, input {
font-family: arial;
width: 100%;
margin: 0;
}
input {
font-size: 30px;
}
input, #result {
text-align: center;
}
</style>
</head>
<script>
Event.observe(window, 'load', function() {
Event.observe('form', 'submit', function(event) {
$('result').update('Generating, please wait...');
event.stop();
new Ajax.Request($('form')['action'], {
method: 'post',
parameters: $('form').serialize(true),
asynchronous: false,
onSuccess: function(res) {
$('result').update(('#{link}<br /><form id="playlist" method="POS'+
'T" action="playlist.pls"><input type="hidden" name="link" valu'+
'e="#{link}" /><a href="#" onclick="$(\'playlist\').submit(); r'+
'eturn false;">Playlist</a></form>').interpolate({link: res.responseText}));
}
});
$('form').reset();
});
})
</script>
<body>
<form id="form" method="POST" action="/generate">
<input type="text" name="link">
<input type="submit" value="Genera">
</form>
<div id="result"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment