Merge pull request #1 from ThBroth/main

changed if/else to match/case and added list_Channels()
This commit is contained in:
unnohwn
2024-11-10 22:55:21 +01:00
committed by GitHub
2 changed files with 60 additions and 44 deletions

View File

@@ -1,4 +1,3 @@
telethon telethon
aiohttp aiohttp
sqlite3
asyncio asyncio

View File

@@ -257,60 +257,77 @@ async def view_channels():
for channel, last_id in state['channels'].items(): for channel, last_id in state['channels'].items():
print(f"Channel ID: {channel}, Last Message ID: {last_id}") print(f"Channel ID: {channel}, Last Message ID: {last_id}")
async def list_Channels():
try:
print("\nList of channels joined by account: ")
async for dialog in client.iter_dialogs():
if (dialog.id != 777000):
print(f"* {dialog.title} (id: {dialog.id})")
except Exception as e:
print(f"Error processing: {e}")
async def manage_channels(): async def manage_channels():
while True: while True:
print("\nMenu:") print("\nMenu:")
print("[A] Add new channel") print("[A] Add new channel")
print("[R] Remove channel") print("[R] Remove channel")
print("[S] Scrape all channels") print("[S] Scrape all channels")
print("[M] Toggle media scraping (currently {})".format("enabled" if state['scrape_media'] else "disabled")) print("[M] Toggle media scraping (currently {})".format(
"enabled" if state['scrape_media'] else "disabled"))
print("[C] Continuous scraping") print("[C] Continuous scraping")
print("[E] Export data") print("[E] Export data")
print("[V] View channels") print("[V] View saved channels")
print("[L] List account channels")
print("[Q] Quit") print("[Q] Quit")
choice = input("Enter your choice: ").lower() choice = input("Enter your choice: ").lower()
if choice == 'a': match (choice):
channel = input("Enter channel ID: ") case 'a':
state['channels'][channel] = 0 channel = input("Enter channel ID: ")
save_state(state) state['channels'][channel] = 0
print(f"Added channel {channel}.")
elif choice == 'r':
channel = input("Enter channel ID to remove: ")
if channel in state['channels']:
del state['channels'][channel]
save_state(state) save_state(state)
print(f"Removed channel {channel}.") print(f"Added channel {channel}.")
else: case 'r':
print(f"Channel {channel} not found.") channel = input("Enter channel ID to remove: ")
elif choice == 's': if channel in state['channels']:
for channel in state['channels']: del state['channels'][channel]
await scrape_channel(channel, state['channels'][channel]) save_state(state)
elif choice == 'm': print(f"Removed channel {channel}.")
state['scrape_media'] = not state['scrape_media'] else:
save_state(state) print(f"Channel {channel} not found.")
print(f"Media scraping {'enabled' if state['scrape_media'] else 'disabled'}.") case 's':
elif choice == 'c': for channel in state['channels']:
global continuous_scraping_active await scrape_channel(channel, state['channels'][channel])
continuous_scraping_active = True case 'm':
task = asyncio.create_task(continuous_scraping()) state['scrape_media'] = not state['scrape_media']
print("Continuous scraping started. Press Ctrl+C to stop.") save_state(state)
try: print(
await asyncio.sleep(float('inf')) f"Media scraping {'enabled' if state['scrape_media'] else 'disabled'}.")
except KeyboardInterrupt: case 'c':
continuous_scraping_active = False global continuous_scraping_active
task.cancel() continuous_scraping_active = True
print("\nStopping continuous scraping...") task = asyncio.create_task(continuous_scraping())
await task print("Continuous scraping started. Press Ctrl+C to stop.")
elif choice == 'e': try:
await export_data() await asyncio.sleep(float('inf'))
elif choice == 'v': except KeyboardInterrupt:
await view_channels() continuous_scraping_active = False
elif choice == 'q': task.cancel()
print("Quitting...") print("\nStopping continuous scraping...")
sys.exit() await task
else: case 'e':
print("Invalid option.") await export_data()
case 'v':
await view_channels()
case 'q':
print("Quitting...")
sys.exit()
case 'l':
await list_Channels()
case _:
print("Invalid option.")
async def main(): async def main():
await client.start() await client.start()