generated from pavel.muhortov/template-bash
add Do.wp_routine_media()
This commit is contained in:
parent
2079c7f951
commit
aea4a5ba52
|
@ -2370,6 +2370,231 @@ class Do():
|
|||
found_list.append(path.join(path.realpath(root), file))
|
||||
return found_list
|
||||
|
||||
@staticmethod
|
||||
def wp_routine_media(
|
||||
wp: Wordpress,
|
||||
targets_media_files: dict,
|
||||
period: str,
|
||||
amount: int,
|
||||
page_id: int
|
||||
) -> dict:
|
||||
"""Custom Wordpress media routine - upload media, create media events, update media page.
|
||||
|
||||
Args:
|
||||
wp (Wordpress): wordpress object.
|
||||
targets_media_files (dict): {'period':{'name':'local/path/to/file'}}.
|
||||
period (str, optional): 'y','m','w','d'.
|
||||
amount (int, optional): +/- periods.
|
||||
page_id (int): unique identifier for the page.
|
||||
|
||||
Returns:
|
||||
dict: {'media upload': bool, 'event create': bool, 'pages update': bool}
|
||||
"""
|
||||
if Do.args_valid(locals(), Do.wp_routine_media.__annotations__):
|
||||
default_media_links = {
|
||||
"d": {
|
||||
"point-01": "https://www.hmp.today/wp-content/uploads/2022/07/point-01_yyyy.mm_.dd_.mp4",
|
||||
"point-02": "https://www.hmp.today/wp-content/uploads/2022/07/point-02_yyyy.mm_.dd_.mp4",
|
||||
"point-04": "https://www.hmp.today/wp-content/uploads/2022/07/point-04_yyyy.mm_.dd_.mp4",
|
||||
"point-05": "https://www.hmp.today/wp-content/uploads/2022/07/point-05_yyyy.mm_.dd_.mp4",
|
||||
"point-11": "https://www.hmp.today/wp-content/uploads/2022/07/point-11_yyyy.mm_.dd_.mp4",
|
||||
"point-12": "https://www.hmp.today/wp-content/uploads/2022/07/point-12_yyyy.mm_.dd_.mp4"
|
||||
},
|
||||
"w": {
|
||||
"point-01": "https://www.hmp.today/wp-content/uploads/2022/07/point-01_w.mp4",
|
||||
"point-02": "https://www.hmp.today/wp-content/uploads/2022/07/point-02_w.mp4",
|
||||
"point-04": "https://www.hmp.today/wp-content/uploads/2022/07/point-04_w.mp4",
|
||||
"point-05": "https://www.hmp.today/wp-content/uploads/2022/07/point-05_w.mp4",
|
||||
"point-11": "https://www.hmp.today/wp-content/uploads/2022/07/point-11_w.mp4",
|
||||
"point-12": "https://www.hmp.today/wp-content/uploads/2022/07/point-12_w.mp4"
|
||||
},
|
||||
"m": {
|
||||
"point-01": "https://www.hmp.today/wp-content/uploads/2022/07/point-01_yyyy.mm_.mp4",
|
||||
"point-02": "https://www.hmp.today/wp-content/uploads/2022/07/point-02_yyyy.mm_.mp4",
|
||||
"point-04": "https://www.hmp.today/wp-content/uploads/2022/07/point-04_yyyy.mm_.mp4",
|
||||
"point-05": "https://www.hmp.today/wp-content/uploads/2022/07/point-05_yyyy.mm_.mp4",
|
||||
"point-11": "https://www.hmp.today/wp-content/uploads/2022/07/point-11_yyyy.mm_.mp4",
|
||||
"point-12": "https://www.hmp.today/wp-content/uploads/2022/07/point-12_yyyy.mm_.mp4"
|
||||
},
|
||||
"y": {
|
||||
"point-01": "https://www.hmp.today/wp-content/uploads/2022/07/point-01_yyyy.mp4",
|
||||
"point-02": "https://www.hmp.today/wp-content/uploads/2022/07/point-02_yyyy.mp4",
|
||||
"point-04": "https://www.hmp.today/wp-content/uploads/2022/07/point-04_yyyy.mp4",
|
||||
"point-05": "https://www.hmp.today/wp-content/uploads/2022/07/point-05_yyyy.mp4",
|
||||
"point-11": "https://www.hmp.today/wp-content/uploads/2022/07/point-11_yyyy.mp4",
|
||||
"point-12": "https://www.hmp.today/wp-content/uploads/2022/07/point-12_yyyy.mp4"
|
||||
}
|
||||
}
|
||||
current_media_links = default_media_links[period]
|
||||
result = {}
|
||||
|
||||
# media upload
|
||||
result['media upload'] = True
|
||||
for name, link in current_media_links.items():
|
||||
file_found = wp.media_search(
|
||||
media_name=path.basename(targets_media_files[name]),
|
||||
media_type='video'
|
||||
)
|
||||
if file_found['success'] and len(file_found['result']) > 0:
|
||||
logging.info(
|
||||
msg=""
|
||||
+ "upload skipped, "
|
||||
+ targets_media_files[name]
|
||||
+ " found on site"
|
||||
)
|
||||
targets_media_files.pop(name, None)
|
||||
current_media_links[name] = file_found['result'][0]
|
||||
else:
|
||||
file_upload = wp.media_upload(targets_media_files[name], 'video/mp4')
|
||||
if file_upload['success']:
|
||||
current_media_links[name] = file_upload['result']
|
||||
else:
|
||||
result['media upload'] = False
|
||||
|
||||
# event create
|
||||
result['event create'] = True
|
||||
for name, link in current_media_links.items():
|
||||
description = (''
|
||||
+ '<figure class="wp-block-video">'
|
||||
+ '<video controls loop src="' + link + '"></video>'
|
||||
+ '</figure>'
|
||||
)
|
||||
date = Do.date_calc(period=period, amount=amount)
|
||||
date_start = (''
|
||||
+ str(date['start']['y'])
|
||||
+ '-'
|
||||
+ str(date['start']['m']).zfill(2)
|
||||
+ '-'
|
||||
+ str(date['start']['d']).zfill(2)
|
||||
+ 'T00:00:00'
|
||||
)
|
||||
date_end = (''
|
||||
+ str(date['end']['y'])
|
||||
+ '-'
|
||||
+ str(date['end']['m']).zfill(2)
|
||||
+ '-'
|
||||
+ str(date['end']['d']).zfill(2)
|
||||
+ 'T23:59:59'
|
||||
)
|
||||
if period == 'd':
|
||||
slug = (''
|
||||
+ name
|
||||
+ '_'
|
||||
+ str(date['start']['y'])
|
||||
+ '-'
|
||||
+ str(date['start']['m']).zfill(2)
|
||||
+ '-'
|
||||
+ str(date['start']['d']).zfill(2)
|
||||
)
|
||||
title = (''
|
||||
+ name
|
||||
+ ' '
|
||||
+ str(date['start']['y'])
|
||||
+ '.'
|
||||
+ str(date['start']['m']).zfill(2)
|
||||
+ '.'
|
||||
+ str(date['start']['d']).zfill(2)
|
||||
)
|
||||
if period == 'w':
|
||||
slug = (''
|
||||
+ name
|
||||
+ '_'
|
||||
+ str(date['start']['y'])
|
||||
+ '-w'
|
||||
+ str(date['start']['w']).zfill(2)
|
||||
)
|
||||
title = (''
|
||||
+ name
|
||||
+ ' '
|
||||
+ str(date['start']['y'])
|
||||
+ '-w'
|
||||
+ str(date['start']['w']).zfill(2)
|
||||
)
|
||||
if period == 'm':
|
||||
slug = (''
|
||||
+ name
|
||||
+ '_'
|
||||
+ str(date['start']['y'])
|
||||
+ '-'
|
||||
+ str(date['start']['m']).zfill(2)
|
||||
)
|
||||
title = (''
|
||||
+ name
|
||||
+ ' '
|
||||
+ str(date['start']['y'])
|
||||
+ '.'
|
||||
+ str(date['start']['m']).zfill(2)
|
||||
)
|
||||
if period == 'y':
|
||||
slug = (''
|
||||
+ name
|
||||
+ '_'
|
||||
+ str(date['start']['y'])
|
||||
)
|
||||
title = (''
|
||||
+ name
|
||||
+ ' '
|
||||
+ str(date['start']['y'])
|
||||
)
|
||||
|
||||
event_api_slug = wp.api_event + '/by-slug/' + slug
|
||||
if current_media_links[name] != default_media_links[period][name]:
|
||||
pass
|
||||
elif Connect.http(event_api_slug)['success']:
|
||||
logging.info(msg="event skipped, " + event_api_slug + " found on site")
|
||||
else:
|
||||
event_create = wp.event_create(
|
||||
title=title,
|
||||
slug=slug,
|
||||
date_start=date_start,
|
||||
date_end=date_end,
|
||||
date_publish=date_start,
|
||||
description=description
|
||||
)
|
||||
if not event_create['success']:
|
||||
result['event create'] = False
|
||||
|
||||
# pages update
|
||||
result['pages update'] = True
|
||||
page_read = wp.pages_read(page_id)
|
||||
if page_read['success']:
|
||||
content = json.loads(page_read['result'])['content']['rendered']
|
||||
for name, link in current_media_links.items():
|
||||
if period == 'd':
|
||||
reg_exp = (""
|
||||
+ "_(?:[0-9]{4}|yyyy)"
|
||||
+ ".(?:[0-9]{2}|mm_)"
|
||||
+ ".(?:[0-9]{2}|dd_)(?:|-[0-9]).mp4"
|
||||
)
|
||||
if period == 'w':
|
||||
reg_exp = "(?:_[0-9]{4}-w[0-9]{2}|_w)(?:|-[0-9]).mp4"
|
||||
if period == 'm':
|
||||
reg_exp = "_(?:[0-9]{4}|yyyy).(?:[0-9]{2}|mm_)(?:|-[0-9]).mp4"
|
||||
if period == 'y':
|
||||
reg_exp = "_(?:[0-9]{4}|yyyy)(?:|-[0-9])"
|
||||
|
||||
replace = 0
|
||||
new_str = link
|
||||
pattern = wp.url_files + "/[0-9]{4}/[0-9]{2}/" + name + reg_exp
|
||||
for old_str in re.findall(pattern, content):
|
||||
if old_str == new_str:
|
||||
logging.info(
|
||||
msg=""
|
||||
+ "page replace skipped, "
|
||||
+ new_str
|
||||
+ " found on page"
|
||||
)
|
||||
else:
|
||||
content = content.replace(old_str, new_str)
|
||||
replace += 1
|
||||
logging.info(msg="page replace" + old_str + " to " + new_str)
|
||||
|
||||
if replace > 0:
|
||||
page_update = wp.pages_update(page_id = page_id, content = content)
|
||||
result['pages update'] = page_update['success']
|
||||
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
time_start = datetime.now()
|
||||
|
|
Loading…
Reference in New Issue
Block a user