Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply

Python - Rapidshare to Megaupload script

 
Thread Tools Search this Thread
Old 02-14-2009, 07:46 PM   #1
aiwarrior
 
Posts: n/a
Default Rapidshare to Megaupload script


I've made this script and would like to have some input and share it
with the community.
I also have a page with some code i produce on my spare time. http://pneves..net
Thanks

# -*- coding: utf-8 -*-
## I Paulo Neves am the owner of this script and i do not allow the
copy or distribution
## of this script without my permission to commercial purposes
## If you have any suggestions please mail me and i will reply and
post the on the site
from __future__ import with_statement
from urlparse import urljoin
import urllib2, urllib
import os, sys
import re
import ConfigParser


urls = []
acc_details = 0
firs_run = 0


def from_rapidshare(url):
'''Check if this is a rapidshare link'''
return (url.startswith("rapidshare.com") or
url.startswith("www.rapidshare.com") or
url.startswith("http://rapidshare.com") or
url.startswith("http://www.rapidshare.com"))


def account_details():
if os.environ.has_key("APPDATA") and os.path.exists(os.environ
["APPDATA"]):
path = os.environ["APPDATA"] + "\\murs.cfg"
else:
path = os.path.expanduser("~") + "\.murs.cfg"

if not os.path.exists(path):
print path
m_user = raw_input("Enter your user name for megaupload: ")
m_password = raw_input("Enter your password for megaupload: ")
r_user = raw_input("Enter your user name for rapidshare: ")
r_password = raw_input("Enter your password for rapidshare: ")
cfg = ConfigParser.SafeConfigParser()
cfg.add_section('Acc_Details')
cfg.set('Acc_Details', 'm_user ', m_user)
cfg.set('Acc_Details', 'm_password', m_password)
cfg.set('Acc_Details', 'r_user', r_user)
cfg.set('Acc_Details', 'r_password', r_password)
with open(path, 'wb') as configfile:
cfg.write(configfile)

cfg = ConfigParser.SafeConfigParser()
cfg.read(path)
try:
m_user = cfg.get("Acc_Details", "m_user")
m_password = cfg.get("Acc_Details", "m_password")
r_user = cfg.get("Acc_Details", "r_user")
r_password = cfg.get("Acc_Details", "r_password")
except ConfigParser.NoSectionError or ConfigParser.NoOptionError:
print "no section or No Option"
print
os.remove(path)
return (m_user, m_password, r_user, r_password)

def cookie_processor(cookie):
cookie = cookie.split("; ")
## cookie = dict( [cookie[x].split("=") for x in xrange ( len
(cookie) ) ] )
## if cookie['user'] != None:
return cookie
## else:
## print "Scheme has changed or authentication failes. Last
option most likely"
## sys.exit()
##

