-
Notifications
You must be signed in to change notification settings - Fork 11
/
publishx.py
156 lines (131 loc) · 5.41 KB
/
publishx.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import logging
from termcolor import colored
import slixmpp
from slixmpp.xmlstream import ET
import slixmpp.plugins.xep_0060.stanza.pubsub as pubsub
from slixmpp.exceptions import IqError, IqTimeout
import re
NS_ATOM = 'http://www.w3.org/2005/Atom'
NS_JABBER_DATA = 'jabber:x:data'
class Publishx(slixmpp.ClientXMPP):
def __init__(self, config):
jid = config.jid
fulljid = config.jid + "/" + config.resource
secret = config.secret
resource = config.resource
slixmpp.ClientXMPP.__init__(self, fulljid, secret)
self.register_plugin('xep_0060')
async def create(self, server, node, feed):
title = description = logo = ''
if hasattr(feed, 'title'):
title = feed.title
if hasattr(feed, 'description'):
description = feed.description
elif hasattr(feed, 'subtitle'):
description = feed.subtitle
print(colored('>> create %s' % title, 'blue'))
iq = self.Iq(stype="set", sto=server)
iq['pubsub']['create']['node'] = node
form = iq['pubsub']['configure']['form']
form['type'] = 'submit'
form.addField('pubsub#persist_items',
ftype='boolean',
value=1)
form.addField('pubsub#title',
ftype='text-single',
value=title)
form.addField('pubsub#max_items',
ftype='text-single',
value='20')
form.addField('pubsub#type',
ftype='text-single',
value=NS_ATOM)
form.addField('pubsub#deliver_payloads',
ftype='boolean',
value=0)
form.addField('pubsub#description',
ftype='text-single',
value=description)
task = iq.send(timeout=5)
try:
await task
except IqError as e:
if e.etype == 'cancel' and e.condition == 'conflict':
print(colored('!! node %s is already created, assuming its configuration is correct' % node, 'yellow'))
return
raise
async def publish(self, server, node, entry, version):
iq = self.Iq(stype="set", sto=server)
iq['pubsub']['publish']['node'] = node
item = pubsub.Item()
# character / is causing a bug in movim. replacing : and , with - in id. It provides nicer urls.
rex = re.compile(r'[:,\/]')
item['id'] = rex.sub('-', str(entry.id))
ent = ET.Element("entry")
ent.set('xmlns', NS_ATOM)
title = ET.SubElement(ent, "title")
title.text = entry.title
updated = ET.SubElement(ent, "updated")
updated.text = entry.updated
#Content
if version == 'atom3':
if hasattr(entry.content[0], 'type'):
content = ET.SubElement(ent, "content")
content.set('type', entry.content[0].type)
content.text = entry.content[0].value
elif version =='rss20' or 'rss10' or 'atom10':
if hasattr(entry, "content"):
content = ET.SubElement(ent, "content")
content.set('type', 'text/html')
content.text = entry.content[0].value
elif hasattr(entry, "description"):
content = ET.SubElement(ent,"content")
content.set('type', 'text/html')
content.text = entry.description
print('In Description - PublishX')
#Links
if hasattr(entry, 'links'):
for l in entry.links:
link = ET.SubElement(ent, "link")
if hasattr(l, 'href'):
link.set('href', l['href'])
link.set('type', l['type'])
link.set('rel', l['rel'])
elif hasattr(entry, 'link'):
link.set('href', entry['link'])
#Tags
if hasattr(entry, 'tags'):
for t in entry.tags:
tag = ET.SubElement(ent, "category")
tag.set('term', t.term)
if hasattr(entry,'category'):
for c in entry["category"]:
cat = ET.SubElement(ent, "category")
cat.set('category', entry.category[0])
#Author
if version == 'atom03':
if hasattr(entry, 'authors'):
author = ET.SubElement(ent, "author")
name = ET.SubElement(author, "name")
name.text = entry.authors[0].name
if hasattr(entry.authors[0], 'href'):
uri = ET.SubElement(author, "uri")
uri.text = entry.authors[0].href
elif version == 'rss20' or 'rss10' or 'atom10':
if hasattr(entry, 'author'):
author = ET.SubElement(ent, "author")
name = ET.SubElement(ent, "author")
name.text = entry.author
if hasattr(entry.author, 'href'):
uri = ET.SubElement(author, "uri")
uri.text = entry.authors[0].href
item['payload'] = ent
iq['pubsub']['publish'].append(item)
task = iq.send(timeout=5)
try:
await task
except (IqError, IqTimeout) as e:
print(e)
pass
def published(self):
print('published')