Advertisement
Guest User

Untitled

a guest
Aug 26th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.65 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,seed,randint
  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('getting album')
  87. #print(album)
  88. if album['response']['count']<500: return item
  89. def albGet(owner_id):
  90. try:
  91. return vk.photos.getAlbums(owner_id=user)
  92. except Exception as ex:
  93. print("albget error ",ex)
  94. time.sleep(1)
  95. return albGet(owner_id)
  96.  
  97. def rnd_album(random_user):
  98. seed()
  99.  
  100. random_album = choice(users[random_user][1])
  101. a=0
  102. for item in ban_list:
  103.  
  104. if int(item) == int(random_album):
  105. a+=1
  106. break
  107.  
  108. if a>0:
  109. print('banned album')
  110. return rnd_album(random_user)
  111. else: return random_album
  112. def errors(owner_id,album_id,offset,count):
  113. exp = vk.photos.get(owner_id=owner_id, album_id=album_id, offset=offset, count=count)
  114. time.sleep(0.33)
  115. if 'error' in exp:
  116. time.sleep(1)
  117. return errors(owner_id,album_id,offset,count)
  118. else:
  119. return exp
  120. def download_file():
  121. try:
  122. with open(ARCHIVE_DIR+'/'+name+'.jpg', 'xb') as f:
  123. print('dwn start')
  124. c = pycurl.Curl()
  125. #c.setopt(pycurl.SSLVERSION, pycurl.SSLVERSION_SSLv3)
  126. c.setopt(pycurl.CAINFO, certifi.where())
  127. c.setopt(pycurl.TIMEOUT, 30)
  128. c.setopt(c.URL, str(get_biggest_photo(photo_id['response']['items'])))
  129. c.setopt(c.WRITEDATA, f)
  130. c.perform()
  131. c.close()
  132. print('dwn end')
  133. except Exception as ex:
  134. print('Error -',ex)
  135. print('try one more time')
  136. download_file()
  137. def upload_file(pserver, file,time):
  138. photo = requests.post(pserver, files=file, timeout=time)
  139. return photo
  140. def random_photo_get(photos, random_album, random_user):
  141.  
  142.  
  143. if int(photos['response']['count'])>0:
  144. random_photo=randint(0,(photos['response']['count']-1))
  145. return random_photo
  146. else:
  147. print('LOW ALBUM')
  148. time.sleep(1)
  149. random_album=rnd_album(random_user)
  150. photos=errors(str(user_list[random_user]),str(random_album),0,1)
  151. return random_photo_get(photos, random_album, random_user)
  152. def main():
  153. main_album= getAlbum(albums_list)
  154. #print('album='+main_album)
  155. a=len(user_list)
  156. random_user=randint(0,a-1)
  157. print('user='+str(user_list[random_user]))
  158. random_album=-15
  159. photos=vk.photos.get(owner_id=str(user_list[random_user]), album_id=str(random_album), offset=0, count=1)
  160. if 'error' in photos:
  161. print('user error')
  162. return main()
  163. #print(photos)
  164. time.sleep(0.5)
  165.  
  166.  
  167.  
  168. random_photo=random_photo_get(photos, random_album, random_user)
  169.  
  170.  
  171. photo_id=errors(str(user_list[random_user]),str(random_album),random_photo,1) #vk.photos.get(owner_id=users[random_user][0], album_id=random_album, offset=random_photo, count=1)
  172. print(photo_id)
  173. main_photo=photo_id['response']['items']
  174. for unit in main_photo:
  175. rand_photo=unit
  176. print(rand_photo['id'])
  177. print('url='+get_biggest_photo(main_photo))
  178. name =str(user_list[random_user])+'_'+str(rand_photo['id'])
  179.  
  180. if os.path.exists(ARCHIVE_DIR+'/'+str(name)+'.jpg'):
  181. print('old file '+str(name))
  182. else:
  183. try:
  184. with open(ARCHIVE_DIR+'/'+name+'.jpg', 'xb') as f:
  185. print('dwn start')
  186. c = pycurl.Curl()
  187. #c.setopt(pycurl.SSLVERSION, pycurl.SSLVERSION_SSLv3)
  188. c.setopt(pycurl.CAINFO, certifi.where())
  189. c.setopt(pycurl.TIMEOUT, 30)
  190. c.setopt(c.URL, str(get_biggest_photo(photo_id['response']['items'])))
  191. c.setopt(c.WRITEDATA, f)
  192. c.perform()
  193. c.close()
  194. print('dwn end')
  195. except Exception as ex:
  196. print('Error -',ex)
  197. else:
  198. print('upl start')
  199. pserver=vk.photos.getUploadServer(album_id=main_album) #Дальше загрузка картинки из архива
  200. print(pserver)
  201. pic = os.listdir(ARCHIVE_DIR)
  202.  
  203. file = open(ARCHIVE_DIR+'/'+str(name)+'.jpg','rb')
  204. try:
  205. photo = upload_file(pserver=pserver['response']['upload_url'], file={'file1': file}, time=20)
  206. except Exception as ex:
  207. print('EEEEEEEEEEEXXXXXXXXXXXXXXXXX',ex)
  208. time.sleep(10)
  209. photo = upload_file(pserver=pserver['response']['upload_url'], file={'file1': file}, time=20)
  210.  
  211.  
  212. text = photo.text
  213. photo = json.JSONDecoder().decode(text)
  214.  
  215. photo = vk.photos.save(album_id=main_album, server=photo['server'], photos_list=photo['photos_list'], hash=photo['hash']) # И сохраняем]
  216. print(photo)
  217.  
  218. file.close()
  219. print('upl end')
  220. #time.sleep(1)
  221. time.sleep(10)
  222.  
  223. return main()
  224. a=0
  225. name='1'
  226. m = 0
  227. l = 0
  228. ids = []
  229. like = {'response': 1}
  230. users = []
  231. albums=[]
  232. photos={}
  233. users_count=[]
  234. albums_count=[]
  235. albums_list=[]
  236. ban_list=[]
  237. ARCHIVE_DIR='sohrpics'
  238. user_list = []
  239. text=''
  240. random_user=0
  241. random_album=''
  242. with open('peoples3.txt') as f:
  243. user_list = f.read().splitlines()
  244. user_list=set(user_list)
  245. user_list=list(user_list)
  246. print('count= ',len(user_list))
  247. #print(user_list)
  248. with open('albums.json') as f:
  249. albums_list = f.read().splitlines()
  250. config = load_config('config5.json')
  251.  
  252. access_token = get_vk_token(config['auth_file'])
  253. if not access_token:
  254. access_token = utils.oauth(login=config['login'],
  255. password=config['password'],
  256. app_id=config['appid'],
  257. scope=config['scope'])
  258.  
  259.  
  260. if access_token:
  261. print("[+] Auth succesfull.")
  262.  
  263. vk = api.API(token=access_token, version='5.65')
  264. set_vk_token(access_token, config['auth_file'])
  265.  
  266.  
  267.  
  268. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement