-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_p2p_storylink.py
63 lines (53 loc) · 2 KB
/
create_p2p_storylink.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from clint.textui import colored
from tarbell.hooks import register_hook
from tarbell.utils import puts
@register_hook('publish')
def create_p2p_storylink(site, s3):
"""Create or update a P2P storylink content item when publishing this project"""
puts("\nAttempting to create P2P storylink for this project ...\n")
try:
import p2p
except ImportError:
puts(colored.red(
"The p2p-python package is not installed. Storylink not created.\n"))
return
if not is_production_bucket(s3.bucket, site.project.S3_BUCKETS):
puts(colored.red(
"Not publishing to production bucket. Skipping storylink creation\n."))
return
try:
p2p_slug = site.project.DEFAULT_CONTEXT['P2P_STORYLINK_SLUG']
except KeyError:
puts(colored.red(
"P2P_STORYLINK_SLUG not defined in DEFAULT_CONTEXT. Storylink "
"not created.\n"))
return
url = "http://" + s3.bucket
connection = p2p.get_connection()
content_item = {
'slug': p2p_slug,
'content_item_type_code': 'storylink',
'url': url,
'title': site.project.DEFAULT_CONTEXT['title'],
}
created, ci = connection.create_or_update_content_item(content_item)
if created:
puts(colored.green(
"P2P storylink with slug '{slug}' created.\n".format(
slug=p2p_slug)))
connection.update_content_item({
'slug': p2p_slug,
'content_item_state_code': 'working',
})
ci['content_item_state_code'] = 'working'
else:
puts(colored.green("P2P storylink with slug '{slug}' updated.\n".format(
slug=p2p_slug)))
if 'thumbnail_url' not in ci:
puts(colored.cyan(
"No thumbnail for storylink. Be sure to add one in P2P."))
def is_production_bucket(bucket_url, buckets):
for name, url in buckets.items():
if url == bucket_url and name == 'production':
return True
return False