Skip to content

Commit

Permalink
Added unified config (IvanGlinkin#16)
Browse files Browse the repository at this point in the history
* unified config

* Update README.md

* cleanup
  • Loading branch information
ask0n authored May 5, 2024
1 parent 6b86702 commit b110d31
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 48 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
telegram_creds.py
config.yaml
reports-html
reports-json
avatars
*.session
*.session-journal
__pycache__/
.DS_Store
reports-*
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ register the application
get App's api_id, api_hash, title and name
```

3. Setting up Telegram creds
3. Settings<br>
Upon first launch script will create `config.yaml` file and request all needed settings.<br>
This settings can be manually changed later:
```
nano ./backend/telegram_creds.py
phone_number = 'your_phone_number' // put your phone number
telegram_name = 'api_name' // put your API name from the step 2
telegram_api_id = 'api_id' // put your API ID from the step 2
telegram_api_hash = 'api_hash' // put your API Hash from the step 2
api_config:
api_hash: ***
api_id: 00000000
phone: "+123456789000"
location:
lat: 51.51404
lon: -0.15063
meters: 1200
misc:
speed_kmh: 50
timesleep: 30
```

4. Launch
Expand Down
3 changes: 0 additions & 3 deletions backend/combine_data.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from .functions import combine_json_files
from .json_into_html import generate_html_from_json

def combine_data():
report_json_directory = "./reports-json/"

def combine_data(report_json_directory="./reports-json/", report_html_directory="./reports-html/"):

combine_json_files(report_json_directory, f"{report_json_directory}_combined_data.json")
Expand Down
61 changes: 60 additions & 1 deletion backend/functions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import sys
from concurrent.futures import ThreadPoolExecutor
from json import dump, load
from math import cos, pi, radians
from os import listdir, path
from time import sleep

import yaml
from bs4 import BeautifulSoup
from requests import get

Expand Down Expand Up @@ -54,8 +56,8 @@ def calculate_coordinates(lat, lon, direction, distance):
def combine_json_files(folder_path, output_file):
combined_data = {}
for filename in listdir(folder_path):
file_path = path.join(folder_path, filename)
if filename.endswith('.json') and filename != output_file:
file_path = path.join(folder_path, filename)
with open(file_path, 'r') as file:
data = load(file)
combined_data.update(data) # Merge dictionaries
Expand Down Expand Up @@ -129,3 +131,60 @@ def download_avatars(json_file, output_folder):
username = user_data.get('username', '')
user_url = f"https://t.me/{username}"
executor.submit(download_avatar, user_id, username, user_url, output_folder)

def create_config(file_path):
"""Create a custom YAML configuration file by asking the user for input."""

settings = {
'api_config': {
'phone': {'prompt': 'TG phone number: ', 'default': None, 'mandatory': True},
'api_id': {'prompt': 'TG api_id: ', 'default': None, 'mandatory': True, 'type': int},
'api_hash': {'prompt': 'TG api_hash:', 'default': None, 'mandatory': True},
},
'location': {
'lat': {'prompt': 'Starting latitude [51.51404]: ', 'default': 51.51404, 'mandatory': False, 'type': float},
'lon': {'prompt': 'Starting longitude [-0.15063]: ', 'default': -0.15063, 'mandatory': False, 'type': float},
'meters': {'prompt': 'Search radius(meters) [1200]: ', 'default': 1200, 'mandatory': False, 'type': int},
},
'misc': {
'timesleep': {'prompt': 'Waiting time between locations(sec) [30]: ', 'default': 30, 'mandatory': False, 'type': int},
'speed_kmh': {'prompt': 'Moving speed between locations(km/h) [50]: ', 'default': 50, 'mandatory': False, 'type': int},
}
}

# Create a dictionary to store the configurations
config_data = {group: {} for group in settings}

print("\nCheck README.md and prepare required values at https://my.telegram.org/auth")
# Iterate over the settings dictionary to prompt user for each value
for group, group_settings in settings.items():
for setting, details in group_settings.items():
while True: # Loop until valid input is given or the program exits
user_input = input(details['prompt'])
if details['mandatory'] and not user_input.strip(): # Check if the input is mandatory and empty
print(f"Value {setting} is mandatory. Exiting program.")
sys.exit() # Exit the program if input is mandatory and not provided
elif user_input.strip():
value = details['type'](user_input) if 'type' in details else user_input
config_data[group][setting] = value
break
elif details['default'] is not None: # Use default if available and input is not mandatory
config_data[group][setting] = details['default']
break
else:
print(f"No input provided for {setting}, and no default available. Exiting program.")
sys.exit() # Exit if no default is available and input is not provided

# Write the collected data to a YAML file
with open(file_path, 'w') as file:
yaml.safe_dump(config_data, file)
print(f"Config file created at {file_path}")

def load_config(file_path):
"""Load the YAML configuration file, creating it if it does not exist."""
if not path.exists(file_path):
print(f"No config file found at {file_path}. Creating initial configuration...")
create_config(file_path)

with open(file_path, 'r') as file:
return yaml.safe_load(file)
5 changes: 0 additions & 5 deletions backend/general_settings.py

This file was deleted.

4 changes: 0 additions & 4 deletions backend/telegram_creds.py

This file was deleted.

1 change: 0 additions & 1 deletion reports-html/_combined_data.html

This file was deleted.

1 change: 0 additions & 1 deletion reports-json/_combined_data.json

This file was deleted.

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
beautifulsoup4==4.12.3
PyYAML==6.0.1
Requests==2.31.0
telethon==1.35.0
48 changes: 22 additions & 26 deletions start.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from datetime import datetime
from json import dump
from os import getcwd, makedirs, path
from time import sleep

from backend.banners import (
banner,
Expand All @@ -28,10 +27,9 @@
countdown_timer,
download_avatars,
generate_pattern,
load_config,
)
from backend.general_settings import latitude, longitude, meters, speed_kmh, timesleep
from backend.json_into_html import generate_html_from_json
from backend.telegram_creds import telegram_api_hash, telegram_api_id, telegram_name
from telethon import functions, types
from telethon.sync import TelegramClient

Expand All @@ -43,32 +41,30 @@
parser.add_argument('-long', '--longitude', type=float, help='Longitude setting')
parser.add_argument('-m', '--meters', type=int, help='Meters setting')
parser.add_argument('-t', '--timesleep', type=int, help='Timesleep setting')
parser.add_argument('-s', '--speed_kmh', type=int, help='Speed setting')

# Add arguments for Telegram credentials
parser.add_argument('-tn', '--telegram_name', type=str, help='Telegram username')
parser.add_argument('-tn', '--telegram_name', type=str, help='Telegram session name')
parser.add_argument('-ti', '--telegram_api_id', type=int, help='Telegram API ID')
parser.add_argument('-th', '--telegram_api_hash', type=str, help='Telegram API hash')

# Parse the command-line arguments
args = parser.parse_args()

#Load or create config file
config_file="config.yaml"
config = load_config(config_file)
# Update settings if provided in command-line arguments
if args.latitude:
latitude = args.latitude
if args.longitude:
longitude = args.longitude
if args.meters:
meters = args.meters
if args.timesleep:
timesleep = args.timesleep

# Update Telegram credentials if provided in command-line arguments
if args.telegram_name:
telegram_name = args.telegram_name
if args.telegram_api_id:
telegram_api_id = args.telegram_api_id
if args.telegram_api_hash:
telegram_api_hash = args.telegram_api_hash
latitude = args.latitude if args.latitude else config['location']['lat']
longitude = args.longitude if args.longitude else config['location']['lon']
meters = args.meters if args.meters else config['location']['meters']
timesleep = args.timesleep if args.timesleep else config['misc']['timesleep']
speed_kmh = args.speed_kmh if args.speed_kmh else config['misc']['speed_kmh']
telegram_name = args.telegram_name if args.telegram_name else "cctv"
telegram_api_id = args.telegram_api_id if args.telegram_api_id else config['api_config']['api_id']
telegram_api_hash = args.telegram_api_hash if args.telegram_api_hash else config['api_config']['api_hash']
phone_number = config['api_config']['phone']


# General variables
pattern = generate_pattern((calculate_length(meters + 400) + 800) // 200) # Adjust the length as needed (x / 2 - 2)
Expand All @@ -92,11 +88,10 @@
report_json_directory = "./reports-json/"
report_html_directory = "./reports-html/"

# Check if the avatar directory exists, create it if not
if not path.exists(avatar_directory):
makedirs(avatar_directory)
if not path.exists(report_json_directory):
makedirs(report_json_directory)
# Check if the needed directories exist, create it if not
for dir in avatar_directory, report_json_directory, report_html_directory:
if not path.exists(dir):
makedirs(dir)

### Banner logo
print(banner)
Expand Down Expand Up @@ -127,7 +122,8 @@
# Initialize the Telegram client
print_telegram_initialization()

with TelegramClient(telegram_name, telegram_api_id, telegram_api_hash) as client:

with TelegramClient(telegram_name, telegram_api_id, telegram_api_hash, system_version="CCTV") as client:
# Authenticate the client
client.connect()
print_successfully()
Expand Down

0 comments on commit b110d31

Please sign in to comment.