LunaGen/LunaGen/src/lunagen/addons/affiliates/affiliates.py

45 lines
1.4 KiB
Python

'''
Created on Oct 24, 2021
@author: terry
'''
import os, random
import yaml
import lunagen.addon
class Affiliates(lunagen.addon.LunaGenAddOn):
"""
Affiliates advertising bar.
"""
name = 'affiliates'
desc = """\
Randomly selects affiliate link ads from a list and sets them to
display on a scrolling bar on the page.
"""
file = __file__
@staticmethod
def load(site):
if site.verbose: print("Loading affiliates data.")
try:
with open(os.path.join(site.datadir, 'affiliates.yaml')) as aff_file:
affiliates = yaml.safe_load(aff_file)
stylesheets = site.sitedata['stylesheets']
site.sitedata.update(affiliates)
site.sitedata['stylesheets'] = site._collect_stylesheets(affiliates)
site.sitedata['affiliates'] = random.sample(
affiliates['affiliates'], min( int(affiliates['affiliates_at_once']),
len(affiliates['affiliates'])))
# Generates a single banner default:
site.sitedata['banners'] = iter(['affiliates_banner.j2'])
site._copy_skeleton(os.path.join(os.path.dirname(__file__), 'skeleton'))
except FileNotFoundError:
print("No affiliates.yaml file, so affiliates list is empty.")
site.sitedata['affiliates'] = []