Skip to content

Instantly share code, notes, and snippets.

@PyNSK
Last active January 6, 2016 19:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save PyNSK/39220dabd72e54faff18 to your computer and use it in GitHub Desktop.
Save PyNSK/39220dabd72e54faff18 to your computer and use it in GitHub Desktop.
# -*- encoding: utf-8 -*-
from __future__ import unicode_literals
import random
from urllib.parse import parse_qs
import webbrowser
import pickle
from datetime import datetime, timedelta
import vk
APP_ID = <Номер приложения>
# file, where auth data is saved
AUTH_FILE = '.auth_data'
# chars to exclude from filename
FORBIDDEN_CHARS = '/\\\?%*:|"<>!'
def get_saved_auth_params():
access_token = None
user_id = None
try:
with open(AUTH_FILE, 'rb') as pkl_file:
token = pickle.load(pkl_file)
expires = pickle.load(pkl_file)
uid = pickle.load(pkl_file)
if datetime.now() < expires:
access_token = token
user_id = uid
except IOError:
pass
return access_token, user_id
def save_auth_params(access_token, expires_in, user_id):
expires = datetime.now() + timedelta(seconds=int(expires_in))
with open(AUTH_FILE, 'wb') as output:
pickle.dump(access_token, output)
pickle.dump(expires, output)
pickle.dump(user_id, output)
def get_auth_params():
auth_url = ("https://oauth.vk.com/authorize?client_id={app_id}"
"&scope=wall,messages&redirect_uri=http://oauth.vk.com/blank.html"
"&display=page&response_type=token".format(app_id=APP_ID))
webbrowser.open_new_tab(auth_url)
redirected_url = input("Paste here url you were redirected:\n")
aup = parse_qs(redirected_url)
aup['access_token'] = aup.pop(
'https://oauth.vk.com/blank.html#access_token')
save_auth_params(aup['access_token'][0], aup['expires_in'][0],
aup['user_id'][0])
return aup['access_token'][0], aup['user_id'][0]
def get_api(access_token):
session = vk.Session(access_token=access_token)
return vk.API(session)
def send_message(api, user_id, message, **kwargs):
data_dict = {
'user_id': user_id,
'message': message,
}
data_dict.update(**kwargs)
return api.messages.send(**data_dict)
def get_texts():
with open('text.txt', 'r') as fio:
return fio.read().split('-----')
def main():
access_token, _ = get_saved_auth_params()
if not access_token or not _:
access_token, _ = get_auth_params()
api = get_api(access_token)
texts = get_texts()
for friend in api.friends.get():
res = send_message(api, user_id=friend, message=random.choice(texts))
if __name__ == '__main__':
main()
Поздравляю с НГ
-----
Новый год, Новый год, желаю счастья!
-----
С НГ!
-----
Хватит жрать за столом! Весь год будешь жрать
-----
Удачи в Новом Году
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment