Last active
August 29, 2015 14:01
-
-
Save vladkorotnev/720ddca8d177d640f833 to your computer and use it in GitHub Desktop.
Receives VK messages and drops attachments into a folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
#return | |
# Dependencies | |
import vk_api, datetime | |
from vk_api import upload | |
import pdb | |
import json, urllib, random, uuid, os | |
from BeautifulSoup import BeautifulStoneSoup | |
# -------Config Here--------- | |
APIID=' ' #VK APP ID | |
SECRET=' ' #VK APP SECRET | |
LOGIN=' ' #Login of passive account | |
PASW=' ' #Pass of passive account | |
OID=' ' # ID of ADMIN (not account) | |
# --------------------------- | |
def captchaHandle(sas): | |
print "CAPTCHA NEEDED" | |
print str("http://api.vk.com/captcha.php?sid=")+str(sas.sid) | |
quit() | |
r = raw_input() | |
sas.try_again(r) | |
def basename(str): | |
return str.split('/')[-1] | |
report = "== inbounder system report ==" | |
sendsReport = False | |
messNumber = 0 | |
fileNumber = 0 | |
def processAttachments(src, onlyPhoto=False, onlyDocu=False, sepaDir=False, dirAlready=""): | |
global report, sendsReport, fileNumber | |
for att in src: | |
kind = att['type'] | |
content = att[kind] | |
dirext = 'incoming/' | |
if sepaDir and (dirAlready is ""): | |
print "SEP DIR" | |
uid = str(uuid.uuid4()) | |
sendsReport = True | |
report += "\nMaking folder "+uid | |
dirext = dirext + uid+'/' | |
os.makedirs(dirext) | |
if not dirAlready is "": | |
dirext = dirAlready | |
if kind == 'wall' and not (content['attachments'] is None): | |
processAttachments(content['attachments'], onlyPhoto, onlyDocu, sepaDir, dirext) | |
if kind == 'photo' and not onlyDocu: | |
purl = '' | |
sendsReport = True | |
for mode in ['src_xxxbig','src_xxbig', 'src_xbig', 'src_big', 'src']: | |
if purl == '': | |
try: | |
purl = content[mode] | |
except: | |
report = report + '\nNo '+mode+', falling...' | |
report += "\nPhoto: "+ basename(purl) | |
urllib.urlretrieve(purl, dirext+basename(purl)) | |
fileNumber += 1 | |
if kind == 'doc' and not onlyPhoto: | |
furl = content['url'] | |
report += "\nDocu: "+ content['title'] | |
sendsReport=True | |
urllib.urlretrieve(furl, dirext+str(content['title'])+'.'+content['ext']) | |
fileNumber += 1 | |
vk = vk_api.VkApi(api_version='4.1',login=LOGIN,password=PASW,app_id=APIID, token=SECRET,captcha_handler=captchaHandle) #Login to VK | |
vk.method('account.setOnline', {'voip': '0'}) | |
unreadable = '' | |
response = vk.method('users.get') | |
myid = response[0]['uid'] | |
print "My uid ", myid | |
response = vk.method('messages.getHistory', {'user_id':OID}) | |
response.pop(0) | |
print "Got messages" | |
#pdb.set_trace() | |
for message in response: | |
if message['read_state'] == 0 and message['out'] == 0: | |
if not ('pass' in message['body'].lower()): | |
try: | |
processAttachments(message['attachments'], onlyPhoto=('photo' in message['body'].lower()), onlyDocu=('docu' in message['body'].lower()),sepaDir=('dir' in message['body'].lower())) | |
if not ('nodel' in message['body'].lower()): | |
vk.method('messages.delete', {'message_ids':message['mid']}) | |
else: | |
unreadable += ("" if (unreadable is '') else ",") +str(message['mid']) | |
report += "\n" | |
messNumber += 1 | |
except: | |
print 'Error on this msg: ',message | |
report += "\nError processing message: "+str(message) | |
report += "\n " | |
sendsReport=True | |
# pdb.set_trace() | |
else: | |
unreadable += ("" if (unreadable is '') else ",")+str(message['mid']) | |
# Shit is deprecated | |
# vk.method('messages.markAsNew',{'message_ids':unreadable}) | |
if sendsReport: | |
report += "\n" | |
report += "Total messages: "+str(messNumber) | |
report += "\nTotal files: "+str(fileNumber) | |
vk.method('messages.send', {'message':report, 'user_id':OID}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment