Advertisement
Guest User

Untitled

a guest
Dec 24th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.27 KB | None | 0 0
  1. # -*- coding: UTF-8 -*-
  2. import re
  3. import pycurl
  4. import time
  5. import json
  6. import requests
  7. import os
  8. import urllib
  9. import certifi
  10. from pickle import load
  11. from pickle import dump
  12. from vklancer import api
  13. from vklancer import utils
  14. from random import choice,randrange,seed
  15. global z, l
  16.  
  17. def get_biggest_photo(item):
  18. for items in item:
  19.  
  20. if 'photo_2560' in items: return items['photo_2560']
  21. if 'photo_1280' in items: return items['photo_1280']
  22. if 'photo_807' in items: return items['photo_807']
  23. if 'photo_604' in items: return items['photo_604']
  24. if 'photo_130' in items: return items['photo_130']
  25. if 'photo_75' in items: return items['photo_75']
  26. else: return False
  27. def set_vk_token(access_token, AUTH_FILE):
  28. with open(AUTH_FILE, 'wb') as output:
  29. dump(access_token, output)
  30.  
  31.  
  32. def get_vk_token(AUTH_FILE):
  33. token = None
  34. try:
  35. with open(AUTH_FILE, 'rb') as pkl_file:
  36. token = load(pkl_file)
  37. except Exception:
  38. pass
  39. return token
  40.  
  41.  
  42. def load_config(failname):
  43. with open(failname) as data_file:
  44. return json.load(data_file)
  45.  
  46.  
  47. def method_errors(data):
  48. try:
  49. if data and 'ok' in data:
  50. return False
  51. else:
  52. return True
  53. except Exception:
  54. return True
  55.  
  56.  
  57. def groupsGetMembers(group_id, offset, count):
  58. try:
  59. return vk.groups.getMembers(group_id=group_id, offset=offset,
  60. count=count)
  61. except Exception:
  62. print("groupsGetMembers ConnectionResetError; retrying")
  63. time.sleep(1)
  64. return groupsGetMembers(group_id=group_id, offset=offset, count=count)
  65.  
  66.  
  67. def executeNewFunc(user):
  68. try:
  69. return vk.execute.newFunc(user=user)
  70. except Exception:
  71. print("executeNewFunc ConnectionResetError; retrying")
  72. time.sleep(1)
  73. return executeNewFunc(user=user)
  74.  
  75.  
  76. def executeKick(user, gid):
  77. try:
  78. return vk.execute.kick(user=user, gid=gid)
  79. except Exception:
  80. print("executeKick ConnectionResetError; retrying")
  81. time.sleep(1)
  82. return executeKick(user=user, gid=gid)
  83. def getAlbum(albums):
  84. for item in albums:
  85. album = vk.photos.get(owner_id=config['owner_id'], album_id=item, offset=0, count=1)
  86. print(album)
  87. if album['response']['count']<500: return item
  88. a=0
  89. name='1'
  90. m = 0
  91. l = 0
  92. ids = []
  93. like = {'response': 1}
  94. users = []
  95. albums=[]
  96. users_count=[]
  97. albums_count=[]
  98. albums_list=[]
  99. ban_list=[]
  100. ARCHIVE_DIR='pics'
  101. with open('albums.json') as f:
  102. albums_list = f.read().splitlines()
  103. with open('albums_ban.json') as f:
  104. ban_list = f.read().splitlines()
  105. print(albums_list)
  106. user_list = []
  107. text=''
  108. random_user=0
  109. random_album=''
  110. with open('list5.json') as f:
  111. user_list = f.read().splitlines()
  112. print(user_list)
  113.  
  114. config = load_config('config5.json')
  115.  
  116. access_token = get_vk_token(config['auth_file'])
  117. if not access_token:
  118. access_token = utils.oauth(login=config['login'],
  119. password=config['password'],
  120. app_id=config['appid'],
  121. scope=config['scope'])
  122.  
  123.  
  124. if access_token:
  125. #print("[+] Auth succesfull.")
  126.  
  127. vk = api.API(token=access_token, version='5.8')
  128. set_vk_token(access_token, config['auth_file'])
  129. def albGet(owner_id):
  130. try:
  131. return vk.photos.getAlbums(owner_id=user)
  132. except ConnectionResetError as ex:
  133. print("albget ConnectionResetError; retrying")
  134. time.sleep(1)
  135. return albGet(owner_id)
  136. '''GET ALBUMS FROM USERS'''
  137.  
  138. for user in user_list:
  139. print(user)
  140. users_count.append(user)
  141. users.append([])
  142. users[a].append(user)
  143. user_albums = albGet(owner_id=user)
  144.  
  145. time.sleep(0.34)
  146.  
  147.  
  148.  
  149. albums_count.append(user_albums['response']['count'])
  150. for item in user_albums['response']['items']:
  151.  
  152. albums.append(item['id'])
  153. users[a].append(albums)
  154. albums=[]
  155. a+=1
  156.  
  157. #print(users_count)
  158. #print(albums_count)
  159. #print(ban_list)
  160. def rnd_album(random_user):
  161. seed()
  162.  
  163. random_album = choice(users[random_user][1])
  164. a=0
  165. for item in ban_list:
  166. #rint('item={}'.format(item))
  167. #print('rndalbum={}'.format(random_album))
  168. if int(item) == int(random_album):
  169. a+=1
  170.  
  171. if a>0:
  172. print('RAVNO')
  173. return rnd_album(random_user)
  174. else: return random_album
  175. def errors(owner_id,album_id,offset,count):
  176. exp = vk.photos.get(owner_id=owner_id, album_id=album_id, offset=offset, count=count)
  177. if 'error' in exp:
  178. time.sleep(1)
  179. return errors(owner_id,album_id,offset,count)
  180. else:
  181. return exp
  182. def main():
  183.  
  184. main_album= getAlbum(albums_list)
  185. print('album='+main_album)
  186. random_user=randrange(0,a-1,1)
  187. print('user='+str(random_user))
  188. random_album=rnd_album(random_user)
  189. print('https://vk.com/album'+str(users[random_user][0])+'_'+str(random_album))
  190. 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)
  191. time.sleep(0.5)
  192.  
  193.  
  194. random_photo=randrange(1,(photos['response']['count']-1),1)
  195.  
  196. 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)
  197. print(photo_id)
  198. main_photo=photo_id['response']['items']
  199. for unit in main_photo:
  200. rand_photo=unit
  201. print(rand_photo['id'])
  202. print('url='+get_biggest_photo(main_photo))
  203. name =str(random_album)+'_'+str(rand_photo['id'])
  204. try:
  205. with open(ARCHIVE_DIR+'/'+name+'.jpg', 'xb') as f:
  206. c = pycurl.Curl()
  207. c.setopt(pycurl.CAINFO, certifi.where())
  208. c.setopt(c.URL, str(get_biggest_photo(photo_id['response']['items'])))
  209. c.setopt(c.WRITEDATA, f)
  210. c.perform()
  211. c.close()
  212. except Exception as ex:
  213. print('Error -',ex)
  214. else:
  215. pserver=vk.photos.getUploadServer(album_id=main_album) #Дальше загрузка картинки из архива
  216. print(pserver)
  217. pic = os.listdir(ARCHIVE_DIR)
  218.  
  219. file = open(ARCHIVE_DIR+'/'+str(name)+'.jpg','rb')
  220. photo = requests.post(pserver['response']['upload_url'], files={'file1': file}) #Грузим
  221. text = photo.text
  222. photo = json.JSONDecoder().decode(text)
  223.  
  224. photo = vk.photos.save(album_id=main_album, server=photo['server'], photos_list=photo['photos_list'], hash=photo['hash']) # И сохраняем]
  225. print(photo)
  226.  
  227. file.close()
  228. #time.sleep(1)
  229. time.sleep(10)
  230.  
  231. main()
  232. main()
  233.  
  234.  
  235.  
  236.  
  237. input(l)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement