Skip to content

Instantly share code, notes, and snippets.

@lov3catch
Created January 9, 2014 22:22
Show Gist options
  • Save lov3catch/8343195 to your computer and use it in GitHub Desktop.
Save lov3catch/8343195 to your computer and use it in GitHub Desktop.
VK_API: get group statistic
#-*-coding:utf-8-*-
import urllib2
import json
from datetime import date, datetime
import time
from time import gmtime, strftime
today = date.today()
vk_api_url = "https://api.vk.com/method/stats.get?group_id=33006648&app_id=3572991&date_from=%s&access_token=ef57857451d5a0b30cee5f6b3cb341ba27197ab9cba7a88a3591099afb4a5d9fa345be40cbe3dc9e153cb"%today
dataStoragePath = '/media/lov3catch/A0FB-B842/statistic_storage'
def writeInFile(file_path, same_value):
current_time = strftime("%a, %d %b %Y %H:%M:%S", gmtime())
infoForStore = "%s. New visitors: %d \n"%(current_time, same_value)
dataStorage = open(file_path, "a")
dataStorage.write(infoForStore)
dataStorage.close()
def getGroupVisitors(same_url, same_date, sleep_time, last_result=0):
#Get started data
response = urllib2.urlopen(same_url)
#Wailt same time...
print last_result
time.sleep(sleep_time)
#Get difference between started data & new data
response = urllib2.urlopen(same_url)
data = json.load(response)
difference = data["response"][0]["visitors"] - last_result
writeInFile(dataStoragePath, difference)
return getGroupVisitors(same_url, same_date, sleep_time, data["response"][0]["visitors"])
if __name__ == "__main__":
print "Start"
getGroupVisitors(vk_api_url, today, 900)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment