Skip to content

Commit

Permalink
[#71] added party option and extra checks to make dupe patching more …
Browse files Browse the repository at this point in the history
…efficient
  • Loading branch information
j-h-s committed Jan 16, 2018
1 parent d9327db commit ec92a4b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
50 changes: 36 additions & 14 deletions src/AppBundle/Command/PatchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ protected function configure() {
->addOption('exturls', 'u', InputOption::VALUE_NONE, "Decode Facebook's external image urls")
->addOption('duplicates', 'd', InputOption::VALUE_NONE, "Scan the social media database for duplicate entries")
->addOption('metadata', 'm', InputOption::VALUE_NONE, "Convert metadata 'value' field to utf8mb4")
->addOption('party', 'y', InputOption::VALUE_OPTIONAL, "Choose a single party to patch, by code")
;
}

Expand All @@ -35,6 +36,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$this->logger = $this->getContainer()->get('logger');
$logger = $this->logger;

$partyCode = $input->getOption('party');

switch (true) { // add more options here
case $input->getOption('twitter'):
$output->writeln("##### Patching Twitter images #####");
Expand All @@ -60,7 +63,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
break;
case $input->getOption('duplicates');
$output->writeln('##### Patching duplicate social media posts #####');
$this->patchDuplicateEntries();
$this->patchDuplicateEntries($partyCode);
break;
case $input->getOption('metadata');
$output->writeln('##### Patching metadata charset #####');
Expand Down Expand Up @@ -93,28 +96,47 @@ public function getConfirmation() {
// This patch locates those posts in the database and deletes the duplicates, leaving only the most recent copy.
// Only run this if you know there are duplicate entries. It takes too long to waste time running it needlessly.
/////
public function patchDuplicateEntries() {
public function patchDuplicateEntries($partyCode = null) {
$this->getConfirmation();
$time = new \DateTime('now');
echo "# NOTE: This will take a long time. Go and make yourself a cup of tea. The time is now ".$time->format('H:i:s').".\n";
echo " Checking database... ";
$posts = $this->em->getRepository('AppBundle:SocialMedia')->findAll();
echo "patching...";

if (is_null($partyCode)) {
$posts = $this->em->getRepository('AppBundle:SocialMedia')->findAll();
echo "patching...";
} else {
echo "patching " . $partyCode . "...";
$posts = $this->em->getRepository('AppBundle:SocialMedia')->findBy(['code' => $partyCode], ['id' => 'DESC']);
}

foreach ($posts as $prime) {
$dupes = $this->em->getRepository('AppBundle:SocialMedia')->findBy([
'postId' => $prime->getPostId(),
'postText' => $prime->getPostText(),
'postImage' => $prime->getPostImage(),
]);
if (!is_null($partyCode)) {
$terms['code'] = $partyCode;
}

$terms['type'] = $prime->getType();
$terms['subType'] = $prime->getSubType();
$terms['postId'] = $prime->getPostId();
$terms['postText'] = $prime->getPostText();
$terms['postImage'] = $prime->getPostImage();

$dupes = $this->em->getRepository('AppBundle:SocialMedia')->findBy($terms, ['id' => 'DESC']);

foreach ($dupes as $dupe) {
if ($dupe->getId() < $prime->getId()) {
echo " duplicate found, deleting...";
// echo "\nprime id = ".$prime->getId().", dupe id = ".$dupe->getId();
// echo ", prime post id = ".$prime->getPostId().", dupe post id = ".$dupe->getPostId();
// echo ", prime text = ".$prime->getPostText().", dupe text = ".$dupe->getPostText();
// echo ", prime image = ".$prime->getPostImage().", dupe image = ".$dupe->getPostImage();
echo " duplicate found...";
// echo "\n prime type = " . $prime->getType() . "-" . $prime->getSubType();
// echo ", id = " . $prime->getId() . ", post id = " . $prime->getPostId();
// echo ", time = " . $prime->getPostTime()->format('Y-m-d H:i:s');
// echo "\n dupe type = " . $dupe->getType() . "-" . $dupe->getSubType();
// echo ", id = " . $dupe->getId() . ", post id = " . $dupe->getPostId();
// echo ", time = " . $dupe->getPostTime()->format('Y-m-d H:i:s');
// echo "\n prime text = " . $prime->getPostText();
// echo "\n dupe text = " . $dupe->getPostText();
// echo "\n prime image = " . $prime->getPostImage();
// echo "\n dupe image = " . $dupe->getPostImage() . "\n";
echo " deleting...";
$this->em->remove($dupe);
$this->em->flush();
echo " done...";
Expand Down
12 changes: 6 additions & 6 deletions src/AppBundle/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,21 @@ public function getSelectSocial($socialMedia, $fields) {

if ($field == 'time') {
if ($temp['sub_type'] != 'E') {
$temp['post_'.$field] = isset($data['posted']) ? $data['posted'] : null;
$temp['post_' . $field] = isset($data['posted']) ? $data['posted'] : null;

} else $temp['post_'.$field] = isset($data['start_time']) ? $data['start_time'] : null;
} else $temp['post_' . $field] = isset($data['start_time']) ? $data['start_time'] : null;

} else if ($field == 'shares' && $temp['type'] == 'tw') {
$temp['post_'.$field] = isset($data['retweets']) ? $data['retweets'] : null;
$temp['post_' . $field] = isset($data['retweets']) ? $data['retweets'] : null;

} else if ($field == 'engagement') {
$temp['post_'.$field] = $this->getPostEngagement($social);
$temp['post_' . $field] = $this->getPostEngagement($social);

} else if ($field == 'reach') {
$temp['post_'.$field] = $this->getPostReach($social);
$temp['post_' . $field] = $this->getPostReach($social);

} else {
$temp['post_'.$field] = isset($data[$field]) ? $data[$field] : null;
$temp['post_' . $field] = isset($data[$field]) ? $data[$field] : null;
}
}

Expand Down

0 comments on commit ec92a4b

Please sign in to comment.