Beispiel #1
0
def friends(uid, vk=False):
	if not vk:
		vk = get_vk(uid)
	rez = vk_request(vk, 'friends.get', {'user_id': uid})
	if rez:
		return set(rez['items'])
	else:
		return set()
Beispiel #2
0
def names(uid, vk=False):
	if not vk:
		try:
			vk = get_vk(uid)
		except IndexError:
			myprint('Прежде всего необходимо добавить векторы!')
			return ''
	if int(uid) > 0:
		rez = vk_request(vk, 'users.get', {'user_ids': uid})
		if rez:
			return rez[0]['first_name']+' '+rez[0]['last_name']
		else:
			return ''
	else:
		rez = vk_request(vk, 'groups.getById', {'group_ids': str(uid)[1:]})
		if rez:
			return rez[0]['name']
		else:
			return ''
Beispiel #3
0
def start():
	from target import targets, vectors
	from vector import vectors as vectors_info
	import methods
	
	junk = ('http://', 'https://', 'vk.com/', 'group', 'id', '/')
	if varlist['links']:
		for i, link in enumerate(varlist['links']):
			for j in junk:
				link = link.replace(j, '')
			if link.replace('-', '').isdigit():
				varlist['links'][i] = link
			else:
				myprint('Неверный формат:', varlist['links'][i], error=True)
				varlist['links'][i] = False
	
	for target_id in targets:
		vector_id, lvl = vectors[target_id]
		vector_obj = vectors_info[vector_id]
		
		myprint('{} ({})'.format(methods.names(target_id), target_id))
		if lvl == 1:
			rez = vk_request(vector_obj, 'fave.getPosts', {'count': 100})
			if rez:
				for post in rez['items']:
					print_post(post)
		else:
			for group in my_iter(vector_obj, target_id);
				print('  Анализ', group)
				posts = vk_request(vector_obj, 'wall.get', 
					{'owner_id': group, 'count': 100})
				if not posts:
					continue
				for post in posts['items']:
					like = vk_request(vector_obj, 'likes.isLiked', {
						'user_id': target_id, 
						'owner_id': -1*int(group),
						'item_id': post['id'],
						'type': 'post'})
					if like and like['liked']:
						print_post(post)
Beispiel #4
0
def my_iter(vector_obj, target_id):
	if not varlist['links']:
		#сообщества юзера
		groups = vk_request(vector_obj, 'groups.get', 
			{'user_id': target_id, 'count': 1000})
		if not groups:
			myprint('  Невозможно получить список групп!', error=True)
			return
		for group in groups['items']:
			yield '-'+(group.replace('-', ''))
	else:
		#список ссылок
		for group in varlist['links']:
			if group:
				yield group
Beispiel #5
0
def start():
	from __main__ import myprint, vk_request
	from target import targets, vectors
	from vector import vectors as vectors_info
	import methods
	from time import gmtime, strftime
	
	for target_id in targets:
		vector_id, lvl = vectors[target_id]
		vector_obj = vectors_info[vector_id]
		if lvl == 1:
			myprint('Цель', target_id, 'не может быть атакована! Необходим вектор I уровня.')
		else:
			rez = vk_request(vector_obj, 'messages.search', 
				{'q': varlist['query'], 'count': varlist['count']})
			if not rez:
				myprint('Сообщение не найдено!')
			else:
				rez = rez['items']
				for info in rez:
					myprint()
					myprint('{} ({}):'.format(methods.names(info['user_id']), info['user_id']))
					myprint(info['body'])
					myprint(strftime('[%d.%m.%Y %H:%M]', gmtime(info['date'])))
Beispiel #6
0
def start():
    from __main__ import myprint, vk_request
    from target import targets, vectors
    from vector import vectors as vectors_info
    import methods

    from time import sleep
    import re
    from grab import Grab
    g = Grab()

    def download(name, link):
        myprint('Download', name + '...')
        g.setup(log_file=varlist['dir'] + name, timeout=False)
        g.go(link)
        g.setup(log_file=False, timeout=14997)

    def get_photo(attach):
        size = False
        for allowed_size in photo_sizes:
            if 'photo_' + allowed_size in attach:
                size = allowed_size
                break
        if size:
            download(
                str(attach['owner_id']) + '_' + str(attach['id']) + '.jpg',
                attach['photo_' + size])

    from urllib.parse import quote

    def get_video(name, link):
        for i in (1, 2, 3):
            try:
                g.go(link)
            except Exception as e:
                myprint(e, error=True)
                sleep(i)
            else:
                break
        else:
            myprint('Не удалось получить видео!', error=True)
            return

        if link.startswith('https://vk.com/'):
            flink = False
            page = g.response.body.decode('utf-8')
            vk_rex = r'https\:\/\/[a-z0-9\-]+\.vk[a-z0-9\/\-\.]+\.{}'
            for i in ('720\.mp4', '480\.mp4', '360\.mp4', 'vk.flv'):
                try:
                    flink = re.search(vk_rex.format(i), page).group()
                except Exception as e:
                    myprint(e, error=True)
                else:
                    break
            else:
                myprint('Не удалось найти ссылку для загрузки видео с vk!',
                        error=True)
                return
            download(name, flink)
        elif link.startswith('https://coub.com/'):
            page = g.response.body.decode('utf-8')
            try:
                page = page[page.find('"html5":{"video":'):page.
                            find(',"iphone":{"url"')]
                page = page[page.find('"url":"') + 7:]
                video = page[:page.find('"')]
                page = page[page.find('"audio"'):]
                page = page[page.find('"url":"') + 7:]
                audio = page[:page.find('"')]
            except Exception as e:
                myprint('Ошибка при парсинге страницы coub!', error=True)
                myprint(e, error=True)
                return
            download(name + '.mp4', video)
            download(name + '.mp3', audio)
        elif link.startswith('https://www.youtube.com/'):
            try:
                g.go('http://keepvid.com/?url=' + quote(link))
                page = g.response.body.decode('utf-8')
            except Exception as e:
                myprint('Ошибка при переходе на keepvid.com!', error=True)
                myprint(e, error=True)
                return
            page = page[page.find('id="info"'):]
            for size in ('720p', '480p', '360p'):
                if size in page:
                    page = page[:page.find(size)]
                    page = page[page.rfind('http'):]
                    link = page[:page.find('"')]
                    break
            else:
                myprint(
                    'Не удалось найти ссылку для загрузки видео с youtube!',
                    error=True)
                return
            download(name, link)
        else:
            myprint('Невозможно скачать видео! Неподдерживаемый тип.',
                    error=True)
            myprint(link)

    from boilerpipe.extract import Extractor

    def get_link(name, link):
        try:
            g.go(link)
        except Exception as e:
            myprint('Ошибка при переходе по ссылке!', error=True)
            myprint(e, error=True)
            return
        try:
            g.response.detect_charset()
            rez = g.response.body.decode(g.response.charset)
        except Exception as e:
            myprint('Ошибка при декодировании страницы!', error=True)
            myprint(e, error=True)
            return
        try:
            extractor = Extractor(extractor='ArticleExtractor', html=rez)
            text = extractor.getText()
        except Exception as e:
            myprint('Ошибка при извлечении текста!', error=True)
            myprint(e, error=True)
            return
        open(varlist['dir'] + name + '.html', 'w').write(rez)
        open(varlist['dir'] + name + '.txt', 'w').write(text)

    photo_sizes = ('1280', '807', '604')

    if not varlist['links']:
        myprint('Не заданы ссылки для скачивания', error=True)
        return

    if varlist['dir'][-1] != '/':
        varlist['dir'] = varlist['dir'] + '/'
    if type(varlist['links']) is str:
        varlist['links'] = [varlist['links']]

    links = []
    for link in varlist['links']:
        if '/wall' not in link or '_' not in link:
            myprint('Неверный формат ссылки:', link, error=True)
            continue
        link = link[link.find('wall') + 4:]
        links.append(link)

    posts = vk_request(methods.get_vk(), 'wall.getById',
                       {'posts': ','.join(links)})
    if posts:
        #for post in posts['items']:
        for post in posts:
            if len(post['text']) > 5:
                myprint('\n' + (post['text'].replace('\n', '')) + '\n')
                open(
                    '{}description.{}_{}.txt'.format(varlist['dir'],
                                                     post['owner_id'],
                                                     post['id']),
                    'w').write(post['text'])
            if 'attachments' not in post:
                myprint('Нет прикреплений!', error=True)
                continue
            for attach in post['attachments']:
                #if attach['type'] in attach and len(attach.keys()) == 2:
                attach[attach['type']]['type'] = attach['type']
                attach = attach[attach['type']]

                if attach['type'] == 'photo':
                    get_photo(attach)
                elif attach['type'] == 'posted_photo':
                    download(
                        str(attach['owner_id']) + '_' + str(attach['id']) +
                        '.jpg', attach['photo_604'])
                elif attach['type'] == 'video':
                    video = vk_request(
                        methods.get_vk(), 'video.get', {
                            'videos':
                            str(attach['owner_id']) + '_' + str(attach['id'])
                        })
                    if video and 'player' in video['items'][0]:
                        get_video(attach['title'], video['items'][0]['player'])
                    else:
                        myprint('Видео',
                                attach['title'],
                                'недоступно для скачивания...',
                                error=True)
                elif attach['type'] == 'audio':
                    download(
                        attach['artist'] + ' - ' + attach['title'] + '.mp3',
                        attach['url'])
                elif attach['type'] == 'doc':
                    download(attach['title'] + '.' + attach['ext'],
                             attach['url'])
                elif attach['type'] == 'link':
                    myprint('Сохранение', attach['title'] + '...')
                    get_link(attach['title'], attach['url'])
                elif attach['type'] == 'note':
                    open(varlist['dir'] + attach['title'] + '.txt',
                         'w').write(attach['text'])
                elif attach['type'] == 'page':
                    get_link(attach['view_url'], attach['url'])
                elif attach['type'] == 'album':
                    photos = vk_request(methods.get_vk(), 'photos.get',
                                        {'album_id': attach['id']})
                    if photos:
                        for photo in photos['items']:
                            get_photo(photo)
                else:
                    myprint('Неподдерживаемый тип:',
                            attach['type'],
                            error=True)
def start():
    from __main__ import myprint, vk_request
    from target import targets, vectors
    from vector import vectors as vectors_info
    import methods

    query_fields = 'verified,sex,city,country,home_town,has_photo,education,bdate,followers_count,interests,music,movies,books,games'
    most = [
        'verified', 'sex', 'city', 'country', 'home_town', 'has_photo',
        'education', 'first_name'
    ]
    avg = ['bdate', 'followers_count']
    cntr = ['interests', 'music', 'movies', 'books', 'games']

    uids = ','.join([str(target_id) for target_id in targets])
    users = vk_request(methods.get_vk(), 'users.get', {
        'user_ids': uids,
        'fields': query_fields
    })
    if not users:
        return

    #раскидываем данные о пользователях по полям
    from itertools import chain
    stat = dict.fromkeys(query_fields.split(','))
    stat.update(dict.fromkeys([i + '_count' for i in cntr]))
    stat.update({'first_name': None})
    for user in users:
        for field in chain(most, avg, cntr):
            if field in user:
                data = user[field]
                if data:
                    if field == 'bdate':
                        data = data[-4:]
                        if '.' in data:
                            continue
                    elif field in ('city', 'country'):
                        data = data['id']
                    elif field == 'hometown':
                        data = data['title']

                    if type(data) is str and data.isdigit():
                        data = int(data)
                    if field in user and data:
                        if stat[field] is None:
                            stat[field] = [data]
                        else:
                            stat[field].append(data)

    #рассчитываем поля
    from pprint import pprint
    pprint(stat)
    count_users = len(users)
    from collections import Counter
    for field in most:
        if stat[field]:
            data = Counter(stat[field])
            data = data.most_common(1)
            if len(data):
                data = data[0]
                if data[1] > count_users / 2:
                    stat[field] = data[0]
                else:
                    stat[field] = None
            else:
                stat[field] = None
    for field in avg:
        if stat[field]:
            stat[field] = round(sum(stat[field]) / len(stat[field]))
    for field in cntr:
        if stat[field]:
            #stat[field+'_count'] = round(sum(stat[field])/len(stat[field]))
            data = Counter(stat[field])
            data = data.most_common(1)
            if len(data):
                data = data[0]
                if data[1] > max(count_users / 4, 2):
                    stat[field] = data[0]
                else:
                    stat[field] = None
            else:
                stat[field] = None

    #вывод собранной статистики
    for field in chain(most, avg, cntr):
        if stat[field] is not None:
            data = stat[field]
            if field == 'sex':
                data = ('женский', 'мужской')[data - 1]
            print('{:15}: {}'.format(field, data))

    #формируем запрос
    fields = [
        'city', 'country', ('hometown', 'home_town'),
        ('university', 'education'), 'sex', ('birth_year', 'bdate'),
        'has_photo', 'interests', ('q', 'first_name')
    ]
    query = dict()
    for field in fields:
        if type(field) is tuple:
            field1, field2 = field
        else:
            field1, field2 = field, field
        if stat[field2] is not None:
            query[field1] = stat[field2]

    query['fields'] = query_fields
    query['count'] = 5  #200
    if stat['followers_count'] and stat['followers_count'] > 200:
        query['sort'] = 0
    else:
        query['sort'] = 1

    users = vk_request(methods.get_vk(), 'users.search', query)
    print()
    for user in users['items']:
        print('{} {}\n    https://vk.com/id{}'.format(user['first_name'],
                                                      user['last_name'],
                                                      user['id']))
Beispiel #8
0
def start():
    from __main__ import myprint, vk_request
    from target import targets, vectors
    from vector import vectors as vectors_info
    import methods

    junk = ('http://', 'https://', 'vk.com/', 'group', 'id', '/')
    if type(varlist['links']) is str:
        varlist['links'] = varlist['links'].strip()
    elif type(varlist['links']) is tuple:
        varlist['links'] = list(varlist['links'])
    if varlist['links']:
        if not type(varlist['links']) is list:
            varlist['links'] = [varlist['links']]
        for i, link in enumerate(varlist['links']):
            for j in junk:
                link = link.replace(j, '')
            if link.replace('-', '').isdigit():
                varlist['links'][i] = link
            else:
                myprint('Неверный формат:', varlist['links'][i], error=True)
                varlist['links'][i] = False

    groups = False
    if varlist['links']:
        groups = varlist['links']
    else:
        #сообщества юзера
        groups = set()
        for target_id in targets:
            vector_id, lvl = vectors[target_id]
            vector_obj = vectors_info[vector_id]
            this_groups = vk_request(vector_obj, 'groups.get', {
                'user_id': target_id,
                'count': 1000
            })
            if this_groups:
                this_groups = [
                    -1 * int(group) for group in this_groups['items']
                ]
                groups.update(set(this_groups))
    if not groups:
        myprint('  Невозможно получить список групп!', error=True)
        return

    for group in groups:
        myprint('\n\n{} ({})'.format(methods.names(group), group))
        posts = vk_request(methods.get_vk(), 'wall.get', {
            'owner_id': group,
            'count': 100
        })
        if not posts:
            continue

        for post in posts['items']:
            if post['from_id'] in targets:
                if len(post['text']) > 78:
                    post['text'] = post['text'][:77] + '…'
                myprint('\n  {} ({})'.format(methods.names(post['from_id']),
                                             post['from_id']))
                myprint(' ', post['text'])
                myprint('  https://vk.com/wall-',
                        group,
                        '_',
                        post['id'],
                        sep='')

            comments = vk_request(methods.get_vk(), 'wall.getComments', {
                'owner_id': group,
                'post_id': post['id'],
                'count': 100
            })
            if not comments:
                continue

            for comment in comments['items']:
                if comment['from_id'] in targets:
                    if len(comment['text']) > 78:
                        comment['text'] = comment['text'][:77] + '…'
                    myprint('\n  {} ({})'.format(
                        methods.names(comment['from_id']), comment['from_id']))
                    myprint(' ', comment['text'])
                    myprint('  https://vk.com/wall',
                            group,
                            '_',
                            post['id'],
                            '?reply=',
                            comment['id'],
                            sep='')