本文整理汇总了Python中db.Database.insert_link方法的典型用法代码示例。如果您正苦于以下问题:Python Database.insert_link方法的具体用法?Python Database.insert_link怎么用?Python Database.insert_link使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类db.Database
的用法示例。
在下文中一共展示了Database.insert_link方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: LinkGetter
# 需要导入模块: from db import Database [as 别名]
# 或者: from db.Database import insert_link [as 别名]
class LinkGetter(object):
def __init__(self):
self.db = Database()
print "Listening to %s" % ADDRESS
socketIO = SocketIO(ADDRESS, 443, secure=True)
socketIO.on('message', self.on_message)
socketIO.wait()
def do_the_thing(self, key, message):
groups = re.finditer(LINKY, message)
for g in groups:
try:
response = requests.get(g.group(0))
if response.status_code == 200:
print "Found a link! %s" % message
self.db.insert_link(key, message, g.group(0))
except:
print "Bad link"
def on_message(self, *args):
try:
data = args[0]
message = data['chat']['value']['message']
key = data['chat']['key']
print message
self.do_the_thing(key, message)
except:
print "never crash because crashing would be bad"
示例2: LinkGetter
# 需要导入模块: from db import Database [as 别名]
# 或者: from db.Database import insert_link [as 别名]
class LinkGetter(object):
def __init__(self):
self.db = Database()
print "Listening to %s" % ADDRESS
def listen(self):
socketIO = SocketIO(ADDRESS, 443, secure=True)
socketIO.on('message', self.on_message)
socketIO.wait()
def is_link_to_music(self, url, response, message):
netloc = urlparse(url).netloc.lower()
#Sound cloud links
if netloc == 'soundcloud.com':
return True
#Youtube links in the music category
if netloc == 'youtube.com':
soup = BeautifulSoup(response.content)
category = soup.find('p', {'id' : 'eow-category'}).text.lower()
return category == 'music'
return 'musicbot' in message
def do_the_thing(self, key, message):
if self.db.link_exists(key):
return
groups = re.finditer(LINKY, message)
for g in groups:
try:
response = requests.get(g.group(0))
if response.status_code == 200:
print "Found a link! %s" % message
url = g.group(0)
is_music = self.is_link_to_music(url, response, message)
self.db.insert_link(key, message, url, is_music)
except Exception as e:
print e
print "Bad link"
def on_message(self, *args):
try:
data = args[0]
message = data['chat']['value']['message']
key = data['chat']['key']
self.do_the_thing(key, message)
except Exception as e:
print e
print "never crash because crashing would be bad"