Solution for python showing this TypeError: string indices must be integers
is Given Below:
I’m running this python Code in Kali Terminal and facing this problem.
I found this code in Github
This script download movies from torrent and it uses WebTorrent API
Once I used this script in windows it was working without any error but when I switch to kali linux and try to run this script I’m facing this error.
I’m not very much familiar with python
Here is my Code
import requests
import subprocess
import sys
def main():
movie_name = input("Enter the movie name:n")
print(f"Searching for {movie_name}")
base_url = f"https://api.sumanjay.cf/torrent/?query={movie_name}"
torrent_results = requests.get(url=base_url).json()
index = 1
magnet = []
for result in torrent_results:
if 'movie' in result['type'].lower():
print(index, ") ", result['name'], "-->", result['size'])
index += 1
magnet.append(result['magnet'])
if magnet:
choice = int(
input("Enter the index of the movie which you want to streamn"))
try:
magnet_link = magnet[choice-1]
download = False # Default is streaming
stream_choice = int(
input("Press 1 to stream or Press 2 to download the movien"))
if stream_choice == 2:
download = True
webtorrent_stream(magnet_link, download)
except IndexError:
print("Incorrect Index entered")
else:
print(f"No results found for {movie_name}")
# Handle Streaming
def webtorrent_stream(magnet_link: str, download: bool):
cmd = []
cmd.append("webtorrent")
cmd.append(magnet_link)
if not download:
cmd.append('--vlc')
if sys.platform.startswith('linux'):
subprocess.call(cmd)
elif sys.platform.startswith('win32'):
subprocess.call(cmd, shell=True)
if __name__ == "__main__":
main()
Error
Enter the movie name:
Baby Boss
Searching for Baby Boss
Traceback (most recent call last):
File "/home/shiva/Desktop/TorrFlix/play.py", line 61, in <module>
main()
File "/home/shiva/Desktop/TorrFlix/play.py", line 23, in main
if 'movie' in result['type'].lower():
TypeError: string indices must be integers
This API is down currently that’s why it is returning {'message': 'API is Down!', 'status': False}
as response instead of expected movie data. Which is the cause of the error.