I'm trying to replace URLs of my conf
file from a HTML file because sometimes the URLs get updated/changed. I made a simple script that can fetch the HTML & update/replace the URLs in my conf
file. The HTML file gets changed by the server admin where I don't have any access. I can only visit the up-to-date site by using the below Html Location
& update my conf file manually.
Html Location: https://10.10.10.1
Part of HTML file:
<li><a href="#" class="dropdown-toggle hvr-bounce-to-bottom" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Movies<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a class="hvr-bounce-to-bottom" href="http://10.10.10.7/MY-FTP-2/English%20Movies/">English Movies</a></li>
<li><a class="hvr-bounce-to-bottom" href="http://10.10.10.8/MY-FTP-1/English%20Movies%20%281080p%29/">English Movies -1080p </a></li>
<li><a class="hvr-bounce-to-bottom" href="http://10.10.10.9/MY-FTP-1/Hindi%20Movies/">Hindi Movies</a></li>
<li><a class="hvr-bounce-to-bottom" href="http://10.10.10.8/MY-FTP-1/SOUTH%20INDIAN%20MOVIES/Hindi%20Dubbed/">South-Movie Hindi Dubbed</a></li>
<li><a class="hvr-bounce-to-bottom" href="http://10.10.10.10/MY-FTP-3/Animation%20Movies/">Animation Movies</a></li>
<li><a class="hvr-bounce-to-bottom" href="http://10.10.10.10/MY-FTP-3/Animation%20Movies%20%281080p%29/">Animation Movies -1080p</a></li>
After getting the HTML, it will replace/update the links in my rclone.conf file.
rclone.conf
file preview:
[Hindi Movies]
type = http
url = http://10.10.10.9/MY-FTP-1/Hindi%20Movies/
[English Movies]
type = http
url = http://10.10.10.7/MY-FTP-2/English%20Movies/
[English Movies -1080p]
type = http
url = http://10.10.10.9/MY-FTP-1/English%20Movies%20%281080p%29/
[South-Movie Hindi Dubbed]
type = http
url = http://10.10.10.9/MY-FTP-1/SOUTH%20INDIAN%20MOVIES/Hindi%20Dubbed/
[Animation Movies]
type = http
url = http://10.10.10.10/MY-FTP-3/Animation%20Movies/
[Animation Movies -1080p]
type = http
url = http://10.10.10.10/MY-FTP-3/Animation%20Movies%20%281080p%29/
So I've written a noob script that will start the work but it seems it's giving me an error !
import re
import requests
from bs4 import BeautifulSoup
# Fetch the HTML from the website
html = requests.get("http://10.10.10.1/")
# Parse the HTML
soup = BeautifulSoup(html.text, 'html.parser')
# The location of the rclone.conf file
rclone_conf_file = '/home/user/tmp/rclone.conf'
# Open the rclone.conf file
with open(rclone_conf_file, 'r') as f:
# Read the file into a list of lines
lines = f.readlines()
# Iterate over the <a> tags in the HTML
for a in soup.find_all('a'):
# Get the text of the <a> tag (e.g. "Hindi Movies")
section_name = a.text.strip().lower()
# Check if the section name exists in the rclone.conf file
if any(section_name in line.lower() for line in lines):
# Get the URL of the <a> tag
new_url = a['href']
# Use a regular expression to match the URL in the rclone.conf file
regex = r'^(\[%s\]\n.*\n.*http.*)' % re.escape(section_name)
# Update the URL in the rclone.conf file
for i, line in enumerate(lines):
if section_name in line.lower():
print(lines[i]) # <-- Add this line
lines[i] = re.sub(regex, r'\1', line, flags=re.IGNORECASE)
lines[i] = lines[i].replace(lines[i].split()[2], new_url)
# Open the rclone.conf file for writing
with open(rclone_conf_file, 'w') as f:
# Write the updated lines to the file
for line in lines:
f.write(line)
The error it's showing:
File "/home/plex/tmp/script.py", line 37, in <module>
lines[i] = lines[i].replace(lines[i].split()[2], new_url)
IndexError: list index out of range
Have A Octotastic Day !!