Advertisement
Guest User

Untitled

a guest
Sep 17th, 2017
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.05 KB | None | 0 0
  1. #! /usr/bin/env python
  2. from Tkinter import BooleanVar
  3. from Tkinter import *
  4. from Tkinter import Toplevel
  5. from Tapiv2 import tuentiAPI
  6. '''from Gui import Gui'''
  7. import time
  8. import urllib2
  9.  
  10. class Gui():
  11.     def __init__(self,main):
  12.  
  13.         '''
  14.        iButtons -> Botones que requieren inicio sesion
  15.        phButtons -> Botones que requieren leer fotos
  16.        '''
  17.         self.buttons={'iButtons' : [], 'phButtons' : []}
  18.         gf = LabelFrame(main, text = 'General', relief = RIDGE, labelanchor = 'nw', width = 300, height = 60)
  19.         gf.grid(row = 0, column = 1)
  20.         gf.grid_propagate(0)
  21.         self.guiGeneral(gf,main)
  22.  
  23.         pf = LabelFrame(main, text = "Perfil", relief = GROOVE, labelanchor = 'nw',width = 300, height = 80)
  24.         pf.grid(row = 2, column = 1)
  25.         pf.grid_propagate(0)
  26.         self.guiProfile(pf,main)
  27.    
  28.         phf = LabelFrame(main, text = 'Fotos', relief = RIDGE, labelanchor = 'nw', width = 300, height = 200)
  29.         phf.grid(row = 0, column = 2)
  30.         phf.grid_propagate(0)
  31.         self.guiPhotos(phf,main)
  32.  
  33.         ''''''
  34.         phf_status = LabelFrame(phf, relief = RIDGE, labelanchor = 'nw', width = 295 , height = 55)
  35.         phf_status.grid(row = 3, column = 0)
  36.         phf_status.grid_propagate(0)
  37.         self.guiPhotosStatus(phf_status,main)
  38.  
  39.         mf = LabelFrame(main, text = "Mensajes", relief = GROOVE, labelanchor = 'nw',width = 480, height = 180)
  40.         mf.grid(row = 1, column = 1)
  41.         mf.grid_propagate(0)
  42.         self.guiMessages(mf,main)
  43.        
  44.         '''
  45.        Button(mf, text= "Mostrar mensajes",width=20,state = DISABLED,command = self.showMessages,justify=LEFT),
  46.                Button(mf, text= "Mandar mensaje",state = DISABLED,command = self.sendMessage,justify=LEFT),
  47.                Button(pf, text= "Ver comentarios perfil",state = DISABLED,command = self.showSelfComments),
  48.                Button(pf, text= "Ver comentarios del perfil de",state = DISABLED,command = self.showComments))
  49.  
  50.        main.options['message'] = Text(mf, foreground="black", background="white", highlightcolor="black", highlightbackground="purple", wrap=NONE, height = 7, width = 40)
  51.        
  52.        self.iButtons[0].grid(row = 1, column = 0,sticky=W)
  53.        self.iButtons[1].grid(row = 3, column = 0,sticky=W)
  54.  
  55.        self.iButtons[2].grid(row = 1, column = 1, columnspan = 2)
  56.        self.iButtons[3].grid(row = 2, column = 1, columnspan = 2)
  57.  
  58.        main.options['message'] = Text(mf, foreground="black", background="white", highlightcolor="black", highlightbackground="purple", wrap=NONE, height = 7, width = 40)
  59.        main.options['message'].grid(row = 2, column = 0,padx=0)
  60.        main.options['message'].insert(END,"Escribe un mensaje...")
  61.  
  62.        Label(pf,text = 'id:').grid(row = 2, column = 4)
  63.        Entry(pf,textvariable = self.options['idProfile']).grid(row = 2, column = 5)
  64.  
  65.        Label(mf,text = 'id:').grid(row = 3, column = 1)
  66.        Entry(mf,textvariable = self.options['idPM'],justify=LEFT).grid(row = 3, column = 2)
  67.  
  68.        self.options['idPM'].set('63455577')
  69.        '''
  70.        
  71.  
  72.     def guiGeneral(self,grid,main):
  73.  
  74.         ind = len(self.buttons['iButtons'])
  75.        
  76.         Label(grid, text = 'Email:').grid(row = 0, column = 1)
  77.         Entry(grid, textvariable = main.options['email']).grid(row = 0, column = 2, columnspan = 2)
  78.  
  79.         Label(grid, text = 'Password:').grid(row = 1, column = 1)
  80.         Entry(grid,show="*", textvariable = main.options['password']).grid(row = 1, column = 2, columnspan = 2)
  81.  
  82.         self.cButton = Button(grid, text= "Conectar",command=main.connect,width=15)
  83.         self.cButton.grid(row = 1, column = 5, columnspan = 2)        
  84.  
  85.     def guiProfile(self,grid,main):
  86.        
  87.         ind = len(self.buttons['iButtons'])
  88.         self.buttons['iButtons'].append(
  89.                 Button(grid, text= "Ver comentarios perfil",state = DISABLED,command = main.showSelfComments))
  90.         self.buttons['iButtons'].append(
  91.                 Button(grid, text= "Ver comentarios del perfil de",state = DISABLED,command = main.showComments))
  92.         self.buttons['iButtons'][ind].grid(row = 0, column = 1)
  93.         self.buttons['iButtons'][ind+1].grid(row = 1, column = 1)
  94.        
  95.         Label(grid,text = 'id:').grid(row = 1, column = 2)
  96.         Entry(grid,textvariable = main.options['idProfile']).grid(row = 1, column = 3)
  97.  
  98.     def guiPhotos(self,grid,main):
  99.         self.buttons['phButtons'].append(
  100.             Button(grid, text= "Ver fotos",state = DISABLED,command = main.viewPhotos,justify=LEFT)
  101.             )
  102.         self.buttons['phButtons'][0].grid(row = 4, column = 0,sticky=W)
  103.         pass
  104.  
  105.     def guiPhotosStatus(self,grid,main):
  106.  
  107.         ind = len(self.buttons['iButtons'])
  108.  
  109.         lb1 = LabelFrame(grid, relief = FLAT, labelanchor = 'nw', width = 300, height = 20)
  110.         lb1.grid(row = 0, column = 0,sticky=W)
  111.         lb1.grid_propagate(0)
  112.        
  113.         Label(lb1, text = 'Estado:').grid(row = 0, column = 0,sticky=W)
  114.         self.phStatus = Label(lb1, text = 'No hay fotos cargadas',fg='red')
  115.         self.phStatus.grid(row = 0, column = 1,sticky=W)
  116.  
  117.         lb2 = LabelFrame(grid, relief = FLAT, labelanchor = 'nw', width = 270, height = 30)
  118.         lb2.grid(row = 1, column = 0)
  119.         lb2.grid_propagate(0)
  120.         self.buttons['iButtons'].append(
  121.         Button(lb2, text= "Leer mis fotos",command = main.getPhotos,justify=LEFT,state=DISABLED)
  122.         )
  123.         self.buttons['iButtons'].append(
  124.         Button(lb2, text= "Leer fotos de",command = main.getPhotos,justify=LEFT,state=DISABLED)
  125.         )
  126.         self.buttons['iButtons'][ind].grid(row = 1, column = 0,sticky=W)
  127.         self.buttons['iButtons'][ind+1].grid(row = 1, column = 1,sticky=W,padx=3)
  128.         Label(lb2, text = 'Id:').grid(row = 1, column = 2,sticky=W,padx=0)
  129.         Entry(lb2, textvariable = main.options['idPhoto']).grid(row = 1, column = 3, columnspan = 2,sticky=W)
  130.        
  131.  
  132.  
  133.     def guiMessages(self,grid,main):
  134.         ind = len(self.buttons['iButtons'])
  135.         self.buttons['iButtons'].append(
  136.                 Button(grid, text= "Mostrar mensajes",width=20,state = DISABLED,command = main.showMessages,justify=LEFT)
  137.                 )
  138.        
  139.         self.buttons['iButtons'].append(
  140.                 Button(grid, text= "Mandar mensaje",state = DISABLED,command = main.sendMessage,justify=LEFT)
  141.                 )
  142.  
  143.         self.buttons['iButtons'][ind].grid(row = 1, column = 0,sticky=W)
  144.         self.buttons['iButtons'][ind+1].grid(row = 3, column = 0,sticky=W)
  145.  
  146.         main.options['message'] = Text(grid, foreground="black", background="white", highlightcolor="black", highlightbackground="purple", wrap=NONE, height = 7, width = 40)
  147.         main.options['message'].grid(row = 2, column = 0,padx=0)
  148.         main.options['message'].insert(END,"Escribe un mensaje...")
  149.        
  150.    
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160. class Main(Tk):
  161.     def __init__(self):
  162.         Tk.__init__(self)
  163.         self.title(string = "PyTuenti")
  164.         '''.resizable(width=FALSE, height=FALSE)
  165.        '''
  166.         self.options = {
  167.             'email' : StringVar(),
  168.             'password' : StringVar(),
  169.             'idProfile' : IntVar(),
  170.             'message' : StringVar(),
  171.             'idPM' : IntVar(),
  172.             'idPhoto' : IntVar()
  173.         }
  174.  
  175.         self.photos = None
  176.         self.connected = False
  177.         self.phGot = False
  178.         self.options['email'].set('agrs700@hotmail.com')
  179.         self.options['password'].set('axelrulez92')
  180.         self.gui=Gui(self)
  181.        
  182.        
  183.     def connect(self):
  184.         if(not self.connected):
  185.             self.api = tuentiAPI(self.options['email'].get(),self.options['password'].get())
  186.             self.gui.cButton['text']="Desconectar"
  187.             self.connected=TRUE
  188.             for button in self.gui.buttons['iButtons']:
  189.                 button['state'] = ACTIVE
  190.         else:
  191.             self.connected=FALSE
  192.             self.gui.cButton['text']="Conectar"
  193.             for button in self.gui.buttons['iButtons']:
  194.                 button['state'] = DISABLED
  195.             del self.api
  196.    
  197.     def showMessages(self):
  198.         messages = self.api.request("getInbox",{'user_id' : "1024"})
  199.         for i in messages['threads']:
  200.             print i['counter_part']['name'] +' '+ i['counter_part']['surname']
  201.             print i['preview']
  202.    
  203.     def showSelfComments(self):
  204.         comments = self.api.request("getProfileWall",{})
  205.         for comment in comments['posts']:
  206.             print comment['author']['name']+" " +comment['author']['surname']
  207.             print comment['body'][0]['plain']
  208.  
  209.     def showComments(self):
  210.         comments = self.api.request("getProfileWall",{'user_id' : self.options['idProfile'].get()})
  211.         for comment in comments['posts']:
  212.             print comment['author']['name']+" " +comment['author']['surname']
  213.             print comment['body'][0]['plain']
  214.     def sendMessage(self):
  215.         print self.api.request("sendMessage",{'recipient' : self.options['idPM'].get(), 'body' : self.options['message'].get(0.0,END)})
  216.  
  217.     def getPhotos(self):
  218.         print self.gui.phStatus
  219.         user = self.api.request("getUsersData",{'ids' : [self.options['idPhoto'].get()]})
  220.         print self.options['idPhoto'].get()
  221.         if 'error' in user:
  222.             print "Error: Usuario no valido"
  223.             self.photos = None
  224.             for button in self.gui.buttons['phButtons']:
  225.                     button['state'] = DISABLED
  226.         else:
  227.             if user['users'][0]['can_see'] == False:
  228.                 print "Error: No puedes ver las fotos del usuario introducido\nComprueba que esta en tu lista de amigos o si puedes ver su perfil"
  229.                 self.photos = None
  230.                 for button in self.gui.buttons['phButtons']:
  231.                     button['state'] = DISABLED
  232.             else:
  233.                 albums = self.api.request("getUserAlbums",{'user_id' : self.options['idPhoto'].get()})
  234.                 album_list = [[a, albums[a]['size'], albums[a]['size']/25 if albums[a]['size']%25 == 0 else albums[a]['size']/25+1] for a in albums]
  235.                 self.photos = []
  236.                 for album in album_list:
  237.                     print 'Nombre del Album: %s\nNumero de Elementos: %d\nNumero de paginas: %d' % tuple(album)
  238.                     print album
  239.                     for p in range(album[2]):
  240.                         print 'Obteniendo pagina: %d' % p
  241.                         for e in self.api.request("getAlbumPhotos",{'user_id' : self.options['idPhoto'].get(),'album_id' : album[0], 'page' : p,})['album']:
  242.                             self.photos.append(e)
  243.                 self.gui.phStatus['foreground'] = 'lime green'
  244.                 self.gui.phStatus['text'] = 'Cargadas fotos de ' + user['users'][0]['name'] + " " + user['users'][0]['surname']
  245.                 for button in self.gui.buttons['phButtons']:
  246.                     button['state'] = ACTIVE
  247.  
  248.     def viewPhotos(self):
  249.         for e in self.photos:
  250.             print e['photo_url_600']
  251.  
  252. if __name__ == '__main__':
  253.     try:
  254.         mw = Main()
  255.         mw.mainloop()
  256.     except Exception, ex:
  257.         print(ex)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement