Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.67 KB | None | 0 0
  1. # =============================================================================
  2. # IMPORTS
  3. # =============================================================================
  4.  
  5. # Python
  6. from configobj import ConfigObj
  7.  
  8. from sys import exit
  9.  
  10. from time import sleep
  11.  
  12. # VK API
  13. from vkontakte import API
  14.  
  15. # =============================================================================
  16. # GLOBAL VARIABLES
  17. # =============================================================================
  18. PATH="vk1/"
  19. CONFIG=ConfigObj(PATH+"config/config.ini")
  20. STRINGS=ConfigObj(PATH+"config/strings.ini")
  21.  
  22. LOADED=False
  23.  
  24. TOSECONDS=1000
  25.  
  26. def echo(text, args=None):
  27.     if text in STRINGS:
  28.         if args is None:
  29.             print STRINGS[text][MainConfig.language].decode("utf-8")
  30.         else:
  31.             print STRINGS[text][MainConfig.language].format(*args).decode("utf-8")
  32.        
  33.     else:
  34.         print text.format(args).decode("utf-8")
  35.  
  36. # =============================================================================
  37. # CLASSES
  38. # =============================================================================
  39. class Config(object):
  40.     def __init__(self, path):
  41.         self.config=ConfigObj(path)
  42.        
  43.         self.error={}
  44.        
  45.         for line in self.config:
  46.             object.__setattr__(self, line, self.config[line])
  47.            
  48.     def save(self):
  49.         self.config.write()
  50.        
  51.        
  52. MainConfig=Config(PATH+"config/config.ini")
  53.        
  54. class VkAPI(object):
  55.     def __init__(self):
  56.         self.api=None
  57.        
  58.     def connect(self, args):
  59.         try:
  60.             api_id=None
  61.             api_secret=None
  62.            
  63.             if "api_id" in args:
  64.                 api_id=args['api_id']
  65.             if "api_secret" in args:
  66.                 api_secret=args['api_secret']
  67.                
  68.             self.api=API(api_secret=api_secret, api_id=api_id)
  69.             echo("Успешное подключение.")
  70.         except:
  71.             self.api=None
  72.             echo("Не удалось подключиться.")
  73.        
  74.     def disconnect(self):
  75.         self.api=None
  76.        
  77.     def getFriends(self, id):
  78.         if str(id).isdigit():
  79.             if id=="0":
  80.                 id=MainConfig.myid
  81.        
  82.             friends=self.api.friends.get(user_id=int(id))
  83.            
  84.             print friends
  85.            
  86.         else:
  87.             echo("VkApi.IdNotDigital")
  88.            
  89.        
  90. VkAPI=VkAPI()
  91. if MainConfig.autoconnect:
  92.     VkAPI.connect({"api_id":MainConfig.api_id, "api_secret":MainConfig.api_secret})
  93.        
  94. # =============================================================================
  95. # FUNCTIONS
  96. # =============================================================================
  97.        
  98.        
  99. def mainloop():
  100.     global LOADED
  101.     if not LOADED:
  102.         echo("*"*20)
  103.         echo("MainLoop.Loading")
  104.         if MainConfig.config:
  105.             echo("MainLoop.ConfigLoaded")
  106.         else:
  107.             echo("MainLoop.ConfigFailed")
  108.             exit("Configuration file not found!")
  109.        
  110.         echo("MainLoop.Loaded")
  111.         echo("*"*20)
  112.         LOADED=True
  113.  
  114.     else:
  115.         echo("MainLoop.Calling")
  116.         text=raw_input()
  117.        
  118.         if text=="exit":
  119.             echo("*"*20)
  120.             echo("MainLoop.Stop", "2")
  121.             echo("*"*20)
  122.             sleep(2)
  123.             exit()
  124.        
  125.         elif text=="reload":
  126.             import vk1
  127.             reload(vk1)
  128.        
  129.         call=text.split(".")
  130.         if len(call)==2:
  131.             if call[0]=='config':
  132.            
  133.                 if not "=" in call[1]:
  134.                     try:
  135.                         value=getattr(MainConfig, call[1])
  136.                     except:
  137.                         value=None
  138.                    
  139.                     print '.'.join(call), "=", value
  140.                    
  141.                 else:
  142.                
  143.                     args=call[1].split("=")
  144.                    
  145.                     setattr(MainConfig, args[0], args[1])
  146.                    
  147.                     echo("Config.SetValue", args)
  148.             elif call[0]=='strings':
  149.                 if call[1]=='update':
  150.                     global STRINGS
  151.                     STRINGS=ConfigObj(PATH+"config/strings.ini")
  152.        
  153.             elif call[0]=="api":
  154.                 if call[1]=="connect":
  155.                     VkAPI.connect({"api_id":MainConfig.api_id, "api_secret":MainConfig.api_secret})
  156.                 elif call[1]=="disconnect":
  157.                     VkAPI.disconnect()
  158.                 else:
  159.                     args=call[1].split()
  160.                     if args[0]=='friends':
  161.                         if len(args)==1:
  162.                             args.append("0")
  163.                         VkAPI.getFriends(args[1])
  164.                    
  165.        
  166.        
  167.     mainloop()
  168.    
  169. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement