/** * @param Emote $emote * @return string */ protected function generateEmoteEntry(Emote $emote) { $entries = ''; /** @var TextCode $textCode */ foreach ($emote->getTextCodes() as $textCode) { $entries .= sprintf("%s %s\n", $emote->getPath(), $textCode->getCode()); } return $entries; }
protected function execute(InputInterface $input, OutputInterface $output) { $logger = $this->getLoggerCallback($output); /** @var SAEmoteCrawler $crawler */ $crawler = $this->getContainer()->get('gbslogistics.emotes.emote_bundle.emote_client'); /** @var EntityRepository $repository */ $repository = $this->getContainer()->get('doctrine.orm.entity_manager')->getRepository(TextCode::class); $logger('Crawling forums.somethingawful.com now.'); $saEmotes = $crawler->crawl(); $logger(sprintf('Found %s emote(s).', count($saEmotes))); $existingEmotes = $repository->findAll(); $logger(sprintf('Found %s existing emote codes.', count($existingEmotes))); /** @var RemoteEmote[] $saEmoteDictionary */ $saEmoteDictionary = array_reduce($saEmotes, function ($result, RemoteEmote $item) { $result[$item->getName()] = $item; return $result; }, []); $existingEmotes = array_map(function (TextCode $item) { return $item->getCode(); }, $existingEmotes); $newCodes = array_diff(array_keys($saEmoteDictionary), $existingEmotes); $logger(sprintf('Found %s new code(s): %s', count($newCodes), join(', ', $newCodes))); $returnCode = 1; if (count($newCodes) > 0) { $dataStorage = $this->getContainer()->get('gbslogistics.emotes.emote_bundle.data_storage'); $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager'); $targetDirectory = $dataStorage->getSourceDirectory(); $history = new History(); foreach ($newCodes as $key) { $remoteEmote = $saEmoteDictionary[$key]; $imageUrl = $remoteEmote->getImageUrl(); // Retrieve emote image $path = parse_url($imageUrl, PHP_URL_PATH); $filename = basename($path); $destinationPath = $targetDirectory . DIRECTORY_SEPARATOR . $filename; $logger("Downloading {$imageUrl} to {$destinationPath}"); file_put_contents($destinationPath, file_get_contents($imageUrl)); // Store metadata $emote = new Emote(); $textCode = new TextCode($emote, $remoteEmote->getName()); $emote->setPath($filename); $history->addEmote($emote); $entityManager->persist($emote); $entityManager->persist($textCode); } $entityManager->persist($history); $entityManager->flush(); $returnCode = 0; } return $returnCode; }
/** * @param string $directoryPath The path to the directory containing all of * the emote images and the pidgin "theme" file. */ public function populateDatabase($directoryPath) { $themeFile = $directoryPath . DIRECTORY_SEPARATOR . 'theme'; if (!is_file($themeFile)) { throw new \InvalidArgumentException(sprintf('Could not locate "%s".', $themeFile)); } if (false === ($themeHandle = fopen($themeFile, 'r'))) { throw new \InvalidArgumentException(sprintf('Could not open "%s".', $themeFile)); } // Seek through the theme until we find where the emotes are located. while (false !== ($line = fgets($themeHandle))) { $line = trim($line); if ('[default]' === $line) { break; } } if (feof($themeHandle)) { throw new \RuntimeException(sprintf('Could not find emote information in "%s".', $themeFile)); } while (false !== ($line = fgets($themeHandle))) { $line = trim($line); if (preg_match('/(?:! )?(\\S+)\\s+(.+)/', $line, $matches)) { $file = $matches[1]; $emoteCodes = preg_split('/\\s+/', $matches[2]); $sourcePath = $directoryPath . DIRECTORY_SEPARATOR . $file; $destinationPath = $this->dataStorage->getImageSourcePath($file); $emote = $this->emoteRepository->findOneBy(['path' => $file]); if (null === $emote) { $emote = new Emote(); $emote->setPath($file); if (null === $destinationPath) { $this->dataStorage->copyImageToSource($sourcePath); } $this->entityManager->persist($emote); $this->entityManager->flush($emote); } $newCodes = array_diff($emoteCodes, $emote->getTextCodesAsArray()); foreach ($newCodes as $code) { $textCode = new TextCode($emote, $code); $this->entityManager->persist($textCode); $this->entityManager->flush($textCode); } } } $this->entityManager->flush(); }
protected function getPlistEntry(Emote $emote) { $equivalents = ''; foreach ($emote->getTextCodes() as $textCode) { $equivalents .= '<string>' . $textCode->getCode() . '</string>'; } return sprintf(<<<PLISTENTRY <key>%s</key> <dict> <key>Equivalents</key> <array>%s</array> <key>Name</key> <string>%s</string> </dict> PLISTENTRY , $emote->getPath(), $equivalents, $emote->getPath()); }