Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Command;
- use Cake\Console\Arguments;
- use Cake\Console\Command;
- use Cake\Console\ConsoleIo;
- use Cake\Console\ConsoleOptionParser;
- use Cake\Filesystem\Folder;
- use Cake\Filesystem\File;
- use InstagramAPI\Instagram;
- use InstagramAPI\Signatures;
- use InstagramAPI\Exception;
- class AddidolCommand extends Command {
- public $Accounts;
- public $Members;
- public $Idols;
- public function initialize() {
- parent::initialize();
- // Load model yang akan digunakan
- $this->Accounts = $this->loadModel('Accounts');
- $this->Members = $this->loadModel('Members');
- $this->Idols = $this->loadModel('Idols');
- }
- public function buildOptionParser(ConsoleOptionParser $parser) {
- // Argumen/variabel yang harus diisi saat pemanggilan command
- // main_account adalah username, tanpa tanda @, utama, mis. miyazghayda
- // idol_account adalah username idola, mis. _febrian
- $parser->addArguments([
- 'main_account' => ['help' => 'Username akun utama, tanpa @, contohnya miyazghayda', 'required' => true],
- 'idol_account' => ['help' => 'Username akun idola, tanpa @, contohnya _febrian', 'required' => true]
- ]);
- return $parser;
- }
- public function execute(Arguments $args, ConsoleIo $io) {
- $main_account = $args->getArguments()[0];
- $idol_account = $args->getArguments()[1];
- // Uji apakah username terdaftar
- $main_account_check = $this->checkMainAccount($main_account);
- if ($main_account_check[0] == false) {
- $io->out($main_account_check[1]);
- return false;
- }
- // Lihat dan simpan username idol
- $idol_account_check = $this->checkIdolAccount($idol_account, $main_account_check[1]['username'], $main_account_check[1]['password']);
- if ($idol_account_check[0] == false) {
- $io->out($main_account_check[1]);
- return false;
- }
- // Lihat dan simpan pada tabel idols
- $query = $this->Idols->find()
- ->where([
- 'account_id' => $main_account_check[1]['id'],
- 'member_id' => $idol_account_check[1]['id'],
- 'active' => 1
- ]);
- $count = $query->count();
- if ($count < 1) {
- $new_data = $this->Idols->newEntity();
- $new_data->account_id = $main_account_check[1]['id'];
- $new_data->member_id = $idol_account_check[1]['id'];
- $new_data->active = 1;
- if ($this->Idols->save($new_data)) {
- $io->out("Berhasil menambahkan " . $idol_account_check[1]['username'] . " sebagai akun idola " . $main_account_check[1]['username']);
- }
- }
- }
- private function checkIdolAccount($idol_username, $main_account_username, $main_account_password) {
- $io = new ConsoleIo;
- $ret = false;
- $message = 'Akun idola tidak terdaftar di Instagram';
- // Uji apakah username telah ada dalam tabel accounts
- $query_user = $this->Accounts->find()
- ->where([
- 'username' => $idol_username,
- 'active' => 1
- ]);
- $count_user = $query_user->count();
- // Jika telah ada dalam tabel accounts, isi variabel message dengan id akun
- if ($count_user > 0) {
- $user = $query_user->first();
- $ret = true;
- $message = $user->id;
- // Jika belum ada dalam tabel accounts, ambil dari IG
- } else {
- $new_account = [];
- $ig = new Instagram(false, false);
- // Login akun IG
- $io->out("Login pada akun IG {$main_account_username} dengan password {$main_account_password}");
- try {
- $ig->login($main_account_username, $main_account_password);
- } catch(Exception $e) {
- $message = $e->getMessage();
- }
- // Ambil informasi akun idola dari IG
- try {
- $idol_ig = $ig->people->getInfoByName($idol_username);
- $new_account = [
- 'pk' => $idol_ig->getUser()->getPk(),
- 'username' => $idol_ig->getUser()->getUsername(),
- 'fullname' => $idol_ig->getUser()->getFull_name()
- ];
- } catch(Exception $e) {
- $message = $e->getMessage();
- }
- if (!empty($new_account)) {
- $new_data = $this->Accounts->newEntity();
- $new_data->pk = $new_account['pk'];
- $new_data->username = $new_account['username'];
- $new_data->fullname = $new_account['fullname'];
- if ($this->Accounts->save($new_data)) {
- $ret = true;
- $message = [
- 'id' => $new_data->id,
- 'username' => $new_data->username,
- 'password' => $new_data->password
- ];
- }
- }
- }
- return [$ret, $message];
- }
- private function checkMainAccount($username) {
- $ret = false;
- $message = 'Akun utama tidak terdaftar';
- $query_user = $this->Accounts->find()
- ->where([
- 'username' => $username,
- 'principal' => 1,
- 'active' => 1]
- );
- $count_user = $query_user->count();
- if ($count_user == 1) {
- $user = $query_user->first();
- $ret = true;
- // variabel message diisi dengan info akun utama
- $message = [
- 'id' => $user->id,
- 'username' => $user->username,
- 'password' => $user->password
- ];
- }
- return [$ret, $message];
- }
- }
Add Comment
Please, Sign In to add comment