usr local bin python2 import re import urllib2 as u2 from lxml import

 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
#!/usr/local/bin/python2.5
import re
import urllib2 as u2
from lxml import etree as et
import sqlalchemy as sq
guild_url = 'http://eu.wowarmory.com/guild-info.xml?r=%D0%A1%D1%82%D1%80%D0%B0%D0%B6+%D0%A1%D0%BC%D0%B5%D1%80%D1%82%D0%B8&n=%D0%94%D0%B5%D0%BD%D1%8C+%D0%93%D0%BD%D0%B5%D0%B2%D0%B0&p=1'
r = u2.Request(guild_url, headers={'User-Agent':'Mozilla/5.0 (X11; U; Plan-9; en-GB; rv:1.9.0.4) Gecko/20380101 Firefox/3.0.4 '})
data = u2.urlopen(r)
tree = et.parse(data)
armory_list = [ char.get('name').encode('UTF-8') for char in tree.xpath('/page/guildInfo/guild/members/character') ]
engine = sq.create_engine('mysql://login:password@host/database', encoding="utf8")
conn = engine.connect()
conn.execute('SET CHARACTER SET utf8')
md = sq.MetaData(engine)
md.reflect()
table = md.tables[u'smf_members']
name = table.c.realName
sel = sq.select([name]).where(sq.and_(table.c.ID_GROUP == 9, sq.not_(name.in_(armory_list))))
result = conn.execute(sel)
members = [ m[0] for m in result.fetchall() ]
to_update = []
for mem in members:
found = False
for arm in armory_list:
if arm in mem:
found = True
if not found:
to_update.append(mem)
print "Content-type: text/plain\n"
print "Guild members found: %s" % len(armory_list)
print "Forum grunts found: %s" % len(members)
print "Have to update %s forum members: " % len(to_update)
for t in to_update:
print t
upd = table.update(values={table.c.ID_GROUP:10}).where(sq.and_(table.c.ID_GROUP == 9, name.in_(to_update)))
print upd
res = conn.execute(upd)
print [i[0] for i in res.fetchall()]
print "OK"