def rs_auth(login):
r_user = login[0]
r_password = login[1]
opener = urllib2.build_opener(urllib2.HTTPSHandler())
urllib2.install_opener( opener )
cred = urllib.urlencode({"login": r_user, "password": r_password})
try:
req = urllib2.urlopen("https://ssl.rapidshare.com/cgi-bin/
premiumzone.cgi", cred)
cookie_rs = cookie_processor( req.headers.get("set-cookie",
"") )
except urllib2.URLError:
print"Some error with the connection ocurred. Try again now or
later"
sys.exit()
#Returns the page if flag is set
return req.read(), cookie_rs


def mu_auth(login):
r_user = login[0]
r_password = login[1]
cred = urllib.urlencode({"login": r_user, "password": r_password})
try:
req = urllib2.urlopen("http://www.megaupload.com", cred)
cookie_mu = cookie_processor( req.headers.get("set-cookie",
"") )

except:
print "Connection failed"
sys.exit()


#returns the authenticated header, in case future changes
specificaly ask the cookie it would be easy
#to change code or even extract it from the header itself as a
dictionary key
return cookie_mu

def set_rscookie_in_musite(cookie_mu, cookie_rs) :
#it doesnt need to check for the cookie because it automaticaly
resets for the new value
cookie_rs = cookie_rs.split("=",1)
header = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain", "cookie":cookie_mu}
params = urllib.urlencode({'domain': "http://rapidshare.com",
'cookiename1': cookie_rs[0], 'cookievalue1': cookie[1] })
req = urllib2.Request("http://www.megaupload.com/multifetch/?
c=cookies", params, header )
print header
print params
#r = urllib2.urlopen(req)
return True

acc_details = account_details()
if len(sys.argv)==1:
print "Try -help for more information"
sys.exit("No arguments")


if sys.argv[1] == "-h" or sys.argv[1] == "–help":
print " > murs http://megaupload.com/?d=FILE1 … http://megaupload.com/?d=FILEN"
print " Download one or several rapidshare links passed as
argument separated by whitespace\n"
print " >murs links.txt"
print " Download a list of links from a file\n"
print " >murs account"
print " Automaticaly downloads the entire contents of your
account"
sys.exit()
## Process the arguments
## AFTER THIS ITS ALL ROLLING


if sys.argv[1] == "account" or sys.argv[1] == "-a":
#Getting the rapidshare user and password respectivelyfrom the
account details already called
print acc_details[2:]
page = rs_auth( acc_details[2:] )
rex = re.findall('''Adliste\["(........|.)"\]\["filename"\] =
"(.*)"''', page[0])
for retrieved_data in rex:
#as there are two matches the retrieved_data is still a
list
urls.append("http://rapidshare.com/files/" + retrieved_data
[0] + "/" + retrieved_data[1])

elif os.path.exists(sys.argv[1]): # If this is a file
from_file = True
l_file = sys.argv[1]
print "Reading list of links from the file " , l_file
print "\n"
list_f = file(l_file, "r")
urls = list_f.readlines()
list_f.close()
if not from_rapidshare(urls):
sys.exit("Urls in text file were not valid")


elif from_rapidshare(sys.argv[1]): #if argument is one or more urls
for i in range(1, len(sys.argv)):
if from_rapidshare(sys.argv[i]):
urls.append(sys.argv[i])
else:
print "This is not valid argument" , sys.argv[1]
sys.exit()
urls = []

cookie_mu = mu_auth(acc_details[:2])
print cookie_mu
headers = {"User Agent": "Mozilla/5.0", "Accept": "text/plain",
"cookie":cookie_mu[0]}
for url in urls:
try:
params = urllib.urlencode({'srcurl': url, 'description':
"something"})
req = urllib2.Request("http://megaupload.com/multifetch/",
params, headers )
r = urllib2.urlopen(req)

except:
print "An error ocurred while trying to talk to megaupload.
Make sure you enter the correct password"
  Reply With Quote
Old 02-14-2009, 09:17 PM   #2
MRAB
 
Posts: n/a
Default Re: Rapidshare to Megaupload script

aiwarrior wrote:
> I've made this script and would like to have some input and share it
> with the community.
> I also have a page with some code i produce on my spare time. http://pneves.net
> Thanks
>

[snip]
>
> def from_rapidshare(url):
> '''Check if this is a rapidshare link'''
> return (url.startswith("rapidshare.com") or
> url.startswith("www.rapidshare.com") or
> url.startswith("http://rapidshare.com") or
> url.startswith("http://www.rapidshare.com"))
>

You can shorten:

s.startswith(x) or s.startswith(y) or ...

to:

s.startswith((x, y, ...))

[snip]
> cfg = ConfigParser.SafeConfigParser()
> cfg.read(path)
> try:
> m_user = cfg.get("Acc_Details", "m_user")
> m_password = cfg.get("Acc_Details", "m_password")
> r_user = cfg.get("Acc_Details", "r_user")
> r_password = cfg.get("Acc_Details", "r_password")
> except ConfigParser.NoSectionError or ConfigParser.NoOptionError:


In order to catch multiple exceptions it should be:

except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):

> print "no section or No Option"
> print
> os.remove(path)
> return (m_user, m_password, r_user, r_password)
>
> def cookie_processor(cookie):
> cookie = cookie.split("; ")
> ## cookie = dict( [cookie[x].split("=") for x in xrange ( len
> (cookie) ) ] )

Or:
## cookie = dict(x.split("=") for x in cookie)

> ## if cookie['user'] != None:

When checking for None use "is" and "is not":
## if cookie['user'] is not None:

