headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
Initialize the parser
d = feedparser.FeedParserDict()
Get the links for the music from the Bandcamp page
def get_links_from_page(url):
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
href_list = []
for a in soup.find_all('a', href=True):
if '/track/' in a['href']:
href_list.append('https://'+a['href'][2:])
return href_list
Process the links and format them into RSS items
def process_links(href_list):
items_content = ''
for href in href_list:
name = href.split('/')[-1].replace('-', ' ').title()
items_content += f'{href}New music released by {name}. Check it out!'
return items_content
Loop through and update for new content
while True:
href_list = get_links_from_page(bandcamp_url)
if not d.entries:
for href in href_list:
d.entries.append({'title': href.split('/')[-1].replace('-', ' ').title(), 'link': href})
items_content = process_links(href_list)
rss_content = f'''
{bandcamp_url}
New releases by your favorite Bandcamp artists.
{items_content}
'''
requests.post(rss_feed_url, data=rss_content.encode('utf-8'), headers={'Content-type': 'application/rss+xml'})
else:
for href in href_list:
if href not in [entry.link for entry in d.entries]:
d.entries.append({'title': href.split('/')[-1].replace('-', ' ').title(), 'link': href})
items_content = process_links([href])
rss_content = f'''
{bandcamp_url}
New releases by your favorite Bandcamp artists.
{items_content}
'''
requests.post(rss_feed_url, data=rss_content.encode('utf-8'), headers={'Content-type': 'application/rss+xml'})
time.sleep(60*30) # Wait for 30 minutes and repeat