-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathar2d2.py
105 lines (87 loc) · 2.94 KB
/
ar2d2.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
import sqlite3
import base64
import xml.etree.ElementTree as etree
import requests
from pinder import Campfire, Room
# ***************************************************************************
# *** Important! Here you need to enter data or write it to settings.py ***
# ***************************************************************************
DB_file = ''
# for Codebase
CODEBASE_PROJECT_URL = ''
CODEBASE_USERNAME =''
CODEBASE_APIKEY = ''
# for Campfire
SECRET_TOKEN = ''
SUBDOMAIN = ''
ROOM_ID = ''
# ***************************************************************************
# if you want to keep confidential data separately
try:
from settings import *
except:
pass
# create DB if need
create = not os.path.exists(DB_file)
if create:
db = sqlite3.connect(DB_file)
cursor = db.cursor()
cursor.execute('''create table Messages(
my_id integer primary key autoincrement unique not null,
title text,
id text,
timestamp text,
type text,
html_title text,
html_text text
)''')
db.commit()
cursor.close()
# load last activity
path = 'activity'
url = "%s/%s" % (CODEBASE_PROJECT_URL, path)
headers = {"Content-type": "application/xml",
"Accept": "application/xml",
"Authorization": "Basic %s" % base64.b64encode("%s:%s" % (CODEBASE_USERNAME,
CODEBASE_APIKEY))}
r = requests.get(url, headers=headers)
# parse answer
tree = etree.parse(r)
root = tree.getroot()
db = sqlite3.connect(DB_file)
cursor = db.cursor()
# connect to Campfire
c = Campfire(SUBDOMAIN, SECRET_TOKEN, ssl=True)
room = c.room(ROOM_ID)
room.join()
for child in root:
cursor.execute('select id from Messages where id=?', (child.findall('id')[0].text,))
messages = cursor.fetchall()
if messages:
continue
# write to DB
my_data = (child.findall('title')[0].text,
child.findall('id')[0].text,
child.findall('timestamp')[0].text,
child.findall('type')[0].text,
child.findall('html-title')[0].text,
child.findall('html-text')[0].text)
cursor.execute('''insert into Messages (
title,
id,
timestamp,
type,
html_title,
html_text)
values (?, ?, ?, ?, ?, ?)''', my_data)
db.commit()
m = re.findall('a href="/.+?">', child.findall('html-title')[0].text)
m = m[0].lstrip('a href="')
m = m.rstrip('">')
link = 'https://arvors.codebasehq.com%s' % m
message = '%s \n %s' % (child.findall('title')[0].text, link)
room.speak(message)