changed if/else to match/case and added list_Channels()

This commit is contained in:
brawler
2024-11-10 21:27:54 +01:00
parent 7373a7f7cb
commit 54cdffeb82
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,25 +257,38 @@ 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):
case 'a':
channel = input("Enter channel ID: ") channel = input("Enter channel ID: ")
state['channels'][channel] = 0 state['channels'][channel] = 0
save_state(state) save_state(state)
print(f"Added channel {channel}.") print(f"Added channel {channel}.")
elif choice == 'r': case 'r':
channel = input("Enter channel ID to remove: ") channel = input("Enter channel ID to remove: ")
if channel in state['channels']: if channel in state['channels']:
del state['channels'][channel] del state['channels'][channel]
@@ -283,14 +296,15 @@ async def manage_channels():
print(f"Removed channel {channel}.") print(f"Removed channel {channel}.")
else: else:
print(f"Channel {channel} not found.") print(f"Channel {channel} not found.")
elif choice == 's': case 's':
for channel in state['channels']: for channel in state['channels']:
await scrape_channel(channel, state['channels'][channel]) await scrape_channel(channel, state['channels'][channel])
elif choice == 'm': case 'm':
state['scrape_media'] = not state['scrape_media'] state['scrape_media'] = not state['scrape_media']
save_state(state) save_state(state)
print(f"Media scraping {'enabled' if state['scrape_media'] else 'disabled'}.") print(
elif choice == 'c': f"Media scraping {'enabled' if state['scrape_media'] else 'disabled'}.")
case 'c':
global continuous_scraping_active global continuous_scraping_active
continuous_scraping_active = True continuous_scraping_active = True
task = asyncio.create_task(continuous_scraping()) task = asyncio.create_task(continuous_scraping())
@@ -302,14 +316,17 @@ async def manage_channels():
task.cancel() task.cancel()
print("\nStopping continuous scraping...") print("\nStopping continuous scraping...")
await task await task
elif choice == 'e': case 'e':
await export_data() await export_data()
elif choice == 'v': case 'v':
await view_channels() await view_channels()
elif choice == 'q': case 'q':
print("Quitting...") print("Quitting...")
sys.exit() sys.exit()
else: case 'l':
await list_Channels()
case _:
print("Invalid option.") print("Invalid option.")
async def main(): async def main():