├── images ├── sorteados │ └── .gitkeep └── fb-icon.png ├── url.txt ├── icon.ico ├── .gitignore ├── README.md ├── fbk.py └── sorteio.py /images/sorteados/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /url.txt: -------------------------------------------------------------------------------- 1 | Salve o link aqui! 2 | -------------------------------------------------------------------------------- /icon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/delete/sorteio-de-amigos/master/icon.ico -------------------------------------------------------------------------------- /images/fb-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/delete/sorteio-de-amigos/master/images/fb-icon.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.py[cod] 2 | 3 | # C extensions 4 | *.so 5 | 6 | # Packages 7 | *.egg 8 | *.egg-info 9 | dist 10 | build 11 | eggs 12 | parts 13 | bin 14 | var 15 | sdist 16 | develop-eggs 17 | .installed.cfg 18 | lib 19 | lib64 20 | 21 | # Installer logs 22 | pip-log.txt 23 | 24 | # Unit test / coverage reports 25 | .coverage 26 | .tox 27 | nosetests.xml 28 | 29 | # Translations 30 | *.mo 31 | 32 | # Mr Developer 33 | .mr.developer.cfg 34 | .project 35 | .pydevproject 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Sorteio de amigos do Facebook 2 | ============================= 3 | 4 | Para que o programa funcione, é preciso atualizar a cada uma hora um link que o Facebook disponibiliza. 5 | 6 | Acesse: https://developers.facebook.com/docs/reference/api/examples/ 7 | 8 | "Connections -> Friends:" e copie o link dado. 9 | 10 | Cole o link no programa. 11 | 12 | É necessário ter o Python3 e o Qt4 instalados. 13 | 14 | #--Instalação 15 | 16 | -Debian-Like 17 | 18 | sudo apt-get install python3 19 | 20 | sudo apt-get install python3-pyqt4 21 | 22 | -Windows/Mac 23 | 24 | Baixar o Python3: http://python.org/download/ 25 | 26 | Baixar o PyQt4: http://www.riverbankcomputing.co.uk/software/pyqt/download 27 | 28 | ![](http://i.imgur.com/Pq7E5Q8.jpg) 29 | -------------------------------------------------------------------------------- /fbk.py: -------------------------------------------------------------------------------- 1 | import urllib.request 2 | import json 3 | import os 4 | 5 | class Facebook(object): 6 | 7 | def __init__(self,user): 8 | self.user = user 9 | 10 | def dados(url): 11 | #Pega os dados no Facebook 12 | #Precisa trocar a URL a cada 1h 13 | #Pegar no site, https://developers.facebook.com/docs/reference/api/examples/ 14 | 15 | resp = urllib.request.urlopen(url).read() 16 | data = json.loads(resp.decode('utf-8')) 17 | return data 18 | 19 | def baixarFoto(self): 20 | #data = self.procura() 21 | url = 'https://graph.facebook.com/' + self.user + '/picture?width=200&height=200' 22 | figura = urllib.request.urlopen(url).read() 23 | arq = 'images/sorteados/' + self.user + '.jpg' 24 | f = open(arq,"wb") 25 | f.write(figura) 26 | f.close() 27 | return arq 28 | 29 | def procura(self): 30 | url = 'https://graph.facebook.com/' 31 | resp = urllib.request.urlopen(url+self.user).read() 32 | data = json.loads(resp.decode('utf-8')) 33 | return data 34 | 35 | def profile(self): #imprime todos os dados de um user 36 | data = self.procura() 37 | print ("User: " + data['username']) 38 | print ("Nome: " + data['first_name']) 39 | print ("Sobrenome: " + data['last_name']) 40 | print ("Local: " + data['locale']) 41 | print ("Sexo: " + data['gender']) 42 | print ("ID: " + data['id']) 43 | 44 | #Recebe a URL token, e o tipo chave que sera adicionada 45 | #Podendo ser 'id' ou 'username' 46 | #Retorna um tupla com a lista e o tamanho dela 47 | def listarAmigos(url, tipo): 48 | lista = [] 49 | cont = 0 50 | 51 | #Mudar a cada 1h 52 | #Pegar no site, https://developers.facebook.com/docs/reference/api/examples/ 53 | #Connections -> Friends 54 | data = Facebook.dados(url) 55 | for amigo in data['data']: 56 | lista.append(amigo[tipo]) #adiciona na lista pelo id 57 | cont += 1 #conta o numero de amigos 58 | return lista,cont 59 | 60 | def limpaDados(): 61 | total_de_pessoas = 10 62 | try: 63 | dir_atual = os.getcwd() 64 | lista_Sorteados = os.listdir(dir_atual + '/images/sorteados/') 65 | if len(lista_Sorteados) > total_de_pessoas: 66 | for pessoa in lista_Sorteados: 67 | os.remove(dir_atual + '/images/sorteados/' + pessoa) 68 | except OSError: 69 | print ("\nArquivo ou diretório não existe!\n") 70 | -------------------------------------------------------------------------------- /sorteio.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # Form implementation generated from reading ui file 'interface.ui' 4 | # 5 | # Created: Tue Apr 2 18:18:07 2013 6 | # by: PyQt4 UI code generator 4.9.1 7 | # Versão 1.2 8 | 9 | ############################## 10 | # 11 | #Mudar a cada 1h 12 | #Pegar no site, https://developers.facebook.com/docs/reference/api/examples/ 13 | #Connections -> Friends 14 | #Mudar no arquivo url 15 | # 16 | ############################### 17 | 18 | from PyQt4 import QtCore, QtGui 19 | from PyQt4.QtCore import * 20 | import sys 21 | import os 22 | import random 23 | from fbk import Facebook #arquivo fbk.py 24 | 25 | 26 | try: 27 | _fromUtf8 = QtCore.QString.fromUtf8 28 | except AttributeError: 29 | _fromUtf8 = lambda s: s 30 | 31 | 32 | #Janela principal 33 | class Ui_Dialog(object): 34 | def setupUi(self, Dialog): 35 | Dialog.setObjectName(_fromUtf8("Dialog")) 36 | Dialog.resize(320, 345) 37 | Dialog.setMaximumSize(QtCore.QSize(320, 345)) 38 | Dialog.setWindowIcon(QtGui.QIcon('icon.ico')) 39 | 40 | #Sortear 41 | self.pushButton = QtGui.QPushButton(Dialog) 42 | self.pushButton.setGeometry(QtCore.QRect(110, 300, 99, 24)) 43 | self.pushButton.setObjectName(_fromUtf8("pushButton")) 44 | QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.atividade) 45 | 46 | #Nome da pessoa 47 | self.label = QtGui.QLabel(Dialog) 48 | self.label.setGeometry(QtCore.QRect(40, 260, 231, 20)) 49 | self.label.setLayoutDirection(QtCore.Qt.LeftToRight) 50 | self.label.setAlignment(QtCore.Qt.AlignCenter) 51 | self.label.setObjectName(_fromUtf8("label")) 52 | 53 | self.label2 = QtGui.QLabel(Dialog) 54 | self.label2.setGeometry(QtCore.QRect(206, 320, 101, 20)) 55 | self.label2.setLayoutDirection(QtCore.Qt.LeftToRight) 56 | self.label2.setAlignment(QtCore.Qt.AlignCenter) 57 | self.label2.setObjectName(_fromUtf8("label2")) 58 | QtCore.QObject.connect(self.label2, QtCore.SIGNAL("linkActivated(QString)"), self.openUrl) 59 | 60 | 61 | self.image = QtGui.QLabel(Dialog) 62 | self.image.setPixmap(QtGui.QPixmap('images/fb-icon.png')) # imagem default 63 | self.image.setGeometry(60, 30, 200, 200) 64 | 65 | self.retranslateUi(Dialog) 66 | QtCore.QMetaObject.connectSlotsByName(Dialog) 67 | 68 | def retranslateUi(self, Dialog): 69 | Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Sorteio de Amigos", None, QtGui.QApplication.UnicodeUTF8)) 70 | self.pushButton.setText(QtGui.QApplication.translate("Dialog", "Sortear", None, QtGui.QApplication.UnicodeUTF8)) 71 | self.label.setText(QtGui.QApplication.translate("Dialog","Que os sorteios comecem!" , None, QtGui.QApplication.UnicodeUTF8)) 72 | self.label2.setText(QtGui.QApplication.translate("Dialog", 'Contato', None, QtGui.QApplication.UnicodeUTF8)) 73 | 74 | def atividade(self): 75 | #Deleta as fotos, se tiverem mais de 10 76 | Facebook.limpaDados() 77 | 78 | #Sorteia uma pessoa 79 | sorteado = random.choice(lista) 80 | 81 | #Intancia um objeto com o vencedor 82 | f = Facebook(sorteado) 83 | 84 | #Procura o nome pelo id 85 | data = f.procura() 86 | 87 | #Baixa a foto do perfil 88 | arq = f.baixarFoto() 89 | 90 | #Deleta o sorteado da lista 91 | lista.remove(sorteado) 92 | 93 | #Muda os dados do sorteado na janela 94 | #self.label.setText(QtGui.QApplication.translate("Dialog",''+ data['name'] +'', None, QtGui.QApplication.UnicodeUTF8)) 96 | QtCore.QObject.connect(self.label, QtCore.SIGNAL("linkActivated(QString)"), self.openUrl) 97 | self.image.setPixmap(QtGui.QPixmap(arq)) 98 | 99 | def openUrl(self,URL): 100 | QtGui.QDesktopServices().openUrl(QUrl(URL)) 101 | 102 | 103 | 104 | #Janela de erro 105 | class Error(object): 106 | def setupUi(self, Dialog): 107 | Dialog.setObjectName(_fromUtf8("Dialog")) 108 | Dialog.resize(400, 245) 109 | Dialog.setMaximumSize(QtCore.QSize(400, 245)) 110 | Dialog.setWindowIcon(QtGui.QIcon('icon.ico')) 111 | 112 | self.label = QtGui.QLabel(Dialog) 113 | self.label.setGeometry(QtCore.QRect(120, 10, 181, 20)) 114 | self.label.setObjectName(_fromUtf8("label")) 115 | 116 | self.label2 = QtGui.QLabel(Dialog) 117 | self.label2.setGeometry(QtCore.QRect(50, 40, 301, 31)) 118 | self.label2.setObjectName(_fromUtf8("label2")) 119 | QtCore.QObject.connect(self.label2, QtCore.SIGNAL("linkActivated(QString)"), self.openUrl) 120 | 121 | self.label3 = QtGui.QLabel(Dialog) 122 | self.label3.setGeometry(QtCore.QRect(80, 70, 241, 31)) 123 | self.label3.setObjectName(_fromUtf8("label3")) 124 | 125 | #Contato 126 | self.label4 = QtGui.QLabel(Dialog) 127 | self.label4.setGeometry(QtCore.QRect(340, 220, 56, 15)) 128 | self.label4.setObjectName(_fromUtf8("label4")) 129 | QtCore.QObject.connect(self.label4, QtCore.SIGNAL("linkActivated(QString)"), self.openUrl) 130 | 131 | #Botão sair 132 | self.pushButton = QtGui.QPushButton(Dialog) 133 | self.pushButton.setGeometry(QtCore.QRect(250, 190, 99, 24)) 134 | self.pushButton.setObjectName(_fromUtf8("pushButton")) 135 | 136 | #Botão atualizar 137 | self.pushButton2 = QtGui.QPushButton(Dialog) 138 | self.pushButton2.setGeometry(QtCore.QRect(40, 190, 99, 24)) 139 | self.pushButton2.setObjectName(_fromUtf8("pushButton2")) 140 | 141 | self.textEdit = QtGui.QTextEdit(Dialog) 142 | self.textEdit.setGeometry(QtCore.QRect(10, 110, 371, 51)) 143 | self.textEdit.setObjectName(_fromUtf8("textEdit")) 144 | 145 | QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.sair) 146 | QtCore.QObject.connect(self.pushButton2, QtCore.SIGNAL("clicked()"), self.atualizar) 147 | 148 | self.retranslateUi(Dialog) 149 | QtCore.QMetaObject.connectSlotsByName(Dialog) 150 | 151 | def retranslateUi(self, Dialog): 152 | Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "ERROR", None, QtGui.QApplication.UnicodeUTF8)) 153 | self.label.setText(QtGui.QApplication.translate("Dialog", "Token do Facebook expirou.", None, QtGui.QApplication.UnicodeUTF8)) 154 | self.label2.setText(QtGui.QApplication.translate("Dialog", 'Copie a URL de: "Connections -> Friends" nesse' + ' link.', None, QtGui.QApplication.UnicodeUTF8)) 155 | self.label3.setText(QtGui.QApplication.translate("Dialog", "Cole a URL, atualize e reabra o programa.", None, QtGui.QApplication.UnicodeUTF8)) 156 | self.label4.setText(QtGui.QApplication.translate("Dialog", 'Contato', None, QtGui.QApplication.UnicodeUTF8)) 157 | self.pushButton.setText(QtGui.QApplication.translate("Dialog", "Sair", None, QtGui.QApplication.UnicodeUTF8)) 158 | self.pushButton2.setText(QtGui.QApplication.translate("Dialog", "Atualizar", None, QtGui.QApplication.UnicodeUTF8)) 159 | self.textEdit.setText(QtGui.QApplication.translate("Dialog", "", None, QtGui.QApplication.UnicodeUTF8)) 160 | 161 | def sair(self): 162 | sys.exit() 163 | 164 | def atualizar(self): 165 | url = self.textEdit.toPlainText() 166 | arq = open("url.txt", "w") 167 | arq.write(url) 168 | arq.close() 169 | self.sair() 170 | 171 | def openUrl(self,URL): 172 | QtGui.QDesktopServices().openUrl(QUrl(URL)) 173 | 174 | def carregarUrl(): 175 | arq = open("url.txt", "r") 176 | url = arq.read() 177 | arq.close() 178 | return url 179 | 180 | 181 | if __name__ == "__main__": 182 | 183 | try: 184 | url = carregarUrl() 185 | lista,quantidade = Facebook.listarAmigos(url,'id') 186 | except Exception: #abre a janela de erro 187 | app = QtGui.QApplication(sys.argv) 188 | error = QtGui.QDialog() 189 | ui = Error() 190 | ui.setupUi(error) 191 | error.show() 192 | sys.exit(app.exec_()) 193 | else: 194 | app = QtGui.QApplication(sys.argv) 195 | Dialog = QtGui.QDialog() 196 | ui = Ui_Dialog() 197 | ui.setupUi(Dialog) 198 | Dialog.show() 199 | sys.exit(app.exec_()) --------------------------------------------------------------------------------