Guest User

Untitled

a guest
May 27th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. <?php
  2. namespace App\Command;
  3.  
  4. use Cake\Console\Arguments;
  5. use Cake\Console\Command;
  6. use Cake\Console\ConsoleIo;
  7. use Cake\Console\ConsoleOptionParser;
  8. use Cake\Filesystem\Folder;
  9. use Cake\Filesystem\File;
  10. use InstagramAPI\Instagram;
  11. use InstagramAPI\Signatures;
  12. use InstagramAPI\Exception;
  13.  
  14. class AddidolCommand extends Command {
  15. public $Accounts;
  16. public $Members;
  17. public $Idols;
  18.  
  19. public function initialize() {
  20. parent::initialize();
  21. // Load model yang akan digunakan
  22. $this->Accounts = $this->loadModel('Accounts');
  23. $this->Members = $this->loadModel('Members');
  24. $this->Idols = $this->loadModel('Idols');
  25. }
  26.  
  27. public function buildOptionParser(ConsoleOptionParser $parser) {
  28. // Argumen/variabel yang harus diisi saat pemanggilan command
  29. // main_account adalah username, tanpa tanda @, utama, mis. miyazghayda
  30. // idol_account adalah username idola, mis. _febrian
  31. $parser->addArguments([
  32. 'main_account' => ['help' => 'Username akun utama, tanpa @, contohnya miyazghayda', 'required' => true],
  33. 'idol_account' => ['help' => 'Username akun idola, tanpa @, contohnya _febrian', 'required' => true]
  34. ]);
  35. return $parser;
  36. }
  37.  
  38. public function execute(Arguments $args, ConsoleIo $io) {
  39. $main_account = $args->getArguments()[0];
  40. $idol_account = $args->getArguments()[1];
  41.  
  42. // Uji apakah username terdaftar
  43. $main_account_check = $this->checkMainAccount($main_account);
  44. if ($main_account_check[0] == false) {
  45. $io->out($main_account_check[1]);
  46. return false;
  47. }
  48.  
  49. // Lihat dan simpan username idol
  50. $idol_account_check = $this->checkIdolAccount($idol_account, $main_account_check[1]['username'], $main_account_check[1]['password']);
  51. if ($idol_account_check[0] == false) {
  52. $io->out($main_account_check[1]);
  53. return false;
  54. }
  55.  
  56. // Lihat dan simpan pada tabel idols
  57. $query = $this->Idols->find()
  58. ->where([
  59. 'account_id' => $main_account_check[1]['id'],
  60. 'member_id' => $idol_account_check[1]['id'],
  61. 'active' => 1
  62. ]);
  63. $count = $query->count();
  64. if ($count < 1) {
  65. $new_data = $this->Idols->newEntity();
  66. $new_data->account_id = $main_account_check[1]['id'];
  67. $new_data->member_id = $idol_account_check[1]['id'];
  68. $new_data->active = 1;
  69. if ($this->Idols->save($new_data)) {
  70. $io->out("Berhasil menambahkan " . $idol_account_check[1]['username'] . " sebagai akun idola " . $main_account_check[1]['username']);
  71. }
  72. }
  73. }
  74.  
  75. private function checkIdolAccount($idol_username, $main_account_username, $main_account_password) {
  76. $io = new ConsoleIo;
  77. $ret = false;
  78. $message = 'Akun idola tidak terdaftar di Instagram';
  79.  
  80. // Uji apakah username telah ada dalam tabel accounts
  81. $query_user = $this->Accounts->find()
  82. ->where([
  83. 'username' => $idol_username,
  84. 'active' => 1
  85. ]);
  86. $count_user = $query_user->count();
  87. // Jika telah ada dalam tabel accounts, isi variabel message dengan id akun
  88. if ($count_user > 0) {
  89. $user = $query_user->first();
  90. $ret = true;
  91. $message = $user->id;
  92. // Jika belum ada dalam tabel accounts, ambil dari IG
  93. } else {
  94. $new_account = [];
  95. $ig = new Instagram(false, false);
  96. // Login akun IG
  97. $io->out("Login pada akun IG {$main_account_username} dengan password {$main_account_password}");
  98. try {
  99. $ig->login($main_account_username, $main_account_password);
  100. } catch(Exception $e) {
  101. $message = $e->getMessage();
  102. }
  103. // Ambil informasi akun idola dari IG
  104. try {
  105. $idol_ig = $ig->people->getInfoByName($idol_username);
  106. $new_account = [
  107. 'pk' => $idol_ig->getUser()->getPk(),
  108. 'username' => $idol_ig->getUser()->getUsername(),
  109. 'fullname' => $idol_ig->getUser()->getFull_name()
  110. ];
  111. } catch(Exception $e) {
  112. $message = $e->getMessage();
  113. }
  114.  
  115. if (!empty($new_account)) {
  116. $new_data = $this->Accounts->newEntity();
  117. $new_data->pk = $new_account['pk'];
  118. $new_data->username = $new_account['username'];
  119. $new_data->fullname = $new_account['fullname'];
  120. if ($this->Accounts->save($new_data)) {
  121. $ret = true;
  122. $message = [
  123. 'id' => $new_data->id,
  124. 'username' => $new_data->username,
  125. 'password' => $new_data->password
  126. ];
  127. }
  128. }
  129. }
  130. return [$ret, $message];
  131. }
  132.  
  133. private function checkMainAccount($username) {
  134. $ret = false;
  135. $message = 'Akun utama tidak terdaftar';
  136.  
  137. $query_user = $this->Accounts->find()
  138. ->where([
  139. 'username' => $username,
  140. 'principal' => 1,
  141. 'active' => 1]
  142. );
  143. $count_user = $query_user->count();
  144. if ($count_user == 1) {
  145. $user = $query_user->first();
  146. $ret = true;
  147. // variabel message diisi dengan info akun utama
  148. $message = [
  149. 'id' => $user->id,
  150. 'username' => $user->username,
  151. 'password' => $user->password
  152. ];
  153. }
  154. return [$ret, $message];
  155. }
  156. }
Add Comment
Please, Sign In to add comment