Initial commit
parent
fbb1247ebf
commit
4eba664d20
@ -1,3 +1,69 @@
|
|||||||
# selenium-energy-bot
|
# selenium-energy-bot_v2018
|
||||||
|
|
||||||
The project contains a selenium bot which was created to automate the Energy Air or Energy Star Night competition. It is only intended as a POC and not to be used to win real tickets. (If you want to win tickets with it, you do it at your own risk.)
|
The project contains a selenium bot which was created to automate the Energy Air or Energy Star Night competition. It is only intended as a POC and not to be used to win real tickets. (If you want to win tickets with it, you do it at your own risk.)
|
||||||
|
|
||||||
|
It is written in Python3 and requires an Android app to redirect the SMS.
|
||||||
|
After successful implementation of the Smartphone app and the Python Bot, the SMS code is automatically read after the end of the session and the game starts from the beginning.
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
[Download Python 3.7.4](https://www.python.org/ftp/python/3.7.4/python-3.7.4.exe)\
|
||||||
|
After downloading the file, run it. During the installation it is important to note that the check mark under "Add Python 3.7 to PATH" is set.\
|
||||||
|
Now open CMD and navigate to the folder "selenium-energy-bot":
|
||||||
|
```
|
||||||
|
cd \path\to\your\folder\selenium-energy-bot
|
||||||
|
```
|
||||||
|
There virtualenv must be installed:
|
||||||
|
```
|
||||||
|
pip3 install virtualenv
|
||||||
|
virtualenv venv
|
||||||
|
```
|
||||||
|
or:
|
||||||
|
```
|
||||||
|
python -m .\venv
|
||||||
|
```
|
||||||
|
Afterwards:
|
||||||
|
```
|
||||||
|
venv\Scripts\activate.bat
|
||||||
|
pip install -r requirements.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
### Gmail Account needed
|
||||||
|
* Click on [Enable The GMAIL API](https://developers.google.com/gmail/api/quickstart/python) and "Download Client Config".
|
||||||
|
* In the Gmail Inbox [mail.google.com](https://mail.google.com/) create a new "label".
|
||||||
|
* Under Settings (top right), go to "Filters & blocked addresses "Create new filter".
|
||||||
|
* As subject: *Forward SMS message from number [+41YOURNR] (sender - Energy (No contact specified))*
|
||||||
|
* Contains the words: *Dein Code für game.energy.ch*
|
||||||
|
* Create a filter and apply it to the newly created label.
|
||||||
|
|
||||||
|
### Download APP PlayStore
|
||||||
|
* Download App [SMS to mail/phone - auto redirect](https://play.google.com/store/apps/details?id=com.gawk.smsforwarder)
|
||||||
|
* Configure the app to forward the SMS to your previously configured Gmail.
|
||||||
|
* Attention: The app must remain open in the background, so do not delete the app from the "open apps". Possibly adjust the energy plan on your mobile phone.
|
||||||
|
|
||||||
|
### App Iphone (optional)
|
||||||
|
* If you don't have an Android, you can also use a corresponding app from the Appstore, which redirects SMS to Mail. The regex to find the code in the string has to be adjusted in the gmail_nrg_code.py script.\
|
||||||
|
Possible App: [mysms mirror: Forward SMS](https://apps.apple.com/ch/app/mysms-mirror-sms-weiterleiten/id681057282)
|
||||||
|
|
||||||
|
### Other Downloads (REQUIRED)
|
||||||
|
* Download the [Chrome Webdriver](https://chromedriver.chromium.org/downloads) in the appropriate version for your browser.\
|
||||||
|
Find out the Webdriver version:
|
||||||
|
1. open Chrome Browser
|
||||||
|
2. click on the three points in the upper right corner
|
||||||
|
3. help, then "about Google Chrome"
|
||||||
|
4. at the top you will see "Version 78.0.3904.97"
|
||||||
|
* Alternatively you can also use webdriver of Edge or IE. But I personally had the best experiences with Chrome (adjust the webdriver in the script nggame_2019.py if you want to change it).
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
### Unique execution
|
||||||
|
* Place credentials.json (GMAIL Api Auth) and chromedriver.exe in the project folder.
|
||||||
|
* In gmail_nrg_code.py there is a place (marked with Uncomment). Comment this block (Attention: After the "#" there is still a space, delete it).
|
||||||
|
* Comment the function (main()) at the last place in the code and execute the script.
|
||||||
|
```
|
||||||
|
python gmail_nrg_code.py
|
||||||
|
```
|
||||||
|
* Copy the id of the label and paste it at the following code position:
|
||||||
|
* ```results = service.users().messages().list(userId='me', labelIds=['UNREAD', 'Label_YOURIDHERE'],```
|
||||||
|
* Comment code out again. Also the function at the end!
|
||||||
|
|
||||||
|
### Execute
|
||||||
|
```python energy-bot.py```
|
@ -0,0 +1,80 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import pickle
|
||||||
|
import os.path
|
||||||
|
from googleapiclient.discovery import build
|
||||||
|
from google_auth_oauthlib.flow import InstalledAppFlow
|
||||||
|
from google.auth.transport.requests import Request
|
||||||
|
import re
|
||||||
|
|
||||||
|
# If modifying these scopes, delete the file token.pickle.
|
||||||
|
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly', 'https://www.googleapis.com/auth/gmail.modify']
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Shows basic usage of the Gmail API.
|
||||||
|
Lists the user's Gmail labels.
|
||||||
|
"""
|
||||||
|
creds = None
|
||||||
|
# The file token.pickle stores the user's access and refresh tokens, and is
|
||||||
|
# created automatically when the authorization flow completes for the first
|
||||||
|
# time.
|
||||||
|
if os.path.exists('token.pickle'):
|
||||||
|
with open('token.pickle', 'rb') as token:
|
||||||
|
creds = pickle.load(token)
|
||||||
|
# If there are no (valid) credentials available, let the user log in.
|
||||||
|
if not creds or not creds.valid:
|
||||||
|
if creds and creds.expired and creds.refresh_token:
|
||||||
|
creds.refresh(Request())
|
||||||
|
else:
|
||||||
|
flow = InstalledAppFlow.from_client_secrets_file(
|
||||||
|
'credentials.json', SCOPES)
|
||||||
|
creds = flow.run_local_server(port=0)
|
||||||
|
# Save the credentials for the next run
|
||||||
|
with open('token.pickle', 'wb') as token:
|
||||||
|
pickle.dump(creds, token)
|
||||||
|
|
||||||
|
service = build('gmail', 'v1', credentials=creds)
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
"""Uncomment to print label_name + label_id
|
||||||
|
is needed to determine Energy Label id.
|
||||||
|
"""
|
||||||
|
# results = service.users().labels().list(userId='me').execute()
|
||||||
|
#
|
||||||
|
# labels = results.get('labels', [])
|
||||||
|
#
|
||||||
|
# if not labels:
|
||||||
|
# print('No labels found.')
|
||||||
|
# else:
|
||||||
|
# print('Labels:')
|
||||||
|
# for label in labels:
|
||||||
|
# print(label['name'] + " " + label['id'])
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
# Call the Gmail API to fetch INBOX Energy, only unread messages
|
||||||
|
results = service.users().messages().list(userId='me', labelIds=['UNREAD', 'Label_5204902350138961179'],
|
||||||
|
maxResults=1).execute()
|
||||||
|
messages = results.get('messages', [])
|
||||||
|
|
||||||
|
if not messages:
|
||||||
|
print("No messages found.")
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
print("Message snippets:")
|
||||||
|
for message in messages:
|
||||||
|
msg = service.users().messages().get(userId='me', format='full', id=message['id']).execute()
|
||||||
|
# marks mail as read
|
||||||
|
service.users().messages().modify(userId='me', id=message['id'],
|
||||||
|
body={'removeLabelIds': ['UNREAD']}).execute()
|
||||||
|
snippet = msg['snippet']
|
||||||
|
print(snippet)
|
||||||
|
verification_code = str(snippet).split("Dein Code für game.energy.ch")[-1]
|
||||||
|
verification_code = verification_code.split(re.match('[0-3][0-9]/[0-1][0-9]/2019', verification_code))[
|
||||||
|
0].strip()
|
||||||
|
print(verification_code)
|
||||||
|
return verification_code
|
||||||
|
###############################################################################
|
||||||
|
"""uncomment to run once to fetch label id or for development"""
|
||||||
|
#if __name__ == '__main__':
|
||||||
|
# main()
|
||||||
|
###############################################################################
|
Loading…
Reference in New Issue