Enhance file parsing and update README to reflect configuration format improvements

This commit is contained in:
afolivieri 2024-04-23 16:37:21 +03:00
parent 553125721f
commit 23e01315ba
2 changed files with 19 additions and 9 deletions

View File

@ -52,17 +52,23 @@ The script uses several files for configuration:
- `**` allows up to six additional characters.
- `#` at the start includes numeric characters (up to three).
- `##` includes numeric characters (up to six).
- `channels.txt`: List of channel usernames (the part after `t.me/`) to monitor (NB You need to JOIN the channel you want to monitor).
**Note: Separate keywords using commas. Keywords can span multiple lines for readability; newlines are ignored.**
- `channels.txt`: List of channel usernames (the part after `t.me/`) to monitor
- Separate channel names with commas, and feel free to list them across multiple lines for better readability. Newlines between names are ignored.
**Note: Ensure you have joined any channel you wish to monitor.**
### Examples
- `keywords.txt`
```
hello*, world**, #123, ##123456, #or_any_combination**
hello*, world**, #123,
##123456, #or_any_combination**
```
- `channels.txt`
```
python, daily_news, target3, target4
python, daily_news,
target3, target4
```
@ -77,7 +83,7 @@ python streaming_overseer.py
## Operational Details
- If not yet saved, the script will ask for your API ID, API hash, phone number, username, and bot token.
- The script listens for new messages in the specified channels.
- The script listens for new messages in the specified channels (You need to have joined the channels you intend to monitor).
- If a message contains any of the specified keywords, it is forwarded to the private channel.
- The script sends a startup message to the private channel when it begins monitoring.
- Use Ctrl+C to safely shut down the monitoring tool.

View File

@ -48,10 +48,15 @@ async def fetch_channel_id(credentials):
return credentials
def load_patterns():
with open(KEYWORDS_FILE, 'r', encoding='utf-8') as file:
keywords = [x.strip() for x in file.read().split(',')]
def load_entries_from_file(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
entries = [x.strip() for x in content.replace('\n', '').split(',') if x.strip()]
return entries
def load_patterns():
keywords = load_entries_from_file(KEYWORDS_FILE)
word_patterns = {}
emoji_pattern = r'[\U0001F600-\U0001F64F\U0001F300-\U0001F5FF\U0001F680-\U0001F6FF\U0001F1E0-\U0001F1FF]'
@ -74,7 +79,6 @@ def load_patterns():
return word_patterns
# Function to handle signals
def signal_handler(signal, frame):
print('Detected Ctrl+C! Gracefully shutting down.')
exit(0)
@ -86,7 +90,7 @@ async def main():
await client.start(phone=creds['phone'])
word_patterns = load_patterns()
channels = [x.strip() for x in open(CHANNELS_FILE, 'r').read().split(',')]
channels = load_entries_from_file(CHANNELS_FILE)
channel_id = creds['channel_id'] # Use the channel ID from credentials
await client.send_message(channel_id, f"Listening to {', '.join(channels)}...")