Advertisement
Guest User

PW_intake.py

a guest
Mar 8th, 2015
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.01 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import cookielib
  4. import urllib
  5. import urllib2
  6. import os
  7. import re
  8.  
  9. # ソコニハ、意志ハ無イ。在ルノハプログラムダケ。
  10. # (pwOpener, pwCookies, pwHH) = PW_Connect()
  11. # songIDList = PW_ParseSongIDs_IIDX(pwOpener)
  12. # chartsRead = PW_BruteForce_IIDX(pwOpener, songIDList)
  13. # chartsRead = PW_BruteForce_DDR(pwOpener)
  14.  
  15. def PW_Connect(pwOutputDir=r"C:\Temp\PW_IIDX"):
  16.     # With appreciation to @blokeley at http://stackoverflow.com/questions/2910221/how-can-i-login-to-a-website-with-python
  17.     loginURL = 'https://programmedworld.net/login'
  18.     loginTel1 = {'username' : 'Telperion_1',
  19.              'password' : 'end_of_world'}
  20.  
  21.     loginData = urllib.urlencode(loginTel1)
  22.     pwCookies = cookielib.CookieJar()
  23.  
  24.     pwOpener = urllib2.build_opener(
  25.         urllib2.HTTPRedirectHandler(),
  26.         urllib2.HTTPHandler(debuglevel=0),
  27.         urllib2.HTTPSHandler(debuglevel=0),
  28.         urllib2.HTTPCookieProcessor(pwCookies))
  29.  
  30.     pwResponse = pwOpener.open(loginURL, loginData)
  31.     pwLandingPage = pwResponse.read()
  32.     pwHTTPHeaders = pwResponse.info()
  33.  
  34.     try:
  35.         localFIDLP = open(pwOutputDir + "\\landingPage.html", 'w')
  36.     except:
  37.         os.makedirs(pwOutputDir)
  38.         localFIDLP = open(pwOutputDir + "\\landingPage.html", 'w')
  39.     localFIDLP.write(pwLandingPage)
  40.     localFIDLP.close()
  41.    
  42. ##  try:
  43. ##      localFIDHH = open(pwOutputDir + "\\HTTPHeaders.html", 'w')
  44. ##  except:
  45. ##      os.makedirs(pwOutputDir)
  46. ##      localFIDHH = open(pwOutputDir + "\\HTTPHeaders.html", 'w')
  47. ##  localFIDHH.write(pwHTTPHeaders)
  48. ##  localFIDHH.close()
  49.    
  50.     return (pwOpener, pwCookies, pwHTTPHeaders)
  51.  
  52. def PW_ReadChartScorePage_IIDX(pwOpener, songID, chartDifficulty, mixIIDX=22, pwOutputDir=r"C:\Temp\PW_IIDX"):
  53.     try:
  54.         pwFilename = 'https://programmedworld.net/iidx/{2:n}/music/{0:n}/{1:s}'.format(songID, chartDifficulty, mixIIDX)
  55.         pwFID = pwOpener.open(pwFilename)
  56.         songPage = pwFID.read()
  57.     except:
  58.         print "Unable to locate: song ID {0:05n}, chart {1:s}".format(songID, chartDifficulty)
  59.         return 0
  60.     else:
  61.         localFilename = (pwOutputDir + '\\IIDX{2:n}\\{0:05n}_{1:s}.html').format(songID, chartDifficulty, mixIIDX)
  62.         try:
  63.             os.makedirs( (pwOutputDir + '\\IIDX{2:n}').format(songID, chartDifficulty, mixIIDX) )
  64.         except:
  65.             pass
  66.         finally:
  67.             localFID = open(localFilename, 'w')
  68.             localFID.write(songPage)
  69.             localFID.close()
  70.  
  71.             print("Successfully read song ID {0:05n}, chart {1:s}!".format(songID, chartDifficulty))
  72.             return 1
  73.  
  74. def PW_ParseSongIDs_IIDX(pwOpener, mixIIDX=22, pwOutputDir=r"C:\Temp\PW_IIDX"):
  75.     try:
  76.         pwFilename = 'https://programmedworld.net/iidx/{0:n}/music'.format(mixIIDX)
  77.         pwFID = pwOpener.open(pwFilename)
  78.         songPage = pwFID.read()
  79.     except:
  80.         print "Unable to locate music listing page"
  81.         return []
  82.     else:
  83.         songIDList = re.findall(r"/iidx/\d+/music/(\d+)/", songPage, re.UNICODE)
  84.  
  85.         localFilename = (pwOutputDir + '\\IIDX{0:n}\\songIDList.txt').format(mixIIDX)
  86.         try:
  87.             os.makedirs( (pwOutputDir + '\\IIDX{0:n}').format(mixIIDX) )
  88.         except:
  89.             pass
  90.         finally:
  91.             localFID = open(localFilename, 'w')
  92.             for songID in songIDList:
  93.                 localFID.write(str(songID) + '\n')
  94.             localFID.close()
  95.  
  96.             print("Successfully downloaded and parsed music listing page!")
  97.         return songIDList
  98.  
  99.  
  100. def PW_BruteForce_IIDX(pwOpener, songIDList=None, mixIIDX=22, pwOutputDir=r"C:\Temp\PW_IIDX"):
  101.     songsRead = 0;
  102.        
  103.     try:
  104.         digestFID = open(pwOutputDir + "\\readResults.csv", 'w')
  105.     except:
  106.         os.makedirs(pwOutputDir)
  107.         digestFID = open(pwOutputDir + "\\readResults.csv", 'w')
  108.  
  109.     if songIDList == None:
  110.         for mixID in range(1, 22):
  111.             songIDsSkipped = 0
  112.             for songIndex in range(150):
  113.                 songID = mixID*1000 + songIndex;
  114.                 for chartID in ['n7', 'h7', 'a7', 'n14', 'h14', 'a14']:
  115.                     try:
  116.                         pwResult = PW_ReadChartScorePage_IIDX(pwOpener, songID, chartID, mixIIDX, pwOutputDir)
  117.                     except:
  118.                         pwResult = 0
  119.                         songIDsSkipped = songIDsSkipped + 1
  120.                     finally:
  121.                         digestFID.write('{0:05n},{1:s},{2:n}\n'.format(songID, chartID, pwResult))
  122.                         songsRead = songsRead + pwResult
  123.             digestFID.flush()
  124.     else:
  125.         for songID in songIDList:
  126.             for chartID in ['n7', 'h7', 'a7', 'n14', 'h14', 'a14']:
  127.                 try:
  128.                     pwResult = PW_ReadChartScorePage_IIDX(pwOpener, int(songID), chartID, mixIIDX, pwOutputDir)
  129.                 except:
  130.                     pwResult = 0
  131.                     songIDsSkipped = songIDsSkipped + 1
  132.                 finally:
  133.                     digestFID.write('{0:05n},{1:s},{2:n}\n'.format(int(songID), chartID, pwResult))
  134.                     songsRead = songsRead + pwResult
  135.         digestFID.flush()
  136.                
  137.     digestFID.close()
  138.  
  139.     return songsRead
  140.  
  141. def PW_ReadChartScorePage_DDR(pwOpener, songID, chartDifficulty, mixDDR=15, pwOutputDir=r"C:\Temp\PW_DDR"):
  142.     try:
  143.         pwFilename = 'https://programmedworld.net/ddr/{2:n}/music/{0:n}/{1:s}'.format(songID, chartDifficulty, mixDDR)
  144.         pwFID = pwOpener.open(pwFilename)
  145.         songPage = pwFID.read()
  146.     except:
  147.         print "Unable to locate: song ID {0:05n}, chart {1:s}".format(songID, chartDifficulty)
  148.         return 0
  149.     else:
  150.         localFilename = (pwOutputDir + '\\DDR{2:n}\\{0:05n}_{1:s}.html').format(songID, chartDifficulty, mixDDR)
  151.         try:
  152.             os.makedirs( (pwOutputDir + '\\DDR{2:n}').format(songID, chartDifficulty, mixDDR) )
  153.         except:
  154.             pass
  155.         finally:
  156.             localFID = open(localFilename, 'w')
  157.             localFID.write(songPage)
  158.             localFID.close()
  159.  
  160.             print("Successfully read song ID {0:05n}, chart {1:s}!".format(songID, chartDifficulty))
  161.             return 1
  162.  
  163. def PW_BruteForce_DDR(pwOpener, mixDDR=15, pwOutputDir=r"C:\Temp\PW_DDR"):
  164.     songsRead = 0;
  165.        
  166.     try:
  167.         digestFID = open(pwOutputDir + "\\readResults.csv", 'w')
  168.     except:
  169.         os.makedirs(pwOutputDir)
  170.         digestFID = open(pwOutputDir + "\\readResults.csv", 'w')
  171.        
  172.     for songID in range(1000):
  173.         for chartID in ['sp-bas', 'sp-dif', 'sp-exp', 'sp-cha',
  174.                 'dp-bas', 'dp-dif', 'dp-exp', 'dp-cha']:
  175.             try:
  176.                 pwResult = PW_ReadChartScorePage_DDR(pwOpener, songID, chartID, mixDDR, pwOutputDir)
  177.             except:
  178.                 pwResult = 0
  179.             finally:
  180.                 digestFID.write('{0:05n},{1:s},{2:n}\n'.format(songID, chartID, pwResult))
  181.                 songsRead = songsRead + pwResult
  182.         digestFID.flush()
  183.                
  184.     digestFID.close()
  185.  
  186.     return songsRead
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement