home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / iria107a.lzh / script / gaogaolib.py < prev    next >
Encoding:
Text File  |  2000-11-25  |  4.0 KB  |  158 lines

  1.  
  2. #name: gaogao libraly
  3. #author: Wolfy
  4. #homepage: http://gaogao.tripod.co.jp/
  5.  
  6. import sys,httplib,re,urlparse,string,os.path
  7.  
  8. #const
  9. TRUE = 1
  10. FALSE = 0
  11.  
  12. ERROR = 0     #Ä╕ös
  13. SUCCESS = 1   #É│ÅφÅIù╣
  14. TERMINATE = 2 #âXâîâbâhé≡ÆΓÄ~é╡é▄é╖
  15. IGNORE = 3    #îïë╩é═û│Äïé│éΩé▄é╖
  16. TERMINATE_SUCCESS = 4 #âXâîâbâhé≡ï¡ÉºôIé╔ɼî≈ÅIù╣é╡é▄é╖
  17. EXIT = 5 #ûóÄ└ìsé╠âXâNâèâvâgé¬éáé┴é─éαâCâxâôâgé≡ÅIù╣
  18. SUCCESS_EXIT = 6 #ûóÄ└ìsé╠âXâNâèâvâgé¬éáé┴é─éαâCâxâôâgé≡É│ÅφÅIù╣
  19.  
  20. def parse_url(url,protocol = ''):
  21.   r'''ò╘Ælé═urlÅεò±é╠ôⁿé┴é╜Ľſ protocol://user:pass@host:port/path?query'''
  22.   u = {'protocol':'http','host':'','port':'','user':'','password':'','path':'/','dir':'/','filename':'index.html','query':''}
  23.   u['url'] = url
  24.   tmp = url
  25.   protocol = string.lower(protocol)
  26.   #protocolé≡ĵéΦÅoé╖
  27.   idx = string.find(tmp,'://')
  28.   if idx == -1:
  29.     u['protocol'] = protocol
  30.   else:
  31.     u['protocol'] = tmp[0:idx]
  32.     tmp = tmp[idx + 3:]
  33.   #host
  34.   idx = string.find(tmp,'/')
  35.   #pathé¬û│é»éΩé╬
  36.   if idx == -1:
  37.     host = tmp[0:]
  38.     tmp = '/'
  39.   else: #pathé¬éáéΩé╬
  40.     host = tmp[0:idx]
  41.     tmp = tmp[idx:]
  42.  
  43.   u['path'] = tmp
  44.   #dir
  45.   u['dir'] = os.path.dirname(tmp)
  46.   #filename
  47.   u['filename'] = os.path.basename(tmp)
  48.   #query
  49.   idx = string.find(u['filename'],'?')
  50.   if idx > -1:
  51.     u['query'] = u['filename'][idx + 1:]
  52.  
  53.   #user&pass
  54.   idx = string.find(host,'@')
  55.   #user&passé╚é╡
  56.   if idx == -1:
  57.     idx = string.find(host,':')
  58.     #porté╚é╡
  59.     if idx == -1:
  60.       u['host'] = host
  61.     else: #portéáéΦ
  62.       u['host'] = host[0:idx]
  63.       u['port'] = host[idx + 1:]
  64.   else: #user&passéáéΦ
  65.     tmp2 = host[0:idx]
  66.     tmp3 = host[idx + 1:]
  67.     idx = string.find(tmp2,':')
  68.     if idx > -1:
  69.       u['user'] = tmp2[0:idx]
  70.       u['password'] = tmp2[idx + 1:]
  71.     #host&port
  72.     idx = string.find(tmp3,':')
  73.     #porté╚é╡
  74.     if idx == -1:
  75.       u['host'] = tmp3
  76.     else: #portéáéΦ
  77.       u['host'] = tmp3[0:idx]
  78.       u['port'] = tmp3[idx + 1:]
  79.  
  80.   return u
  81. #end parse_url
  82.  
  83. def extract_url(url,proxy = ''):
  84.   r'''ò╘Ælé═ host,port,pathé╠â^âvâï'''
  85.   url_info = parse_url(url,'http')
  86.   #proxyé¬éáéΘé╚éτé╬
  87.   if proxy:
  88.     #pathé═URL
  89.     if url_info['port']:
  90.       path = url_info['protocol'] + '://' + url_info['host'] + ':' + url_info['port'] + url_info['path']
  91.     else:
  92.       path = url_info['protocol'] + '://' + url_info['host'] + url_info['path']
  93.  
  94.     #porté≡â`âFâbâN
  95.     proxy_info = parse_url(proxy)
  96.     host = proxy_info['host']
  97.     try:
  98.       port = string.atoi(proxy_info['port'])
  99.     except:
  100.       port = 8080
  101.   else: #proxyé╚é╡
  102.     #pathé═é╗é╠é▄é▄path
  103.     path = url_info['path']
  104.     host = url_info['host']
  105.     try:
  106.       port = string.atoi(url_info['port'])
  107.     except:
  108.       port = 80
  109.  
  110.   return (host,port,path)
  111. #end extract_url
  112.  
  113. def restore_webjump(url):
  114.   r'''ò╘Ælé═ webjumpé╠î│é╠Url
  115.   http://freehosting1.at.webjump.com/0919a3cc8/ac/acount-webjump/filename.jpg'''
  116.  
  117.   m = re.search(r'freehosting[0-9]*\.at\.webjump\.com',url,re.IGNORECASE)
  118.   #â}âbâ`é╡é╜
  119.   if m:
  120.     #âAâJâEâôâgé≡ÆTé╖
  121.     m = re.search(r'/[^/]+-webjump/',url,re.IGNORECASE)
  122.     if m:
  123.       acount = url[m.start(0) + 1:m.end(0) - 9]
  124.       path = url[m.end(0) - 1:]
  125.       #ɼî≈
  126.       return 'http://' + acount + '.webjump.com' + path
  127.  
  128.   return url
  129. #end restore_webjump
  130.  
  131. def restore_homestead(url):
  132.   r'''ò╘Ælé═homesteadé╠î│é╠url
  133.   http://freeload.homestead.com/_ksi0701961600708147/acount/files/filename'''
  134.  
  135.   m = re.search(r'freeload.*\.homestead\.com/',url,re.IGNORECASE)
  136.   if m:
  137.     path = url[m.end(0) - 1:]
  138.     m = re.search(r'/[^/]*/files/.*',path,re.IGNORECASE)
  139.     if m:
  140.       return 'http://www.homestead.com' + path[m.start(0):]
  141.  
  142.   return url
  143. #end restore_homestead
  144.  
  145. def replace_renren(url):
  146.   r'''ò╘Ælé═Æuè╖é╡é╜renrené╠Url'''
  147.   
  148.   m = re.search(r'renren\.com/.+/',url,re.IGNORECASE)
  149.   if m:
  150.     user = url[m.start(0) + 11: m.end(0) - 1]
  151.     path = url[m.end(0) - 1:]
  152.     return 'http://www.renren.com/js/download.DPage?user=' + user + '&path=' + path
  153.   else:
  154.     return url
  155. #end replace_renren
  156.  
  157.   
  158.