Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: UTF-8 -*-
- import re
- import pycurl
- import time
- import json
- import requests
- import os
- import urllib
- import certifi
- from pickle import load
- from pickle import dump
- from vklancer import api
- from vklancer import utils
- from random import choice,randrange,seed
- global z, l
- def get_biggest_photo(item):
- for items in item:
- if 'photo_2560' in items: return items['photo_2560']
- if 'photo_1280' in items: return items['photo_1280']
- if 'photo_807' in items: return items['photo_807']
- if 'photo_604' in items: return items['photo_604']
- if 'photo_130' in items: return items['photo_130']
- if 'photo_75' in items: return items['photo_75']
- else: return False
- def set_vk_token(access_token, AUTH_FILE):
- with open(AUTH_FILE, 'wb') as output:
- dump(access_token, output)
- def get_vk_token(AUTH_FILE):
- token = None
- try:
- with open(AUTH_FILE, 'rb') as pkl_file:
- token = load(pkl_file)
- except Exception:
- pass
- return token
- def load_config(failname):
- with open(failname) as data_file:
- return json.load(data_file)
- def method_errors(data):
- try:
- if data and 'ok' in data:
- return False
- else:
- return True
- except Exception:
- return True
- def groupsGetMembers(group_id, offset, count):
- try:
- return vk.groups.getMembers(group_id=group_id, offset=offset,
- count=count)
- except Exception:
- print("groupsGetMembers ConnectionResetError; retrying")
- time.sleep(1)
- return groupsGetMembers(group_id=group_id, offset=offset, count=count)
- def executeNewFunc(user):
- try:
- return vk.execute.newFunc(user=user)
- except Exception:
- print("executeNewFunc ConnectionResetError; retrying")
- time.sleep(1)
- return executeNewFunc(user=user)
- def executeKick(user, gid):
- try:
- return vk.execute.kick(user=user, gid=gid)
- except Exception:
- print("executeKick ConnectionResetError; retrying")
- time.sleep(1)
- return executeKick(user=user, gid=gid)
- def getAlbum(albums):
- for item in albums:
- album = vk.photos.get(owner_id=config['owner_id'], album_id=item, offset=0, count=1)
- print(album)
- if album['response']['count']<500: return item
- a=0
- name='1'
- m = 0
- l = 0
- ids = []
- like = {'response': 1}
- users = []
- albums=[]
- users_count=[]
- albums_count=[]
- albums_list=[]
- ban_list=[]
- ARCHIVE_DIR='pics'
- with open('albums.json') as f:
- albums_list = f.read().splitlines()
- with open('albums_ban.json') as f:
- ban_list = f.read().splitlines()
- print(albums_list)
- user_list = []
- text=''
- random_user=0
- random_album=''
- with open('list5.json') as f:
- user_list = f.read().splitlines()
- print(user_list)
- config = load_config('config5.json')
- access_token = get_vk_token(config['auth_file'])
- if not access_token:
- access_token = utils.oauth(login=config['login'],
- password=config['password'],
- app_id=config['appid'],
- scope=config['scope'])
- if access_token:
- #print("[+] Auth succesfull.")
- vk = api.API(token=access_token, version='5.8')
- set_vk_token(access_token, config['auth_file'])
- def albGet(owner_id):
- try:
- return vk.photos.getAlbums(owner_id=user)
- except ConnectionResetError as ex:
- print("albget ConnectionResetError; retrying")
- time.sleep(1)
- return albGet(owner_id)
- '''GET ALBUMS FROM USERS'''
- for user in user_list:
- print(user)
- users_count.append(user)
- users.append([])
- users[a].append(user)
- user_albums = albGet(owner_id=user)
- time.sleep(0.34)
- albums_count.append(user_albums['response']['count'])
- for item in user_albums['response']['items']:
- albums.append(item['id'])
- users[a].append(albums)
- albums=[]
- a+=1
- #print(users_count)
- #print(albums_count)
- #print(ban_list)
- def rnd_album(random_user):
- seed()
- random_album = choice(users[random_user][1])
- a=0
- for item in ban_list:
- #rint('item={}'.format(item))
- #print('rndalbum={}'.format(random_album))
- if int(item) == int(random_album):
- a+=1
- if a>0:
- print('RAVNO')
- return rnd_album(random_user)
- else: return random_album
- def errors(owner_id,album_id,offset,count):
- exp = vk.photos.get(owner_id=owner_id, album_id=album_id, offset=offset, count=count)
- if 'error' in exp:
- time.sleep(1)
- return errors(owner_id,album_id,offset,count)
- else:
- return exp
- def main():
- main_album= getAlbum(albums_list)
- print('album='+main_album)
- random_user=randrange(0,a-1,1)
- print('user='+str(random_user))
- random_album=rnd_album(random_user)
- print('https://vk.com/album'+str(users[random_user][0])+'_'+str(random_album))
- photos=errors(str(users[random_user][0]),str(random_album),0,1) #vk.photos.get(owner_id=str(users[random_user][0]), album_id=str(random_album),count=1,offset=0)
- time.sleep(0.5)
- random_photo=randrange(1,(photos['response']['count']-1),1)
- photo_id=errors(str(users[random_user][0]),str(random_album),random_photo,1) #vk.photos.get(owner_id=users[random_user][0], album_id=random_album, offset=random_photo, count=1)
- print(photo_id)
- main_photo=photo_id['response']['items']
- for unit in main_photo:
- rand_photo=unit
- print(rand_photo['id'])
- print('url='+get_biggest_photo(main_photo))
- name =str(random_album)+'_'+str(rand_photo['id'])
- try:
- with open(ARCHIVE_DIR+'/'+name+'.jpg', 'xb') as f:
- c = pycurl.Curl()
- c.setopt(pycurl.CAINFO, certifi.where())
- c.setopt(c.URL, str(get_biggest_photo(photo_id['response']['items'])))
- c.setopt(c.WRITEDATA, f)
- c.perform()
- c.close()
- except Exception as ex:
- print('Error -',ex)
- else:
- pserver=vk.photos.getUploadServer(album_id=main_album) #Дальше загрузка картинки из архива
- print(pserver)
- pic = os.listdir(ARCHIVE_DIR)
- file = open(ARCHIVE_DIR+'/'+str(name)+'.jpg','rb')
- photo = requests.post(pserver['response']['upload_url'], files={'file1': file}) #Грузим
- text = photo.text
- photo = json.JSONDecoder().decode(text)
- photo = vk.photos.save(album_id=main_album, server=photo['server'], photos_list=photo['photos_list'], hash=photo['hash']) # И сохраняем]
- print(photo)
- file.close()
- #time.sleep(1)
- time.sleep(10)
- main()
- main()
- input(l)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement