<gh_stars>10-100
#!/usr/bin/env python
#coding:utf-8
# Author:  Beining --<<EMAIL>>
# Purpose: A simple script to get lots of viewers of Bilibili Live
# Created: 08/11/2015
# Error report: http://www.cnbeining.com/?p=952
# https://github.com/cnbeining  somewhere within my gists

import sys
import time
import getopt
from multiprocessing import Process
from websocket import create_connection, WebSocketConnectionClosedException, WebSocketProxyException, WebSocketException
import random
import re
import traceback
import socket

global proxy_list

#----------------------------------------------------------------------
def fake_connector(cid, is_proxy = False):
    """"""
    try:
        if is_proxy:
            proxy_server = random.choice(proxy_list)
            host, port = proxy_server.split(':')
            ws = create_connection('ws://livecmt.bilibili.com:88/{cid}'.format(cid = cid), http_proxy_host = host, http_proxy_port = int(port))
        else:
            ws = create_connection('ws://livecmt.bilibili.com:88/{cid}'.format(cid = cid))
        while 1:
            time.sleep(5)
            a = ws.recv()
    except (WebSocketProxyException, socket.error, TypeError):  #proxy server died
            #/usr/local/lib/python2.7/site-packages/websocket/_http.py L186 WTF????
        print(proxy_server + ' died!')
        try:
            proxy_list.remove(proxy_server)
        except Exception, err:
            print(traceback.format_exc())
    except (WebSocketConnectionClosedException, WebSocketException):
        return -1
    except Exception, err:
        print(traceback.format_exc())
        return 0
    finally:
        return

#----------------------------------------------------------------------
def main(cid, thread_number, is_proxy = False):
    """"""
    #Get a list of threads
    process_list = [Process(target=fake_connector, args=((cid, is_proxy, ))) for i in xrange(int(thread_number))]  #Queue? Your ass
    [i.start() for i in process_list]  #ignite every one
    try:
        while 1:
            alive_list = [i.is_alive() for i in process_list]
            print('Active thread: ' + str(len(alive_list)))
            death_position_list = [i for i, x in enumerate(alive_list) if x == False]
            if len(death_position_list) > 0:  #someone died
                print('Some died, adding {COUNT} new threads'.format(COUNT = len(death_position_list)))
                for i in death_position_list:
                    del process_list[i]  #remove body
                    process_list.append(Process(target=fake_connector, args=((cid, is_proxy, ))))
                    process_list[-1].start()  #ignite
            time.sleep(3)
    except Exception as e:
        print(e)
        for i in process_list:
            try:
                i.terminate()  #ensure safe exit
            except:
                pass
        exit()

#----------------------------------------------------------------------
def proxy_file_to_list(proxy_file):
    """fileIO->list
    file:
    192.168.3.11:80
    172.16.58.3:9000
    192.168.127.12:8090
    172.16.31.10:8118
    172.16.58.3:8888
    
    list:
    ['192.168.3.11:80', '172.16.58.3:9000', '192.168.127.12:8090',...
    """
    final_list = []
    pattern = re.compile(r'\d+.\d+.\d+.\d+:\d+')  #172.16.31.10:8123
    with open(proxy_file, 'r') as file_this:
        final_list = [line.strip() for line in file_this if pattern.match(line)]
    return final_list

#----------------------------------------------------------------------
def usage():
    """"""
    print('''Use as:
    -c: cid, room number
    -t: thread number
    -p: proxy file
    
    Press Ctrl+C to exit.
    ''')

if __name__=='__main__':
    is_proxy = False
    argv_list = sys.argv[1:]
    try:
        opts, args = getopt.getopt(argv_list, "hc:t:p:",
                                   ['help', "cid=", 'thread_number=', 'proxy_file='])
    except getopt.GetoptError:
        usage()
        exit()
    for o, a in opts:
        if o in ('-h', '--help'):
            usage()
            exit()
        if o in ('-c', '--cid'):
            cid = a
        if o in ('-t', '--thread_number'):
            thread_number = int(a)
        if o in ('-p', '--proxy_file'):
            is_proxy = True
            proxy_file = a
    if is_proxy:
        proxy_list = proxy_file_to_list(proxy_file)
    print('Getting room {cid} {thread_number} viewers...'.format(cid = cid, thread_number = thread_number))
    main(cid, thread_number, is_proxy)

#!/usr/bin/env python
#coding:utf-8
# Author:  Beining --<<EMAIL>>
# Purpose: A simple script to get lots of viewers of Bilibili Live
# Created: 08/11/2015
# Error report: http://www.cnbeining.com/?p=952
# https://github.com/cnbeining  somewhere within my gists

import sys
import time
import getopt
from multiprocessing import Process
from websocket import create_connection, WebSocketConnectionClosedException, WebSocketProxyException, WebSocketException
import random
import re
import traceback
import socket

global proxy_list

#----------------------------------------------------------------------
def fake_connector(cid, is_proxy = False):
    """"""
    try:
        if is_proxy:
            proxy_server = random.choice(proxy_list)
            host, port = proxy_server.split(':')
            ws = create_connection('ws://livecmt.bilibili.com:88/{cid}'.format(cid = cid), http_proxy_host = host, http_proxy_port = int(port))
        else:
            ws = create_connection('ws://livecmt.bilibili.com:88/{cid}'.format(cid = cid))
        while 1:
            time.sleep(5)
            a = ws.recv()
    except (WebSocketProxyException, socket.error, TypeError):  #proxy server died
            #/usr/local/lib/python2.7/site-packages/websocket/_http.py L186 WTF????
        print(proxy_server + ' died!')
        try:
            proxy_list.remove(proxy_server)
        except Exception, err:
            print(traceback.format_exc())
    except (WebSocketConnectionClosedException, WebSocketException):
        return -1
    except Exception, err:
        print(traceback.format_exc())
        return 0
    finally:
        return

#----------------------------------------------------------------------
def main(cid, thread_number, is_proxy = False):
    """"""
    #Get a list of threads
    process_list = [Process(target=fake_connector, args=((cid, is_proxy, ))) for i in xrange(int(thread_number))]  #Queue? Your ass
    [i.start() for i in process_list]  #ignite every one
    try:
        while 1:
            alive_list = [i.is_alive() for i in process_list]
            print('Active thread: ' + str(len(alive_list)))
            death_position_list = [i for i, x in enumerate(alive_list) if x == False]
            if len(death_position_list) > 0:  #someone died
                print('Some died, adding {COUNT} new threads'.format(COUNT = len(death_position_list)))
                for i in death_position_list:
                    del process_list[i]  #remove body
                    process_list.append(Process(target=fake_connector, args=((cid, is_proxy, ))))
                    process_list[-1].start()  #ignite
            time.sleep(3)
    except Exception as e:
        print(e)
        for i in process_list:
            try:
                i.terminate()  #ensure safe exit
            except:
                pass
        exit()

#----------------------------------------------------------------------
def proxy_file_to_list(proxy_file):
    """fileIO->list
    file:
    192.168.3.11:80
    172.16.58.3:9000
    192.168.127.12:8090
    172.16.31.10:8118
    172.16.58.3:8888
    
    list:
    ['192.168.3.11:80', '172.16.58.3:9000', '192.168.127.12:8090',...
    """
    final_list = []
    pattern = re.compile(r'\d+.\d+.\d+.\d+:\d+')  #172.16.31.10:8123
    with open(proxy_file, 'r') as file_this:
        final_list = [line.strip() for line in file_this if pattern.match(line)]
    return final_list

#----------------------------------------------------------------------
def usage():
    """"""
    print('''Use as:
    -c: cid, room number
    -t: thread number
    -p: proxy file
    
    Press Ctrl+C to exit.
    ''')

if __name__=='__main__':
    is_proxy = False
    argv_list = sys.argv[1:]
    try:
        opts, args = getopt.getopt(argv_list, "hc:t:p:",
                                   ['help', "cid=", 'thread_number=', 'proxy_file='])
    except getopt.GetoptError:
        usage()
        exit()
    for o, a in opts:
        if o in ('-h', '--help'):
            usage()
            exit()
        if o in ('-c', '--cid'):
            cid = a
        if o in ('-t', '--thread_number'):
            thread_number = int(a)
        if o in ('-p', '--proxy_file'):
            is_proxy = True
            proxy_file = a
    if is_proxy:
        proxy_list = proxy_file_to_list(proxy_file)
    print('Getting room {cid} {thread_number} viewers...'.format(cid = cid, thread_number = thread_number))
    main(cid, thread_number, is_proxy)
#!/usr/bin/env python #coding:utf-8 # Author:  Beining --<<EMAIL>> # Purpose: A simple script to get lots of viewers of Bilibili Live # Created: 08/11/2015 # Error report: http://www.cnbeining.com/?p=952 # https://github.com/cnbeining  somewhere within my gists #---------------------------------------------------------------------- #proxy server died #/usr/local/lib/python2.7/site-packages/websocket/_http.py L186 WTF???? #---------------------------------------------------------------------- #Get a list of threads #Queue? Your ass #ignite every one #someone died #remove body #ignite #ensure safe exit #---------------------------------------------------------------------- fileIO->list     file:     192.168.3.11:80     172.16.58.3:9000     192.168.127.12:8090     172.16.31.10:8118     172.16.58.3:8888          list:     ['192.168.3.11:80', '172.16.58.3:9000', '192.168.127.12:8090',... #172.16.31.10:8123 #---------------------------------------------------------------------- Use as:     -c: cid, room number     -t: thread number     -p: proxy file          Press Ctrl+C to exit.