Skip to content

Commit 2621367

Browse files
authored
Add randomly generated user agents (#42)
1 parent 887655a commit 2621367

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,30 @@ Any methods that may be useful.
6868

6969
Some variables you may want to set.
7070

71-
`api.server_url` The growatt server URL, default: 'https://server.growatt.com/'
71+
`api.server_url` The growatt server URL, default: 'https://server-api.growatt.com/'
7272

7373
## Note
7474

7575
This is based on the endpoints used on the mobile app and could be changed without notice.
7676

77+
## Initialisation
78+
79+
The library can be initialised to introduce randomness into the User Agent field that is used when communicating with the servers.
80+
81+
This has been added since the Growatt servers started checking for the presence of a `User-Agent` field in the headers that are sent.
82+
83+
By default the library will use a pre-set `User-Agent` value which identifies this library while also appearing like an Android device. However, it is also possible to pass in parameters to the intialisation of the library to override this entirely, or just add a random ID to the value. e.g.
84+
85+
```python
86+
api = growattServer.GrowattApi() # The default way to initialise
87+
88+
api = growattServer.GrowattApi(True) # Adds a randomly generated User ID to the default User-Agent
89+
90+
api = growattServer.GrowattApi(False, "my_user_agent_value") # Overrides the default and uses "my_user_agent_value" in the User-Agent header
91+
```
92+
93+
Please see the `user_agent_options.py` example in the `examples` directory if you wish to investigate further.
94+
7795
## Examples
7896

7997
The `examples` directory contains example usage for the library. You are required to have the library installed to use them `pip install growattServer`. However, if you are contributing to the library and want to use the latest version from the git repository, simply create a symlink to the growattServer directory inside the `examples` directory.

examples/user_agent_options.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import growattServer
2+
import getpass
3+
4+
"""
5+
This is a simple script that demonstrates the various ways to initialise the library to set a User Agent
6+
"""
7+
8+
#Prompt user for username
9+
username=input("Enter username:")
10+
11+
#Prompt user to input password
12+
user_pass=getpass.getpass("Enter password:")
13+
14+
15+
16+
api = growattServer.GrowattApi()
17+
login_response = api.login(username, user_pass)
18+
print("Default initialisation")
19+
print("User-Agent: %s\nLogged in User id: %s" % (api.agent_identifier, login_response['userId']))
20+
print("")
21+
22+
api = growattServer.GrowattApi(True)
23+
login_response = api.login(username, user_pass)
24+
print("Add random ID to default User-Agent")
25+
print("User-Agent: %s\nLogged in User id: %s" % (api.agent_identifier, login_response['userId']))
26+
print("")
27+
28+
api = growattServer.GrowattApi(False, "my-user-id")
29+
login_response = api.login(username, user_pass)
30+
print("Override default User-Agent")
31+
print("User-Agent: %s\nLogged in User id: %s" % (api.agent_identifier, login_response['userId']))
32+
print("")
33+
34+
api = growattServer.GrowattApi(True, "my-user-id")
35+
login_response = api.login(username, user_pass)
36+
print("Override default User-Agent and add random ID")
37+
print("User-Agent: %s\nLogged in User id: %s" % (api.agent_identifier, login_response['userId']))
38+
print("")
39+
40+
api = growattServer.GrowattApi(False, growattServer.GrowattApi.agent_identifier + " - my-user-id")
41+
login_response = api.login(username, user_pass)
42+
print("Extend default User-Agent")
43+
print("User-Agent: %s\nLogged in User id: %s" % (api.agent_identifier, login_response['userId']))
44+
print("")
45+
46+
api = growattServer.GrowattApi(True, growattServer.GrowattApi.agent_identifier + " - my-user-id")
47+
login_response = api.login(username, user_pass)
48+
print("Extend default User-Agent and add random ID")
49+
print("User-Agent: %s\nLogged in User id: %s" % (api.agent_identifier, login_response['userId']))
50+
print("")
51+

growattServer/__init__.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
import requests
88
import warnings
9+
from random import randint
910

1011
def hash_password(password):
1112
"""
@@ -24,10 +25,20 @@ class Timespan(IntEnum):
2425

2526
class GrowattApi:
2627
server_url = 'https://server-api.growatt.com/'
28+
agent_identifier = "Dalvik/2.1.0 (Linux; U; Android 12; https://github.com/indykoning/PyPi_GrowattServer)"
29+
30+
def __init__(self, add_random_user_id=False, agent_identifier=None):
31+
if (agent_identifier != None):
32+
self.agent_identifier = agent_identifier
33+
34+
#If a random user id is required, generate a 5 digit number and add it to the user agent
35+
if (add_random_user_id):
36+
random_number = ''.join(["{}".format(randint(0,9)) for num in range(0,5)])
37+
self.agent_identifier += " - " + random_number
2738

28-
def __init__(self):
2939
self.session = requests.Session()
30-
headers = {'User-Agent': 'Dalvik/2.1.0 (Linux; U; Android Device)'}
40+
41+
headers = {'User-Agent': self.agent_identifier}
3142
self.session.headers.update(headers)
3243

3344
def __get_date_string(self, timespan=None, date=None):

0 commit comments

Comments
 (0)