Advertisement
Guest User

Untitled

a guest
Dec 28th, 2018
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.57 KB | None | 0 0
  1. import random
  2. import urllib.request as urll
  3. #import time
  4. #undocument if sleep() is needed
  5.  
  6. url = 'https://prnt.sc/'
  7.  
  8. request_headers = {'User-Agent': 'Mozilla/5.0'}
  9. #The headers below often gave 403, the headers above usually give positive results.
  10. #Just comment one and uncomment another if you want to change headers
  11. '''
  12. request_headers = {
  13. "Accept-Language": "en-US,en;q=0.5",
  14. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0",
  15. "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
  16. "Referer": "https://google.com",
  17. "Connection": "keep-alive"
  18. }'''
  19.  
  20. FullRandomMode = False
  21. FirstLettersMode = False
  22.  
  23. noimage = 'st.prntscr.com/2018/10/13/2048/img/0_173a7b_211be8ff.png'
  24.  
  25.  
  26. fullrandomrange = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
  27. randomletters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
  28. randomnumbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
  29.  
  30. print('\nFull Random Mode - 1\n\nEnter first letters Mode - 2\n')
  31. decision = input()
  32.  
  33. while decision == '1':
  34.     print('\nEnterig Full Random Mode!')
  35.     FullRandomMode = True
  36.     break
  37. while decision == '2':
  38.     print('\nFirst Letters Mode chosen:\n\nEnter your first letters (up to 6):')
  39.     first_letters = input()
  40.     print('\nEntering First Letters Mode')
  41.     FirstLettersMode = True
  42.     break
  43. while decision not in ['1', '2']:
  44.     print('\nRestart the script and enter eiter 1 or 2')
  45.     break
  46.  
  47. def scrapefullrandom():
  48.     #Generating random URL
  49.     r1 = random.choice(fullrandomrange)
  50.     r2 = random.choice(fullrandomrange)
  51.     r3 = random.choice(fullrandomrange)
  52.     r4 = random.choice(fullrandomrange)
  53.     r5 = random.choice(fullrandomrange)
  54.     r6 = random.choice(fullrandomrange)
  55.     fullrandomsymbols=r1+r2+r3+r4+r5+r6
  56.     full_url = url+fullrandomsymbols
  57.     #Getting html code
  58.     request = urll.Request(full_url, headers=request_headers)
  59.     page = urll.urlopen(request).read()
  60.     #From here done without BeautifulSoup
  61.     page_str = str(page)
  62.     #Seeing if it's a valid URL (contains an image)
  63.     if noimage in page_str:
  64.         print(full_url+'\n'+'Invalid URL')
  65.     else:
  66.         print(full_url+'\n'+'Valid URL')
  67.         with open('1.txt', 'a') as file:
  68.             file.write('\n'+full_url)
  69.         #Searching for direct image url
  70.         page_str_noreturns = page_str.replace(' ', '\n')
  71.         page_list = page_str_noreturns.split('\n')
  72.         #Excepting error if no image was detected (can fix the overall detection)
  73.         try:
  74.             page_list_image_url_number = page_list.index('screenshot-image"')
  75.         except ValueError:
  76.             print('\nAn error occured, valid URL but no image detected, continuing running the script!\n')
  77.             with open('1.txt','a') as file:
  78.                 file.write('\nAn Error Occured - No image detected\n')
  79.             return
  80.         page_list_image_url_number += 1
  81.         almost_image_url = page_list[page_list_image_url_number]
  82.         image_url = almost_image_url[5:][:-1]
  83.         print(image_url)
  84.         #Parsing a name for the image
  85.         page_list_image_name_number = page_list_image_url_number+5
  86.         almost_image_name = page_list[page_list_image_name_number]
  87.         image_name = almost_image_name[10:][:-3]
  88.         print(image_name)      
  89.         #Opening the image
  90.         image_request = urll.Request(image_url, headers=request_headers)
  91.         image_resourced = urll.urlopen(image_request)
  92.         #Saving the image (assuming they are all .png)
  93.         image_output = open(image_name+'.png', 'wb')
  94.         image_output.write(image_resourced.read())
  95.         image_output.close()
  96.         print('Image downloaded!')
  97.         #Writing the direct image URL in the text file
  98.         with open('1.txt','a') as file:
  99.             file.write('\nDownloaded! Direct URL at:\n'+image_url+'\n')
  100.         #Using sleep() to not get banned right away (if needed, just uncomment the line below)
  101.         #time.sleep(1)
  102.  
  103. def scrapeletters():
  104.     l1 = random.choice(fullrandomrange)
  105.     l2 = random.choice(fullrandomrange)
  106.     l3 = random.choice(fullrandomrange)
  107.     l4 = random.choice(fullrandomrange)
  108.     l5 = random.choice(fullrandomrange)
  109.     l6 = random.choice(fullrandomrange)
  110.     #first_letters
  111.     symbols = l1+l2+l3+l4+l5+l6
  112.     full_url = url+symbols
  113.     source = urll.urlopen(urll.Request(full_url, headers={'User-Agent': 'Mozilla/5.0'}))
  114.     html = source.read()
  115.        
  116.     if noimage in str(html):
  117.         print(full_url+'\n'+'Invalid URL')
  118.     else:
  119.         print(full_url+'\n'+'Valid URL')
  120.         with open('1.txt', 'a', encoding='utf-8') as file:
  121.             file.write('\n'+full_url)
  122.  
  123.  
  124.  
  125.  
  126. while FullRandomMode == True:
  127.     scrapefullrandom()
  128. else:
  129.     pass
  130. while FirstLettersMode == True:
  131.     scrapeletters()
  132. else:
  133.     pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement