Created
November 16, 2016 22:28
-
-
Save Islati/4b5aca20e814fee8fb321191ee5c5569 to your computer and use it in GitHub Desktop.
Basis for a web-requests based Upwork API for Python
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
import requests | |
import logging | |
from bs4 import BeautifulSoup | |
logger = logging.getLogger("upwork-api") | |
class UpworkAPI(object): | |
_LOGIN_URL = "https://www.upwork.com/ab/account-security/login" | |
_HEADERS = { | |
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.90 Safari/537.36', | |
'Origin': 'https://www.upwork.com', | |
'Upgrade-Insecure-Requests': '1', | |
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' | |
} | |
def __init__(self, username, password): | |
self.logged_in = False | |
self.username = username | |
self.password = password | |
self._session = requests.Session() | |
self._setup() | |
def _setup(self): | |
self._session.headers.update(self._HEADERS) | |
self.logged_in = self._login() | |
if not self.logged_in is True: | |
raise Exception("Login to upwork was unsuccessful!") | |
def _login(self): | |
headers = self._HEADERS.copy() | |
headers.update({ | |
'Referer': 'https://www.upwork.com/ab/account-security/login', | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Accept-Language': 'en-GB,en-US;q=0.8,en;q=0.6', | |
}) | |
request = self._session.get(url=self._LOGIN_URL) | |
html = request.text | |
soup = BeautifulSoup(html, 'html.parser') | |
try: | |
login_token = soup.find("input", {'id': 'login__token'}).get('value') | |
except: | |
return False | |
login_request = self._session.post( | |
url=self._LOGIN_URL, | |
data={ | |
'login[_token]': login_token, | |
'login[iovation]': '', | |
'login[password]': self.password, | |
'login[redir]': '/home', | |
'login[username]': self.username | |
} | |
) | |
if "FindWorkHomeUI" in login_request.text: | |
print("Logged In Successfully") | |
return True | |
print("Failed to Login") | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
now this snippet not working