Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # =============================================================================
- # IMPORTS
- # =============================================================================
- # Python
- from configobj import ConfigObj
- from sys import exit
- from time import sleep
- # VK API
- from vkontakte import API
- # =============================================================================
- # GLOBAL VARIABLES
- # =============================================================================
- PATH="vk1/"
- CONFIG=ConfigObj(PATH+"config/config.ini")
- STRINGS=ConfigObj(PATH+"config/strings.ini")
- LOADED=False
- TOSECONDS=1000
- def echo(text, args=None):
- if text in STRINGS:
- if args is None:
- print STRINGS[text][MainConfig.language].decode("utf-8")
- else:
- print STRINGS[text][MainConfig.language].format(*args).decode("utf-8")
- else:
- print text.format(args).decode("utf-8")
- # =============================================================================
- # CLASSES
- # =============================================================================
- class Config(object):
- def __init__(self, path):
- self.config=ConfigObj(path)
- self.error={}
- for line in self.config:
- object.__setattr__(self, line, self.config[line])
- def save(self):
- self.config.write()
- MainConfig=Config(PATH+"config/config.ini")
- class VkAPI(object):
- def __init__(self):
- self.api=None
- def connect(self, args):
- try:
- api_id=None
- api_secret=None
- if "api_id" in args:
- api_id=args['api_id']
- if "api_secret" in args:
- api_secret=args['api_secret']
- self.api=API(api_secret=api_secret, api_id=api_id)
- echo("Успешное подключение.")
- except:
- self.api=None
- echo("Не удалось подключиться.")
- def disconnect(self):
- self.api=None
- def getFriends(self, id):
- if str(id).isdigit():
- if id=="0":
- id=MainConfig.myid
- friends=self.api.friends.get(user_id=int(id))
- print friends
- else:
- echo("VkApi.IdNotDigital")
- VkAPI=VkAPI()
- if MainConfig.autoconnect:
- VkAPI.connect({"api_id":MainConfig.api_id, "api_secret":MainConfig.api_secret})
- # =============================================================================
- # FUNCTIONS
- # =============================================================================
- def mainloop():
- global LOADED
- if not LOADED:
- echo("*"*20)
- echo("MainLoop.Loading")
- if MainConfig.config:
- echo("MainLoop.ConfigLoaded")
- else:
- echo("MainLoop.ConfigFailed")
- exit("Configuration file not found!")
- echo("MainLoop.Loaded")
- echo("*"*20)
- LOADED=True
- else:
- echo("MainLoop.Calling")
- text=raw_input()
- if text=="exit":
- echo("*"*20)
- echo("MainLoop.Stop", "2")
- echo("*"*20)
- sleep(2)
- exit()
- elif text=="reload":
- import vk1
- reload(vk1)
- call=text.split(".")
- if len(call)==2:
- if call[0]=='config':
- if not "=" in call[1]:
- try:
- value=getattr(MainConfig, call[1])
- except:
- value=None
- print '.'.join(call), "=", value
- else:
- args=call[1].split("=")
- setattr(MainConfig, args[0], args[1])
- echo("Config.SetValue", args)
- elif call[0]=='strings':
- if call[1]=='update':
- global STRINGS
- STRINGS=ConfigObj(PATH+"config/strings.ini")
- elif call[0]=="api":
- if call[1]=="connect":
- VkAPI.connect({"api_id":MainConfig.api_id, "api_secret":MainConfig.api_secret})
- elif call[1]=="disconnect":
- VkAPI.disconnect()
- else:
- args=call[1].split()
- if args[0]=='friends':
- if len(args)==1:
- args.append("0")
- VkAPI.getFriends(args[1])
- mainloop()
- mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement