Advertisement
Guest User

Untitled

a guest
Apr 14th, 2020
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.09 KB | None | 0 0
  1. '''
  2. Copyright 2015 ohyou
  3.  
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7.  
  8.    http://www.apache.org/licenses/LICENSE-2.0
  9.  
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. '''
  16.  
  17. import requests
  18. import subprocess
  19. import json
  20. import sys
  21. import multiprocessing
  22. import time
  23. import random
  24.  
  25. channel_url = "twitch.tv/"
  26. processes = []
  27. response = None
  28.  
  29.  
  30. def get_channel():
  31.     # Reading the channel name - passed as an argument to this script
  32.     if len(sys.argv) >= 2:
  33.         global channel_url
  34.         channel_url += sys.argv[1]
  35.     else:
  36.         print("An error has occurred while trying to read arguments. Did you specify the channel?")
  37.         sys.exit(1)
  38.  
  39.  
  40. def get_proxies():
  41.     # Reading the list of proxies
  42.     try:
  43.         lines = [line.rstrip("\n") for line in open("proxylist.txt")]
  44.     except IOError as e:
  45.         print("An error has occurred while trying to read the list of proxies: %s" % e.strerror)
  46.         sys.exit(1)
  47.  
  48.     return lines
  49.  
  50.  
  51. def get_url():
  52.     global response
  53.     # Getting the json with all data regarding the stream
  54.     try:
  55.         response = subprocess.Popen(
  56.             ['streamlink.exe', "--http-header", "Client-ID=kimne78kx3ncx6brgo4mv6wki5h1ko",
  57.              channel_url, "-j"], stdout=subprocess.PIPE).communicate()[0]
  58.     except subprocess.CalledProcessError:
  59.         print("An error has occurred while trying to get the stream data. Is the channel online? Is the channel name correct?")
  60.         sys.exit(1)
  61.     except OSError:
  62.         print("An error has occurred while trying to use livestreamer package. Is it installed? Do you have Python in your PATH variable?")
  63.  
  64.     # Decoding the url to the worst quality of the stream
  65.     try:
  66.         url = json.loads(response)['streams']['audio_only']['url']
  67.     except:
  68.         try:
  69.             url = json.loads(response)['streams']['worst']['url']
  70.         except (ValueError, KeyError):
  71.             print("An error has occurred while trying to get the stream data. Is the channel online? Is the channel name correct?")
  72.             sys.exit(1)
  73.  
  74.     return url
  75.  
  76.  
  77. def open_url(url, proxy):
  78.     global response
  79.     # Sending HEAD requests
  80.     while True:
  81.         try:
  82.             with requests.Session() as s:
  83.                 response = s.head(url, proxies=proxy)
  84.             print("Sent HEAD request with %s" % proxy["http"])
  85.             time.sleep(20)
  86.         except requests.exceptions.Timeout:
  87.             print("  Timeout error for %s" % proxy["http"])
  88.         except requests.exceptions.ConnectionError:
  89.             print("  Connection error for %s" % proxy["http"])
  90.  
  91.  
  92. def prepare_processes():
  93.     global processes
  94.     proxies = get_proxies()
  95.     n = 0
  96.  
  97.     if len(proxies) < 1:
  98.         print("An error has occurred while preparing the process: Not enough proxy servers. Need at least 1 to function.")
  99.         sys.exit(1)
  100.  
  101.     for proxy in proxies:
  102.         # Preparing the process and giving it its own proxy
  103.         processes.append(
  104.             multiprocessing.Process(
  105.                 target=open_url, kwargs={
  106.                     "url": get_url(), "proxy": {
  107.                         "http": proxy}}))
  108.  
  109.         print('.',)
  110.  
  111.     print('')
  112.  
  113.  
  114. if __name__ == "__main__":
  115.     print("Obtaining the channel...")
  116.     get_channel()
  117.     print("Obtained the channel")
  118.     print("Preparing the processes...")
  119.     prepare_processes()
  120.     print("Prepared the processes")
  121.     print("Booting up the processes...")
  122.  
  123.     # Timer multiplier
  124.     n = 8
  125.  
  126.     # Starting up the processes
  127.     for process in processes:
  128.         time.sleep(random.randint(1, 5) * n)
  129.         process.daemon = True
  130.         process.start()
  131.         if n > 1:
  132.             n -= 1
  133.  
  134.     # Running infinitely
  135.     while True:
  136.         time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement