Skip to content

Instantly share code, notes, and snippets.

@nyarurato
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nyarurato/363a602a8e22b7a1d9a3 to your computer and use it in GitHub Desktop.
Save nyarurato/363a602a8e22b7a1d9a3 to your computer and use it in GitHub Desktop.
steamの最安値を報告するためのスクリプト(今は最後の最安値を取ってくるだけ)
# coding:utf-8
import urllib2
import re
import datetime
import sys
def check_web(id,isapp):
try:
html = get_html(id,isapp)
name,time,price = parse_html(html)
except:
try:
html= get_html(id,not isapp)
name,time,price = parse_html(html)
except:
print u"エラー。番号間違えていないか確認してみてね"
print u"もしくは一度も値下がりしていないかも"
timedif = calc_time_dif(time)
s = make_str(timedif,name,price)
print(s)
def get_html(id,isapp):
apporsub = "app" if isapp else "sub"
try:
response = urllib2.urlopen("http://prosteamer.jp/"+apporsub+"/"+str(id))
charcode = response.headers.getparam("charset")
html = unicode(response.read(),"utf-8")
print html
return html
except:
print sys.exc_info()[0]
def parse_html(html):
try:
# 改行あるとパターンマッチが効かないようなので改行削除
deleten = re.compile(u"\n")
html = deleten.sub(u"",html)
# ゲームタイトル抜き出し
pattern = re.compile(u"<title>.+?</title>")
name = re.findall(pattern,html)
name = name[0][36:-8]
pattern = re.compile(u"<div class='sale_discount'>.{,50}最安値")
# パターンに合うものをリストにいれる
# [[u"<div class='sale_discount'>2014年08月15日(金) 02時09分18秒$3.59最安値"]]
items = re.findall(pattern,html)
# (曜日)が邪魔なので消す
youbipattern = re.compile(u"\(.\)")
n = len(items)
name_time_price = [name]
# 最初から27字,最後から3字を削除
# $で分離
# [[u"2014年08月15日 02時09分18秒",u"3.59"]]
time_price = re.sub(youbipattern,"",items[n-1][27:-3]).split(u"$")
# 時間はdatetimeにして管理
# [[datetime.datetime(2014, 8, 15, 2, 9, 18), u"3.59"]]
time_price[0] = datetime.datetime.strptime(time_price[0].encode("utf-8"),"%Y年%m月%d日 %H時%M分%S秒")
# 価格はstrからfloatに変換
# [[datetime.datetime(2014, 8, 15, 2, 9, 18), 3.59]]
time_price[1] = float(time_price[1])
name_time_price.extend(time_price)
return name_time_price
except :
print sys.exc_info()[0]
def calc_time_dif(time):
t = datetime.datetime.now() - time
return t
def make_str(time,name,price):
s = u"ゲーム名:" + name + "\n"
if(time.days != 0):
s +=u"今から"+ str(time.days)+u"日前"
else:
s +=u"今から"+ str(int(time.seconds/3600))+u"時間前"
s += u"に最安値 $"+str(price)+u"だったよ"
return s
def parse_url(url):
# url = "http://store.steampowered.com/app/334980/"
isapp = False
if("/app/" in url):
isapp = True
elif("/sub/" in url):
isapp = False
else:
print "URLが間違っていないか確認してください"
id = int(url.split("/")[-2])
return [id,isapp]
if __name__ == "__main__":
print "input steam url"
s = raw_input()
x,isapp = parse_url(s)
check_web(x,isapp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment