Skip to content

Instantly share code, notes, and snippets.

@Pusungwi
Created November 14, 2013 14:12
Show Gist options
  • Select an option

  • Save Pusungwi/7467430 to your computer and use it in GitHub Desktop.

Select an option

Save Pusungwi/7467430 to your computer and use it in GitHub Desktop.
불면증엔 국화차
#######################################################
# GukhwaTea
# 불면증엔 국화차가 최고
# Ver 0.1
# Author : Yi Yeon Jae (pusungwi@gmail.com)
#######################################################
from twitter import *
import os
import urllib.request
import io
import json
import lxml.html
import time, datetime
SEARCH_URL = 'http://www.gog.com'
TWT_CONSUMER_APP_NAME = "SSSNotifier"
TWT_CONSUMER_KEY = ''
TWT_CONSUMER_SECRET = ''
MY_TWITTER_CREDS = os.path.expanduser('~/.sss_creds')
VOTE_SALE_TXT_PATH = os.path.expanduser('vSaleList')
def realCheckAndPostSaleStatus(filePath, targetList, format):
isAlreadyPosted = False
if os.path.exists(filePath):
with open(filePath) as f:
resultDict = json.load(f)
savedFSaleList = resultDict['data']
for savedFSaleItem in savedFSaleList:
if targetList[0]['name'] == savedFSaleItem['name']: #이름이 같으면 이미 처리한걸로 간주하고 보냄
print('already posted.')
isAlreadyPosted = True
if isAlreadyPosted == False:
if os.path.exists(filePath):
os.remove(filePath)
saleDict = {'data':targetList, 'version':0.1}
with open(filePath, 'w') as f:
json.dump(saleDict, f)
for saleItem in targetList:
tmpStr = format % (saleItem['url'], saleItem['name'], saleItem['price'], saleItem['discountPercentage'], saleItem['quantity'])
if len(tmpStr) > 140:
tmpStr = format % (saleItem['url'], saleItem['name'][0:20] + '... ', saleItem['price'], saleItem['discountPercentage'], saleItem['quantity'])
try:
dSearchDict = twt.search.tweets(q=tmpStr)
if len(dSearchDict['statuses']) != 0:
for dTweet in dSearchDict['statuses']:
if dTweet['source'].count(TWT_CONSUMER_APP_NAME) == 1:
print('removing duplicate tweet...')
twt.statuses.destroy(id=dTweet['id'])
break
twt.statuses.update(status=tmpStr)
except TwitterHTTPError as err:
print('status error - {0}'.format(err))
else:
print(tmpStr)
# twitter auth
if not os.path.exists(MY_TWITTER_CREDS):
oauth_dance(TWT_CONSUMER_APP_NAME, TWT_CONSUMER_KEY, TWT_CONSUMER_SECRET, MY_TWITTER_CREDS)
oauth_token, oauth_secret = read_token_file(MY_TWITTER_CREDS)
# auth end
twt = Twitter(auth=OAuth(oauth_token, oauth_secret, TWT_CONSUMER_KEY, TWT_CONSUMER_SECRET))
def checkAndPostSaleStatus():
print('running... (%s)' % datetime.datetime.now())
try:
requestFullUrl = SEARCH_URL
recvSearchHtml = urllib.request.urlopen(requestFullUrl)
except IOError:
print("URL address Error")
else:
recvRawHtml = recvSearchHtml.read()
recvRawDecodedHtml = recvRawHtml.decode('utf-8')
recvParsedHtml = lxml.html.fromstring(recvRawDecodedHtml)
insomniacDict = {}
leftGameHtml = recvParsedHtml.cssselect('div.game__left')
for tmpTree in leftGameHtml:
insomniacDict['quantity'] = tmpTree.cssselect('span.js-left-count')[0].text
gameInfoHtml = recvParsedHtml.cssselect('div.game__info')
for tmpTree in gameInfoHtml:
insomniacDict['name'] = tmpTree.cssselect('a.game__title')[0].text
insomniacDict['discountPercentage'] = tmpTree.cssselect('b.game__discount')[0].text[1:4]
insomniacDict['price'] = recvParsedHtml.cssselect('div.game__price')[0].text[5:]
gameLinkHtml = recvParsedHtml.cssselect('a.game__title')
for tmpTree in gameLinkHtml:
insomniacDict['url'] = SEARCH_URL + tmpTree.attrib['href']
#DEBUG CODE
print(insomniacDict)
realCheckAndPostSaleStatus(VOTE_SALE_TXT_PATH, [insomniacDict], '%s [NEW] GOG.com 불면증 세일 :%s(%s, -%s, %s개 한정) #gogsalebot')
if __name__ == "__main__":
while True:
checkAndPostSaleStatus()
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment