I am struggling to implement a while True:
loop in my code.
At the point of if file_removed in bs:
I need to ask the user for a new int
from the list of links provided (go back to which_link = input(lb + "\\n|Which vodlocker link should we use?\\n|\\n| --> ")
) if file_removed
is present on the page.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from GoogleScraper import scrape_with_config, GoogleSearchError
from urllib.request import urlretrieve
from urllib.request import URLError
from urllib.request import urlopen
from hurry.filesize import size
from sys import getsizeof
from bs4 import BeautifulSoup as BS
import subprocess as sp
import requests as r
import pandas as pd
from pyprind import ProgBar
import psutil
import string
import tqdm
import time
import sys
import re
import os
def the_link():
lb = "+--------------------------------------------------------------+"
site = "site:vodlocker.com " # prefix of search term
keywords = site + input(lb + "\n|Film to search\n|\n| --> ") # ask user for search term
config = {
'use_own_ip': True,
'keyword': keywords,
'search_engines': ['google'], # various configuration settings
'num_pages_for_keyword': 1,
'scrape_method': 'http',
#'sel_browser': 'phantomjs', # this makes scraping with browsers headless and quite fast.
'do_caching': True,
'num_results_per_page': 50,
'log_level': 'CRITICAL',
'output_filename': 'results.csv' # file to save links to
}
try:
search = scrape_with_config(config)
except GoogleSearchError as e:
print(e)
csv_file = "results.csv" # create an instance of the file
df = pd.read_csv(csv_file) # read said file
vodlocker_link = df['link'] # get the vodlocker links from the file (note: you can also use df.column_name
link_id = df['title']
results = df['num_results_for_query']
results_lower = results[0].lower()
print(lb + "\nWe have found " + results_lower + "\n") # print the link we will use
title_dict = [
"HDTV","hdtv", "BDRip","BRrip", "HDRip", "HDTS", "hdts", # disc type
"720p", "1080p", # dimensions
"XviD", "Watch",
"mp4", "MP4", "X264",
"mkv", "MKV", # video types
"avi", "AVI",
"-LOL", "LOL",
"ac3", "AC3",
"-playnow", "playnow", "VoDLocker", "-", "_"
"AC3-PLAYNOW",
"EVO", "evo",
"GECKOS", "FASTSUB", # tags
"DIMENSION", "-DIMENSION",
"REPACK", "Vostfr",
"VOSTFR", "libre",
"fum", "-fum",
"on 4vid tv online", "(", ")"
]
regex_title = r"|".join(title_dict)
print(link_id)
s = r.Session() # create an instance of a requests session
headers = {"User-Agent":"Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) Safari/9537.53", "Accept":"text/q=0.9,image/webp,*/*;q=0.8", "Accept-Language":"en-US,en"} # set the user agent and stuff in the header
which_link = input(lb + "\n|Which vodlocker link should we use?\n|\n| --> ")
req = s.get(vodlocker_link[int(which_link)], headers=headers) # get the page from link
bs = BS(req.text, "lxml") # create a soup object
file_removed = "<h3>The file was removed by administrator</h3>"
if file_removed in bs:
print("The file was removed by administrator")
else:
film = bs.find(type="video/mp4") # find the film link
film_link = film["src"] # get the actual film link
title = bs.find(id="file_title").get_text()
fixed_title = re.sub(regex_title, "",title, flags=re.I)
title_lower = fixed_title.lower()
title_strip = title_lower.strip()
title_hyphen = title_strip.replace(" ", "-")
print(lb + "\n|The title of the film is:\n|" + fixed_title)
print(lb + "\n|We found a video link on the vodlocker page:\n|" + film_link)
prfx = "/home/jack/server.files/mp4/films/" # prefix for file_name location
# file_name = input("Please name the file:\n--> ") # ask user for file name
ext = film_link[-4:]
file_name = title_hyphen + ext
print(lb + "\n|We will name the file:\n|\n|" + file_name)
file_name_ok = input(lb + "\n|We have attempted to name the file from the title. Is our guess O.K?\n|\n|[Any key to cotinue]--> ") #TODO prompt user for name if we cant guess it
u = s.get(film_link, headers=headers, stream=True) # create an instance of the file stream
file_size = int(u.headers["content-length"]) # get meta info -- file size
print(lb + "\n|File Path and name:\n|\n|" + prfx + file_name) # print the file name and path
print(lb + "\n|File Size: " + size(file_size)) # print the file size
bar = ProgBar(file_size / 1024, title=lb + "\n|Downloading:\n|\n|" + file_name + "\n" + lb, stream=sys.stdout, bar_char='█', update_interval=1)
with open(prfx + file_name, 'wb') as f:
dl = 0
if file_size is None: # no content length header
f.write(r.content) #TODO print error if size is none!
else:
for chunk in u.iter_content(1024):
dl += len(chunk)
f.write(chunk)
f.flush()
bar.update(item_id = file_name)
print(lb)
print("\n|Finished downloading " + file_name)
print(lb)
the_link()
I know my code is messy and in need of formatting nicely so any pointers on that would be appreciated.
Put the part that needs to be repeated into a loop. When you reach a successful exit condition, break
out of the loop:
...
headers = {"User-Agent":"Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) Safari/9537.53", "Accept":"text/q=0.9,image/webp,*/*;q=0.8", "Accept-Language":"en-US,en"} # set the user agent and stuff in the header
while True:
which_link = input(lb + "\n|Which vodlocker link should we use?\n|\n| --> ")
req = s.get(vodlocker_link[int(which_link)], headers=headers) # get the page from link
bs = BS(req.text, "lxml") # create a soup object
file_removed = "<h3>The file was removed by administrator</h3>"
if file_removed in bs:
print("The file was removed by administrator")
else:
break
film = bs.find(type="video/mp4") # find the film link
film_link = film["src"] # get the actual film link
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.