Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.21 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 = 1, 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.         gid = LabelFrame(main, text = "Id", relief = GROOVE, labelanchor = 'nw',width = 340, height = 100)
  45.         gid.grid(row = 0, column = 2)
  46.         gid.grid_propagate(0)
  47.         self.guiGetId(gid,main)
  48.        
  49.         '''
  50.        Button(mf, text= "Mostrar mensajes",width=20,state = DISABLED,command = self.showMessages,justify=LEFT),
  51.                Button(mf, text= "Mandar mensaje",state = DISABLED,command = self.sendMessage,justify=LEFT),
  52.                Button(pf, text= "Ver comentarios perfil",state = DISABLED,command = self.showSelfComments),
  53.                Button(pf, text= "Ver comentarios del perfil de",state = DISABLED,command = self.showComments))
  54.  
  55.        main.options['message'] = Text(mf, foreground="black", background="white", highlightcolor="black", highlightbackground="purple", wrap=NONE, height = 7, width = 40)
  56.        
  57.        self.iButtons[0].grid(row = 1, column = 0,sticky=W)
  58.        self.iButtons[1].grid(row = 3, column = 0,sticky=W)
  59.  
  60.        self.iButtons[2].grid(row = 1, column = 1, columnspan = 2)
  61.        self.iButtons[3].grid(row = 2, column = 1, columnspan = 2)
  62.  
  63.        main.options['message'] = Text(mf, foreground="black", background="white", highlightcolor="black", highlightbackground="purple", wrap=NONE, height = 7, width = 40)
  64.        main.options['message'].grid(row = 2, column = 0,padx=0)
  65.        main.options['message'].insert(END,"Escribe un mensaje...")
  66.  
  67.        Label(pf,text = 'id:').grid(row = 2, column = 4)
  68.        Entry(pf,textvariable = self.options['idProfile']).grid(row = 2, column = 5)
  69.  
  70.        Label(mf,text = 'id:').grid(row = 3, column = 1)
  71.        Entry(mf,textvariable = self.options['idPM'],justify=LEFT).grid(row = 3, column = 2)
  72.  
  73.        self.options['idPM'].set('63455577')
  74.        '''
  75.        
  76.  
  77.     def guiGeneral(self,grid,main):
  78.  
  79.         ind = len(self.buttons['iButtons'])
  80.        
  81.         Label(grid, text = 'Email:').grid(row = 0, column = 1)
  82.         Entry(grid, textvariable = main.options['email']).grid(row = 0, column = 2, columnspan = 2)
  83.  
  84.         Label(grid, text = 'Password:').grid(row = 1, column = 1)
  85.         Entry(grid,show="*", textvariable = main.options['password']).grid(row = 1, column = 2, columnspan = 2)
  86.  
  87.         self.cButton = Button(grid, text= "Conectar",command=main.connect,width=15)
  88.         self.cButton.grid(row = 1, column = 5, columnspan = 2)        
  89.  
  90.     def guiProfile(self,grid,main):
  91.        
  92.         ind = len(self.buttons['iButtons'])
  93.         self.buttons['iButtons'].append(
  94.                 Button(grid, text= "Ver comentarios perfil",state = DISABLED,command = main.showSelfComments))
  95.         self.buttons['iButtons'].append(
  96.                 Button(grid, text= "Ver comentarios del perfil de",state = DISABLED,command = main.showComments))
  97.         self.buttons['iButtons'][ind].grid(row = 0, column = 1)
  98.         self.buttons['iButtons'][ind+1].grid(row = 1, column = 1)
  99.        
  100.         Label(grid,text = 'id:').grid(row = 1, column = 2)
  101.         Entry(grid,textvariable = main.options['idProfile']).grid(row = 1, column = 3)
  102.  
  103.     def guiPhotos(self,grid,main):
  104.         self.buttons['phButtons'].append(
  105.             Button(grid, text= "Ver fotos",state = DISABLED,command = main.viewPhotos,justify=LEFT)
  106.             )
  107.         self.buttons['phButtons'][0].grid(row = 4, column = 0,sticky=W)
  108.         pass
  109.  
  110.     def guiPhotosStatus(self,grid,main):
  111.  
  112.         ind = len(self.buttons['iButtons'])
  113.  
  114.         lb1 = LabelFrame(grid, relief = FLAT, labelanchor = 'nw', width = 300, height = 20)
  115.         lb1.grid(row = 0, column = 0,sticky=W)
  116.         lb1.grid_propagate(0)
  117.        
  118.         Label(lb1, text = 'Estado:').grid(row = 0, column = 0,sticky=W)
  119.         self.phStatus = Label(lb1, text = 'No hay fotos cargadas',fg='red')
  120.         self.phStatus.grid(row = 0, column = 1,sticky=W)
  121.  
  122.         lb2 = LabelFrame(grid, relief = FLAT, labelanchor = 'nw', width = 270, height = 30)
  123.         lb2.grid(row = 1, column = 0)
  124.         lb2.grid_propagate(0)
  125.         self.buttons['iButtons'].append(
  126.         Button(lb2, text= "Leer mis fotos",command = main.getPhotos,justify=LEFT,state=DISABLED)
  127.         )
  128.         self.buttons['iButtons'].append(
  129.         Button(lb2, text= "Leer fotos de",command = main.getPhotos,justify=LEFT,state=DISABLED)
  130.         )
  131.         self.buttons['iButtons'][ind].grid(row = 1, column = 0,sticky=W)
  132.         self.buttons['iButtons'][ind+1].grid(row = 1, column = 1,sticky=W,padx=3)
  133.         Label(lb2, text = 'Id:').grid(row = 1, column = 2,sticky=W,padx=0)
  134.         Entry(lb2, textvariable = main.options['idPhoto']).grid(row = 1, column = 3, columnspan = 2,sticky=W)
  135.        
  136.  
  137.  
  138.     def guiMessages(self,grid,main):
  139.         ind = len(self.buttons['iButtons'])
  140.         self.buttons['iButtons'].append(
  141.                 Button(grid, text= "Mostrar mensajes",width=20,state = DISABLED,command = main.showMessages,justify=LEFT)
  142.                 )
  143.        
  144.         self.buttons['iButtons'].append(
  145.                 Button(grid, text= "Mandar mensaje",state = DISABLED,command = main.sendMessage,justify=LEFT)
  146.                 )
  147.  
  148.         self.buttons['iButtons'][ind].grid(row = 1, column = 0,sticky=W)
  149.         self.buttons['iButtons'][ind+1].grid(row = 3, column = 0,sticky=W)
  150.  
  151.         main.options['message'] = Text(grid, foreground="black", background="white", highlightcolor="black", highlightbackground="purple", wrap=NONE, height = 7, width = 40)
  152.         main.options['message'].grid(row = 2, column = 0,padx=0)
  153.         main.options['message'].insert(END,"Escribe un mensaje...")
  154.  
  155.         Label(grid,text = 'id:').grid(row = 3, column = 1)
  156.         Entry(grid,textvariable = main.options['idPM'],justify=LEFT).grid(row = 3, column = 2)
  157.  
  158.        
  159.     def guiGetId(self,grid,main):
  160.        
  161.         ind = len(self.buttons['iButtons'])
  162.  
  163.         lb1 = LabelFrame(grid, relief = FLAT, labelanchor = 'nw', width = 160, height = 40)
  164.         lb1.grid(row = 1, column = 0,sticky=W)
  165.         lb1.grid_propagate(0)
  166.        
  167.         Label(lb1, text = 'Nombre:').grid(row = 1, column = 1,padx=0,sticky=W)
  168.         Entry(lb1, textvariable = main.options['nameSearch']).grid(row = 1, column = 2, columnspan = 2,sticky=W)
  169.        
  170.         Label(lb1, text = 'Apellidos:').grid(row = 2, column = 1,sticky=W,padx=0)
  171.         Entry(lb1, textvariable = main.options['surnameSearch']).grid(row = 2, column = 2, columnspan = 2,sticky=W)
  172.        
  173.         self.buttons['iButtons'].append(
  174.             Button(grid, text = "Obtener id",state = DISABLED,command = main.getId)
  175.             )
  176.         self.buttons['iButtons'][ind].grid(row = 3, column = 1,sticky=NW)
  177.  
  178.         lb2 = LabelFrame(grid, relief = FLAT, labelanchor = 'nw', width = 240, height = 40)
  179.         lb2.grid(row = 3, column = 0,sticky=W)
  180.         lb2.grid_propagate(0)
  181.  
  182.         self.searchStatus = Label(lb2, text = '',fg='red')
  183.         self.searchStatus.grid(row = 2, column = 1,sticky=W)
  184.         Label(lb2, text = 'Estado:').grid(row = 2, column = 0,sticky=W)
  185.  
  186.         Label(lb2, text = 'Id obtenida:').grid(row = 1, column = 0,sticky=W,padx=0)
  187.         Entry(lb2, textvariable = main.idFound,width = 28).grid(row = 1, column = 1, columnspan = 2,sticky=W)
  188.  
  189.  
  190.  
  191.  
  192.  
  193. class Main(Tk):
  194.     def __init__(self):
  195.         Tk.__init__(self)
  196.         self.title(string = "PyTuenti")
  197.         '''.resizable(width=FALSE, height=FALSE)
  198.        '''
  199.         self.options = {
  200.             'email' : StringVar(),
  201.             'password' : StringVar(),
  202.             'idProfile' : IntVar(),
  203.             'message' : StringVar(),
  204.             'idPM' : IntVar(),
  205.             'idPhoto' : IntVar(),
  206.             'nameSearch' : StringVar(),
  207.             'surnameSearch' : StringVar()
  208.         }
  209.         self.friendsData = None
  210.         self.idFound= StringVar();
  211.         self.photos = None
  212.         self.connected = False
  213.         self.phGot = False
  214.         self.options['email'].set('agrs700@hotmail.com')
  215.         self.options['password'].set('axelrulez92')
  216.         self.gui=Gui(self)
  217.        
  218.        
  219.     def connect(self):
  220.         if(not self.connected):
  221.             self.api = tuentiAPI(self.options['email'].get(),self.options['password'].get())
  222.             self.friendsData = (self.api.request("getFriendsData",{}))['friends']
  223.             self.gui.cButton['text']="Desconectar"
  224.             self.connected=TRUE
  225.             for button in self.gui.buttons['iButtons']:
  226.                 button['state'] = ACTIVE
  227.         else:
  228.             self.connected=FALSE
  229.             self.gui.cButton['text']="Conectar"
  230.             for button in self.gui.buttons['iButtons']:
  231.                 button['state'] = DISABLED
  232.             del self.api
  233.    
  234.     def showMessages(self):
  235.         messages = self.api.request("getInbox",{'user_id' : "1024"})
  236.         for i in messages['threads']:
  237.             print i['counter_part']['name'] +' '+ i['counter_part']['surname']
  238.             print i['preview']
  239.    
  240.     def showSelfComments(self):
  241.         comments = self.api.request("getProfileWall",{})
  242.         for comment in comments['posts']:
  243.             print comment['author']['name']+" " +comment['author']['surname']
  244.             print comment['body'][0]['plain']
  245.  
  246.     def showComments(self):
  247.         comments = self.api.request("getProfileWall",{'user_id' : self.options['idProfile'].get()})
  248.         for comment in comments['posts']:
  249.             print comment['author']['name']+" " +comment['author']['surname']
  250.             print comment['body'][0]['plain']
  251.     def sendMessage(self):
  252.         print self.api.request("sendMessage",{'recipient' : self.options['idPM'].get(), 'body' : self.options['message'].get(0.0,END)})
  253.  
  254.     def getId(self):
  255.         ids = []
  256.         i = 0
  257.         print self.options['nameSearch'].get()
  258.         for friendData in self.friendsData:
  259.             if(self.options['nameSearch'].get().lower().startswith(friendData['name'].lower())):
  260.                 if(self.options['surnameSearch'].get()==""):
  261.                     ids.append(friendData['id'])
  262.                     i = i+1
  263.                 elif(self.options['surnameSearch'].get().lower() == friendData['surname'].lower()):
  264.                     ids.append(friendData['id'])
  265.                     i = i+1
  266.                
  267.                    
  268.         if(i==0):
  269.             self.gui.searchStatus['text'] = "Ningun usuario encontrado"
  270.             self.gui.searchStatus['foreground'] = 'red'
  271.         elif(i>1):
  272.             self.gui.searchStatus['text'] = "Encontrados mas de un usuario"
  273.             self.gui.searchStatus['foreground'] = 'lime green'
  274.         else:
  275.             self.gui.searchStatus['text'] = "Usuario encontrado"
  276.             self.gui.searchStatus['foreground'] = 'lime green'
  277.            
  278.         idsStr = str(ids).strip('[]')
  279.         self.idFound.set(idsStr)
  280.  
  281.        
  282.     def getPhotos(self):
  283.         print self.gui.phStatus
  284.         user = self.api.request("getUsersData",{'ids' : [self.options['idPhoto'].get()]})
  285.         print self.options['idPhoto'].get()
  286.         if 'error' in user:
  287.             print "Error: Usuario no valido"
  288.             self.photos = None
  289.             for button in self.gui.buttons['phButtons']:
  290.                     button['state'] = DISABLED
  291.         else:
  292.             if user['users'][0]['can_see'] == False:
  293.                 print "Error: No puedes ver las fotos del usuario introducido\nComprueba que esta en tu lista de amigos o si puedes ver su perfil"
  294.                 self.photos = None
  295.                 for button in self.gui.buttons['phButtons']:
  296.                     button['state'] = DISABLED
  297.             else:
  298.                 albums = self.api.request("getUserAlbums",{'user_id' : self.options['idPhoto'].get()})
  299.                 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]
  300.                 self.photos = []
  301.                 for album in album_list:
  302.                     print 'Nombre del Album: %s\nNumero de Elementos: %d\nNumero de paginas: %d' % tuple(album)
  303.                     print album
  304.                     for p in range(album[2]):
  305.                         print 'Obteniendo pagina: %d' % p
  306.                         for e in self.api.request("getAlbumPhotos",{'user_id' : self.options['idPhoto'].get(),'album_id' : album[0], 'page' : p,})['album']:
  307.                             self.photos.append(e)
  308.                 self.gui.phStatus['foreground'] = 'lime green'
  309.                 self.gui.phStatus['text'] = 'Cargadas fotos de ' + user['users'][0]['name'] + " " + user['users'][0]['surname']
  310.                 for button in self.gui.buttons['phButtons']:
  311.                     button['state'] = ACTIVE
  312.  
  313.     def viewPhotos(self):
  314.         for e in self.photos:
  315.             print e['photo_url_600']
  316.  
  317. if __name__ == '__main__':
  318.     try:
  319.         mw = Main()
  320.         mw.mainloop()
  321.     except Exception, ex:
  322.         print(ex)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement