A Python script that allows you to automatically scrape and download stories from your Telegram friends using the Telethon library. The script continuously monitors and saves both photos and videos from stories, along with their metadata.
Due to Telegram API restrictions, this script can only access stories from:
- Users you have added to your friend list
- Users whose privacy settings allow you to view their stories
This is a limitation of Telegram's API and cannot be bypassed.
- Automatically scrapes all available stories from your Telegram friends
- Downloads both photos and videos from stories
- Stores metadata in SQLite database
- Exports data to Excel spreadsheet
- Real-time monitoring with customizable intervals
- Timestamp is set to (UTC+2)
- Maintains record of previously downloaded stories
- Resume capability
- Automatic retry mechanism
Before running the script, you'll need:
- Python 3.7 or higher
- Telegram account
- API credentials from Telegram
- Friends on Telegram whose stories you want to track
pip install -r requirements.txt
Contents of requirements.txt
:
telethon
openpyxl
schedule
- Visit https://my.telegram.org/auth
- Log in with your phone number
- Click on "API development tools"
- Fill in the form:
- App title: Your app name
- Short name: Your app short name
- Platform: Can be left as "Desktop"
- Description: Brief description of your app
- Click "Create application"
- You'll receive:
api_id
: A numberapi_hash
: A string of letters and numbers
Keep these credentials safe, you'll need them to run the script!
- Clone the repository:
git clone https://github.com/unnohwn/telegram-story-scraper.git
cd telegram-story-scraper
- Install requirements:
pip install -r requirements.txt
- Run the script:
python TGSS.py
- On first run, you'll be prompted to enter:
- Your API ID
- Your API Hash
- Your phone number (with country code)
- Verification code (sent to your Telegram)
- Checking interval in seconds (default is 60)
The script:
- Connects to your Telegram account
- Periodically checks for new stories from your friends
- Downloads any new stories (photos/videos)
- Stores metadata in a SQLite database
- Exports information to an Excel file
- Runs continuously until interrupted (Ctrl+C)
SQLite database containing:
user_id
: Telegram user ID of the story creatorstory_id
: Unique story identifiertimestamp
: When the story was posted (UTC+2)filename
: Local filename of the downloaded media
Export file containing the same information as the database, useful for:
- Easy viewing of story metadata
- Filtering and sorting
- Data analysis
- Sharing data with others
- Photos are saved as:
{user_id}_{story_id}.jpg
- Videos are saved with their original extension:
{user_id}_{story_id}.{extension}
- All media files are saved in the script's directory
- Customizable checking interval (default: 60 seconds)
- Runs continuously until manually stopped
- Maintains state between runs
- Avoids duplicate downloads
- Supports both photos and videos
- Automatically detects media type
- Preserves original quality
- Generates unique filenames
The script includes:
- Automatic retry mechanism for failed downloads
- Error logging for failed operations
- Connection error handling
- State preservation in case of interruption
- Subject to Telegram's rate limits
- Stories must be currently active (not expired)
- Media download size limits apply as per Telegram's restrictions
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
This tool is for educational purposes only. Make sure to:
- Respect Telegram's Terms of Service
- Obtain necessary permissions before scraping
- Use responsibly and ethically
- Comply with data protection regulations
- Respect user privacy