Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyagreco committed Sep 3, 2024
1 parent 0d10155 commit 4f835d7
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,5 @@ dmypy.json

# Pyre type checker
.pyre/

*.dnc.py
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.10.14
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

- N/A

## [1.1.0]

- Fixed cooldown time enforcing
- Use seed comprised of username and SID when determining User Agent

## [1.0.1]

Expand All @@ -17,7 +22,5 @@ All notable changes to this project will be documented in this file.
### Initial Release

[Unreleased]: https://github.com/joeyagreco/pythontextnow/compare/v1.0.1...HEAD

[1.0.1]: https://github.com/joeyagreco/pythontextnow/releases/tag/v1.0.1

[1.0.0]: https://github.com/joeyagreco/pythontextnow/releases/tag/v1.0.0
[1.0.0]: https://github.com/joeyagreco/pythontextnow/releases/tag/v1.0.0
28 changes: 20 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
# INSTALL DEPENDENCIES
.PHONY: deps
deps:
@python3.10 -m pip install -r requirements.dev.txt
@python3.10 -m pip install -r requirements.txt
@python -m pip install -r requirements.dev.txt
@python -m pip install -r requirements.txt

# FORMAT PYTHON CODE
.PHONY: fmt
fmt:
@black --config=pyproject.toml .
@autoflake --config=pyproject.toml .
@isort .
@ruff check --fix
@ruff format

.PHONY: fmt-check
fmt-check:
@ruff check
@ruff format --check

# RUN UNIT TESTS
.PHONY: test
test:
@pytest

.PHONY: pkg-build
pkg-build:
@rm -rf build
@rm -rf dist
@python setup.py sdist bdist_wheel

.PHONY: pkg-test
pkg-test:
@python -m twine upload --repository-url https://test.pypi.org/legacy/ dist/*

.PHONY: pkg-prod
pkg-prod:
@python -m twine upload dist/*
17 changes: 2 additions & 15 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,2 @@
[tool.black]
line-length = 100
skip-magic-trailing-comma = true
include = '\.py$'
quiet = true

[tool.isort]
profile = "black"
line_length = 100

[autoflake]
ignore_init_module_imports = true
in_place = true
recursive = true
remove_all_unused_imports = true
[tool.ruff.lint]
select = [ "I001" ]
2 changes: 1 addition & 1 deletion pythontextnow/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.0.1"
__version__ = "1.1.0"
11 changes: 10 additions & 1 deletion pythontextnow/api/Client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import datetime
import hashlib
from dataclasses import dataclass
from typing import Optional

Expand Down Expand Up @@ -28,7 +29,15 @@ class Client:

@classmethod
def set_client_config(cls, *, username: str, sid_cookie: str) -> None:
headers = {"user-agent": get_random_user_agent(), "Cookie": f"connect.sid={sid_cookie};"}
# use the same user agent for the same username + sid cookie combo
# we do this by creating a hash of them and then using it as a seed
hash: int = int(
hashlib.sha256(f"{username}+{sid_cookie}".encode()).hexdigest(), 16
)
headers = {
"user-agent": get_random_user_agent(hash),
"Cookie": f"connect.sid={sid_cookie};",
}

client_config = ClientConfig(
username=username,
Expand Down
10 changes: 7 additions & 3 deletions pythontextnow/util/general.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import random
import re
from typing import Optional

from random_user_agent.params import OperatingSystem, SoftwareName
from random_user_agent.user_agent import UserAgent
Expand All @@ -8,12 +10,14 @@ def replace_newlines(text: str):
return re.sub(r"(?<!\\)\n", r"\\n", text)


def get_random_user_agent() -> str:
def get_random_user_agent(seed: Optional[int] = None) -> str:
software_names = [SoftwareName.CHROME.value]
operating_systems = [OperatingSystem.WINDOWS.value, OperatingSystem.LINUX.value]
user_agent_rotator = UserAgent(
software_names=software_names, operating_systems=operating_systems, limit=100
)

# Get Random User Agent String.
return user_agent_rotator.get_random_user_agent()
if seed is not None:
random.seed(seed)

return random.choice(user_agent_rotator.get_user_agents())["user_agent"]
9 changes: 4 additions & 5 deletions requirements.dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
black~=23.3.0
autoflake~=2.1.1
isort~=5.12.0
pipreqs~=0.4.13
pytest~=7.4.4
wheel~=0.42.0
pytest~=8.0.0
twine~=5.1.1
ruff~=0.5.7

0 comments on commit 4f835d7

Please sign in to comment.