Created
June 14, 2015 19:31
-
-
Save nux17/7e238bc6e7a66ced9a2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'nux' | |
import urllib | |
import urllib2 | |
import json | |
from selenium import webdriver | |
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities | |
from sys import stdout | |
import time | |
import pickle | |
import requests | |
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' | |
headers = {'User-Agent': user_agent} | |
class SteamHandlerReq(object): | |
def __init__(self, name, passwd): | |
self.session = requests.session() | |
self.uname = name | |
self.passwd = passwd | |
def load_cookies(self, cookies): | |
self.session.cookies = cookies | |
class SteamHandlerPhantom(object): | |
def __init__(self, name, passwd): | |
dcap = dict(DesiredCapabilities.PHANTOMJS) | |
dcap["phantomjs.page.settings.userAgent"] = (user_agent,) | |
self.uname = name | |
self.passwd = passwd | |
stdout.write('Starting driver...') | |
stdout.flush() | |
self.driver = webdriver.PhantomJS('/usr/bin/phantomjs', service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any', '--load-images=false'], desired_capabilities=dcap) | |
self.driver.start_client() | |
self.driver.start_session(dcap) | |
stdout.write(' OK\n') | |
stdout.flush() | |
self.key = [] | |
def __del__(self): | |
self.driver.close() | |
def is_logged(self): | |
try: | |
return self.driver.find_element_by_id('home_recommended_spotlight_notloggedin').is_displayed() | |
except: | |
return False | |
def get_cookies(self): | |
return self.driver.get_cookies() | |
def load_cookies(self, cookies): | |
for cookie in cookies: | |
self.driver.add_cookie(cookie) | |
self.driver.refresh() | |
def dump(self): | |
png = self.driver.get_screenshot_as_png() | |
f = open("out.png", "w") | |
f.write(png) | |
f.close() | |
def fetch_key(self): | |
url = 'https://steamcommunity.com/login/getrsakey/' | |
values = {'username': self.uname, 'donotcache': str(int(time.time() * 1000))} | |
post = urllib.urlencode(values) | |
req = urllib2.Request(url, post, headers) | |
response = urllib2.urlopen(req).read() | |
data = json.loads(response) | |
self.key.append(data["publickey_mod"]) | |
self.key.append(data["publickey_exp"]) | |
def captcha(self): | |
source = self.driver.find_element_by_id('captchaImg') | |
print 'Captcha needed at ' + source.get_attribute('src') | |
captcha = raw_input('Enter the captcha: ') | |
i = self.driver.find_element_by_id('input_captcha') | |
i.clear() | |
i.send_keys(captcha) | |
stdout.write('Validating captcha...') | |
stdout.flush() | |
self.driver.find_element_by_id('SteamLogin').click() | |
time.sleep(3) | |
try: | |
e = self.driver.find_element_by_id('error_display') | |
except: | |
e = None | |
if (e and e.is_displayed()) and not self.driver.find_element_by_id('loginAuthCodeModal').is_displayed(): | |
stdout.write(' ERROR\n') | |
stdout.flush() | |
print 'Error message: ' + e.text | |
self.captcha() | |
stdout.write(' OK\n') | |
stdout.flush() | |
def auth(self): | |
print 'Auth needed' | |
code = raw_input('Code: ') | |
name = raw_input('Navigator name: ') | |
i = self.driver.find_element_by_id('authcode') | |
i.clear() | |
i.send_keys(code) | |
i = self.driver.find_element_by_id('friendlyname') | |
i.clear() | |
i.send_keys(name) | |
self.driver.find_element_by_id('auth_buttonset_entercode').find_element_by_class_name('leftbtn').click() | |
stdout.write('Validating authentication...') | |
stdout.flush() | |
time.sleep(3) | |
e = self.driver.find_element_by_id('auth_message_incorrectcode') | |
if e.is_displayed(): | |
stdout.write(' ERROR\n') | |
stdout.flush() | |
print 'Incorrect code:' + e.text | |
self.auth() | |
e = self.driver.find_element_by_id('auth_buttonset_success') | |
if e.is_displayed(): | |
stdout.write(' OK\nProceed to login...\n') | |
stdout.flush() | |
self.driver.find_element_by_id('success_continue_btn').click() | |
else: | |
stdout.write(' ERROR\n') | |
stdout.flush() | |
print 'Weird error, back to logging...' | |
self.do_log() | |
def get(self, url): | |
self.driver.get(url) | |
def get_session(self): | |
return self.driver.session_id | |
def set_session(self, session): | |
self.driver.session_id = session | |
def do_log(self, cookie=False): | |
url = 'https://steamcommunity.com/login/' | |
self.driver.get(url) | |
i = self.driver.find_element_by_id('steamAccountName') | |
i.clear() | |
i.send_keys(self.uname) | |
i = self.driver.find_element_by_id('steamPassword') | |
i.clear() | |
i.send_keys(self.passwd) | |
if cookie: | |
self.driver.find_element_by_id('remember_login').click() | |
self.driver.find_element_by_id('SteamLogin').click() | |
time.sleep(5) | |
try: | |
if self.driver.find_element_by_id('captcha_entry').is_displayed(): | |
self.captcha() | |
except: | |
pass | |
try: | |
if self.driver.find_element_by_id('error_display').is_displayed(): | |
print 'Error :' + self.driver.find_element_by_id('error_display').text | |
return | |
except: | |
pass | |
try: | |
if self.driver.find_element_by_id('loginAuthCodeModal').is_displayed(): | |
self.auth() | |
except: | |
pass | |
if not cookie: | |
self.driver.execute_script("Logout();") | |
time.sleep(1) | |
print 'Saving session cookies...' | |
self.do_log(cookie=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment