-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.py
More file actions
executable file
·87 lines (79 loc) · 3.67 KB
/
web.py
File metadata and controls
executable file
·87 lines (79 loc) · 3.67 KB
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json, socket, sys
html = u'''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Banco de dados do ptwikisBot</title>
<link rel="icon" type="image/x-icon" href="static/Tool_labs_logo.ico" />
<link rel="stylesheet" type="text/css" media="screen" href="static/style.css" />
</head>
<body>
<div id="content-wrapper">
<div id="menu">
<img src="//upload.wikimedia.org/wikipedia/commons/thumb/a/a4/Tool_labs_logo.svg/120px-Tool_labs_logo.svg.png">
<div id="menu-content">
<div id="menu-item"><a href="https://github.com/ptwikis/ptwikisBot/wiki">Manual</a></div>
<div id="menu-item"><a href="https://webchat.freenode.net/?channels=wikipedia-pt">Web chat</a></div>
</div>
</div>
<div id="content">
<br/>
<h1>Banco de dados do ptwikisBot</h1>
<hr/> Tool Labs – Ferramentas para projetos lusófonos <br/><br/>
%s
</div>
</div>
<div style="text-align:center; font-size:x-small; margin: 10px 10px 12px 181px">
O <a href="https://github.com/ptwikis/ptwikisBot">código-fonte</a> do robô é disponibilizado sob a licença GNU General Public License 3.0 (GPL V3).
</div>
</body>
</html>
'''
def page():
with open('db.json') as f:
db = json.load(f)
channels = u'<h2>Configuração dos canais</h2><hr/>\n'
for chan in sorted(set(db['vigiar'].keys() + db['links'])):
channels += u'<h3>%s</h3>\n<p>Wikilinks: %sligado</p>\n' % (chan, u'' if chan in db['links'] else u'des')
if chan in db['vigiar']:
pages = u'\n'.join(u'<tr><td>%s</td></tr>' % p for p in db['vigiar'][chan])
channels += u'<table class="wikitable"><tr><th>Páginas vigiadas</th></tr>\n%s\n</table>\n\n' % pages
phab = u'''<h2>Phabricator</h2><hr/>
<p>Os seguintes termos disparam notificações do phabricator e gerrit em #wikipedia-pt-tecn:</p>
<ul>
%s
</ul>
''' % u'\n'.join(u'<li>%s' % t for t in sorted(db['phab']))
return html % (channels + u'<br/>\n' + phab)
def app(environ, start_response):
if environ['REQUEST_METHOD'] == 'POST' and environ.get('CONTENT_TYPE') == 'application/json':
return github(environ, start_response)
start_response('200 OK', [('Content-Type', 'text/html')])
return [page().encode('utf-8')]
def github(environ, start_response):
size = int(environ.get('CONTENT_LENGTH', 0))
data = json.loads(environ.get('wsgi.input').read(size))
for c in data.get('commits', []):
msg = u'\x0fGitHub [\x0306%s\x03] \x0303%s\x03 (%s) \x0314commit\x03 \x0312%s\x03 (%s)' % \
(data['repository']['full_name'], c['author']['name'], c['author']['username'], c['url'], c['message'][:100])
irc('#mediawiki-pt ' + msg.encode('utf-8'))
_type = 'issue' in data and 'issue' or 'pull_request' in data and 'pull_request'
if _type:
msg = u'\x0fGitHub [\x0306%s\x03] \x0303%s\x03 \x0314%s\x03 %s \x0312%s\x03 %s' % \
(data['repository']['full_name'], data[_type]['user']['login'], _type, data[_type]['title'],
data[_type]['html_url'], data[_type]['body'][:80])
irc('#mediawiki-pt ' + msg.encode('utf-8'))
start_response('200 OK', [('Content-Type', 'text/html')])
return ['github OK']
def irc(msg):
with open('/data/project/ptwikis/bot/bothost') as f:
host = f.read().split(':')
s = socket.socket()
s.connect((host[0], int(host[1])))
s.send(msg)
s.shutdown(socket.SHUT_RDWR)
if __name__ == '__main__':
from flup.server.fcgi import WSGIServer
WSGIServer(app).run()