> return cookie
> ## else:
> ## print "Scheme has changed or authentication failes. Last
> option most likely"
> ## sys.exit()

Raising an exception is better than exiting.

> ##
>

[snip]
> def mu_auth(login):
> r_user = login[0]
> r_password = login[1]
> cred = urllib.urlencode({"login": r_user, "password": r_password})
> try:
> req = urllib2.urlopen("http://www.megaupload.com", cred)
> cookie_mu = cookie_processor( req.headers.get("set-cookie",
> "") )
>
> except:

Don't use an empty "except"; be explicit in which exception you want to
catch .

[snip]
>
> if sys.argv[1] == "-h" or sys.argv[1] == "–help":

Or:

if sys.argv[1] in ("-h", "–help"):

[snip]

> elif from_rapidshare(sys.argv[1]): #if argument is one or more urls
> for i in range(1, len(sys.argv)):

Or:

for arg in sys.argv[1 : ]:

and so on.

> if from_rapidshare(sys.argv[i]):
> urls.append(sys.argv[i])
> else:
> print "This is not valid argument" , sys.argv[1]
> sys.exit()
> urls = []
>

[snip]
HTH
  Reply With Quote
Old 02-15-2009, 05:33 PM   #3
aiwarrior
 
Posts: n/a
Default Re: Rapidshare to Megaupload script

Thanks a lot for your input i really needed because i realized these
are minor flaws but even so define whether its good or bad code and i
really need to improve that. I already implemented the changes you
suggested and this one,

> cookie = dict(x.split("=") for x in cookie)


for me is just very good and really is elegant.
An aspect you didn't mention was the
> urls.append("http://rapidshare.com/files/" + retrieved_data[0] + "/" + retrieved_data[1])

I think its very hackish and crude but urlparser doesnt seem to accept
more than one argument at a time and doing it in a loop seem worse
than the current solution. What would you do?
Thanks a lot again
  Reply With Quote
Old 02-15-2009, 05:33 PM   #4
aiwarrior
 
Posts: n/a
Default Re: Rapidshare to Megaupload script

Thanks a lot for your input i really needed because i realized these
are minor flaws but even so define whether its good or bad code and i
really need to improve that. I already implemented the changes you
suggested and this one,

> cookie = dict(x.split("=") for x in cookie)


for me is just very good and really is elegant.
An aspect you didn't mention was the
> urls.append("http://rapidshare.com/files/" + retrieved_data[0] + "/" + retrieved_data[1])

I think its very hackish and crude but urlparser doesnt seem to accept
more than one argument at a time and doing it in a loop seem worse
than the current solution. What would you do?
Thanks a lot again
  Reply With Quote
Old 02-15-2009, 06:10 PM   #5
MRAB
 
Posts: n/a
Default Re: Rapidshare to Megaupload script

aiwarrior wrote:
> Thanks a lot for your input i really needed because i realized these
> are minor flaws but even so define whether its good or bad code and i
> really need to improve that. I already implemented the changes you
> suggested and this one,
>
>> cookie = dict(x.split("=") for x in cookie)

>
> for me is just very good and really is elegant. An aspect you didn't
> mention was the
>> urls.append("http://rapidshare.com/files/" + retrieved_data[0] +
>> "/" + retrieved_data[1])

> I think its very hackish and crude but urlparser doesnt seem to
> accept more than one argument at a time and doing it in a loop seem
> worse than the current solution. What would you do?
>

re.findall() returns a list of tuples, so retrieved_data is a tuple and
the following will work:

urls.append("http://rapidshare.com/files/%s/%s" % retrieved_data)
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
help with master page and java script alain23 Software 0 09-20-2007 05:02 AM
Megaupload Free & Rapidshare undergroundforum.co.nr Hardware 1 05-13-2007 01:32 PM
A lot of files for download - Rapidshare links - New Files (11.out.2006) Lucas22 DVD Video 0 10-12-2006 02:53 AM
28 videos for download - rapidshare links Lucas22 DVD Video 0 10-09-2006 08:25 PM
scarface poster made out of entire script JimRibs@gmail.com DVD Video 0 11-13-2005 08:20 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47