使用的语言:
概览
我希望能够跟踪用户何时登录并注销游戏服务器。
像
ID,用户名,激活,已登录,已退出
示例:
1,弓箭手,在线,11-24-2015 05:10:11,11-24-2015 12:10:11
使用这些数据我想创建一个网站,显示特定玩家在线时间最常见的图表,最佳游戏时间(当大多数玩家在线时)等。
我用来计算所有这些的唯一数据是在游戏网站上发布的“谁在线”列表。
所以我所做的就是我编写了一个python脚本,每1分钟检查一次列表(用BS4过滤掉所有HTML,并在线转储到用户的文本文件1),它能够通过与前一分钟的列表进行比较,找出谁签约并签字的人。
现在我想解决的问题是:
问题:
目前,该脚本只是将新在线用户或新离线用户转储到文本文件中。
我需要像上面说的那样更新MySQL数据库。
我可以谷歌并找出如何让python每次上线时更新数据库 - 只需创建一个新行,自动增加ID,输入用户名,以及他们登录时的时间戳。
但我不知道以后我怎么能去更新那一行来添加Logged Off时间。我怎么知道要更新哪一行?
将有多个具有相同用户名的行,因为每个登录/注销会话都会添加一个新行!我知道它可能必须通过ID找到,但我该如何跟踪它呢?
感谢!!!!!
我有什么:
# IMPORT WEB BROWSER
import mechanize
# IMPORT HTML PARSER
from bs4 import BeautifulSoup, NavigableString, Tag
# FOR TIME CALCS
from datetime import datetime, timedelta, date
# FOR RENAMING FILES
import os
# FOR SLEEP
import time
# FOR TIMESTAMPS
import datetime
# Used for timestamps when writing to database [DB not used yet]
ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
###############################################################
# BEGIN NEVER ENDING LOOP THAT SLEEPS 1 MINUTE BETWEEN CYCLES #
###############################################################
# This will eventually be removed and will run as a 1min cronjob when ready
while True:
#########################################
# SETUP MECAHNIZE TO USE AS WEB BROWSER #
#########################################
br = mechanize.Browser()
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
###########################################
# GET "WHO'S ONLINE?" LIST FROM GAME SITE #
###########################################
try:
# User-Agent
br.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko')]
r = br.open('http://en.war2.ru/wp-content/themes/war2.ru/lib/server-stats.php')
page = r.read()
soup = BeautifulSoup(page)
user_list = soup.find_all('div', class_='status-list')
except:
print "ERROR: Connection failed. Retrying ..."
continue
#################################################
# ERASE USERS ONLINE THIS MINUTE FROM LAST TIME #
#################################################
with open("users-on-this-minute.txt", "w") as f:
f.close()
#########################################################
# FILTER OUT HTML AND GIVE A PLAIN LIST OF USERS ONLINE #
#########################################################
for br in user_list:
users = br.findAll('br')
for br in users:
next = br.nextSibling
if not (next and isinstance(next,NavigableString)):
continue
next2 = next.nextSibling
if next2 and isinstance(next2,Tag) and next2.name == 'br':
text = str(next.encode('latin1')).strip()
if text:
output = next[1:].encode('latin1')
####################################################
# PRINT EACH ONLINE USER TO TEXT FILE ONE PER LINE #
####################################################
file = open("users-on-this-minute.txt", "at")
print >>file, str(output)
file.close
############################
# CREATE NEWLY ONLINE LIST #
############################
# Creates the Newly Online list by checking the users logged on now
# against the users logged on a minute ago. If there is a user logged on
# now who wasn't on a minute ago, he is added to newly online
with open("users-on-previous-minute.txt", "r") as f:
check_online = f.readlines()
new_online = set() # holds lines already seen
outfile = open("users-newly-online.txt", "w")
for line in open("users-on-this-minute.txt", "r"):
if line not in check_online: # not a duplicate
new_online.add(line)
outfile.writelines(sorted(new_online))
outfile.close()
f.close()
#############################
# CREATE NEWLY OFFLINE LIST #
#############################
# Creates the Newly Offline list by checking the users logged on now
# against the users logged on a minute ago. If there was a
# user logged on a minute ago but not now, he is added to newly offline
with open("users-on-this-minute.txt", "r") as f:
check_offline = f.readlines()
new_offline = set() # holds lines already seen
outfile = open("users-newly-offline.txt", "w")
for line in open("users-on-previous-minute.txt", "r"):
if line not in check_offline: # not a duplicate
new_offline.add(line)
outfile.writelines(sorted(new_offline))
outfile.close()
f.close()
#############################################################
# COPY CURRENT MINUTE LIST TO BECOME "PREVIOUS" MINUTE LIST #
#############################################################
# We are done using the list from the previous minute, and now we write
# the current minute's list to become "previous minute" for next loop
with open("users-on-this-minute.txt", "r") as f:
lines = f.readlines()
with open("users-on-previous-minute.txt", "w") as f1:
f1.writelines(lines)
f1.close()
f.close()
print "Sleeping for 1 minute ..."
time.sleep(60)
答案 0 :(得分:0)
我相信你让这有点太复杂了。以下是您的问题的要点:
但我不知道以后我怎么能去更新那一行来添加Logged Off时间。我怎么知道要更新哪一行?
将有多个具有相同用户名的行,因为每个登录/注销会话都会添加一个新行!我知道它可能必须通过ID找到,但我该如何跟踪它呢?
由于您拥有用户名,因此您只需更新该用户名没有注销时间的行。
"UPDATE logtable SET logouttime = NOW() WHERE username = " + username + " AND logouttime IS NULL"
当您设置数据库时,logouttime
的默认值为NULL
。