Skip to content

Commit

Permalink
Proof of concept for Freebase db link-up #42
Browse files Browse the repository at this point in the history
  • Loading branch information
hoyes committed Mar 28, 2013
1 parent 002d720 commit be08977
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 1 deletion.
28 changes: 28 additions & 0 deletions app/DoctrineMigrations/Version20130328022931.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Application\Migrations;

use Doctrine\DBAL\Migrations\AbstractMigration,
Doctrine\DBAL\Schema\Schema;

/**
* Auto-generated Migration: Please modify to your need!
*/
class Version20130328022931 extends AbstractMigration
{
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql", "Migration can only be executed safely on 'mysql'.");

$this->addSql("ALTER TABLE acts_shows ADD freebase_id VARCHAR(255)");
}

public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql", "Migration can only be executed safely on 'mysql'.");

$this->addSql("ALTER TABLE acts_shows DROP freebase_id");
}
}
52 changes: 52 additions & 0 deletions src/Acts/CamdramBackendBundle/Command/ShowsFreebaseCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
namespace Acts\CamdramBackendBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use Acts\CamdramBundle\Entity\Show;
use Doctrine\ORM\EntityRepository;

class ShowsFreebaseCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('camdram:shows:freebase')
->setDescription('Map shows to freebase plays')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{

$em = $this->getContainer()->get('doctrine.orm.entity_manager');
/** @var $repo EntityRepository */
$repo = $em->getRepository('ActsCamdramBundle:Show');

$shows = $repo->findBy(array('freebase_id' => null));
$social = $this->getContainer()->get('acts.social_api.provider');
$api = $social->get('google_simple');

$i = 0;

foreach ($shows as $show) {
$results = $api->doFreebaseSearch($show->getName(), '(any type:/theater/play type:/theater/opera)', 2);
if (count($results) > 1 && ($results[0]['score'] > 150 || $results[0]['name'] == $show->getName())) {
$show->setFreebaseId($results[0]['mid']);
$output->writeln('Mapped show "'.$show->getName().'" to Freebase play "'
.$results[0]['name'].'" ('.$results[0]['mid'].')');

}
if ($i % 30 == 0) {
$em->flush();
}
$i++;
}
$em->flush();
}

}
25 changes: 25 additions & 0 deletions src/Acts/CamdramBundle/Controller/PlayController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Acts\CamdramBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Acts\CamdramBundle\Entity\Play;

class PlayController extends Controller
{

public function displayAction($mid)
{
$social = $this->get('acts.social_api.provider');
$api = $social->get('google_simple');

$topic = $api->doFreebaseTopic($mid);
$play = new Play($topic);

$shows = $this->getDoctrine()->getRepository('ActsCamdramBundle:Show')->findBy(array('freebase_id' => $topic['id']));

return $this->render('ActsCamdramBundle:Play:display.html.twig', array('play' => $play, 'shows' => $shows));
}

}
88 changes: 88 additions & 0 deletions src/Acts/CamdramBundle/Entity/Play.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Acts\CamdramBundle\Entity;

/**
* Play (or other theatrical work)
*
*/
class Play
{
private $data;

public function __construct($data)
{
$this->data = $data;
}

public function getMid()
{
return $this->data['id'];
}

protected function getValue($key, $array = false)
{
if ($array) {
return $this->data['property'][$key]['values'];
}
else {
return $this->data['property'][$key]['values'][0];
}
}

public function getName()
{
return $this->getValue('/type/object/name');
}

public function getDescription()
{
return $this->getValue('/common/topic/description');
}

public function getAuthor()
{
return $this->getValue('/book/written_work/author', true);
}

public function getPlaywright()
{
return $this->getValue('/theater/play/playwright', true);
}

public function getComposer()
{
return $this->getValue('/theater/play/composer', true);
}

public function getLyricist()
{
return $this->getValue('/theater/play/lyricist', true);
}

public function getOrchestrator()
{
return $this->getValue('/theater/play/orchestrator', true);
}

public function getCharacters()
{
return $this->getValue('/theater/play/characters', true);
}

public function getSoundtracks()
{
return $this->getValue('/theater/play/soundtracks', true);
}

public function getProductions()
{
return $this->getValue('/theater/play/productions', true);
}

public function getUrl()
{
return 'https://www.freebase.com/'.$this->getMid();
}

}
30 changes: 30 additions & 0 deletions src/Acts/CamdramBundle/Entity/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ class Show extends Entity
*/
private $end_at;

/**
* @var string
*
* @ORM\Column(name="freebase_id", type="string", nullable=true)
*/
private $freebase_id;

protected $entity_type = 'show';

private $multi_venue;
Expand Down Expand Up @@ -944,4 +951,27 @@ public function getRank()

}


/**
* Set freebase_id
*
* @param string $freebaseId
* @return Show
*/
public function setFreebaseId($freebaseId)
{
$this->freebase_id = $freebaseId;

return $this;
}

/**
* Get freebase_id
*
* @return string
*/
public function getFreebaseId()
{
return $this->freebase_id;
}
}
7 changes: 6 additions & 1 deletion src/Acts/CamdramBundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ acts_camdram_settings:
defaults: { _controller: ActsCamdramBundle:Account:settings }
acts_camdram_homepage:
pattern: /
defaults: { _controller: ActsCamdramBundle:Default:index }
defaults: { _controller: ActsCamdramBundle:Default:index }
acts_camdram_play:
pattern: /plays/{mid}
defaults: { _controller: ActsCamdramBundle:Play:display }
requirements:
mid: .*
63 changes: 63 additions & 0 deletions src/Acts/CamdramBundle/Resources/views/Play/display.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{% extends 'ActsCamdramBundle::layout.html.twig' %}

{% block title %}Play: {{ play.name.text }}{% endblock %}

{% block body %}

<div class="row">
<div class="twelve columns">

<h2>{{ play.name.text }}</h2>

{% if play.playwright | length > 0 %}
<h5>Author</h5>
<p>{% for playwright in play.playwright %}{{ playwright.text }}<br/>{% endfor %}</p>
{% endif %}
{% if play.composer | length > 0 %}
<h5>Composer</h5>
<p>{% for composer in play.composer %}{{ composer.text }}<br/>{% endfor %}</p>
{% endif %}
{% if play.lyricist | length > 0 %}
<h5>Lyricist</h5>
<p>{% for lyricist in play.lyricist %}{{ lyricist.text }}<br/>{% endfor %}</p>
{% endif %}
{% if play.orchestrator | length > 0 %}
<h5>Orchestrator</h5>
<p>{% for orchestrator in play.orchestrator %}{{ orchestrator.text }}<br/>{% endfor %}</p>
{% endif %}

<p>{{ play.description.value }}</p>

<h3>Characters</h3>
<ul>
{% for character in play.characters %}
<li>{{ character.text }}</li>
{% endfor %}
</ul>

{% if play.soundtracks | length > 0 %}
<h3>Cast Recordings</h3>
<ul>
{% for soundtrack in play.soundtracks %}
<li><a href="https://www.freebase.com{{ soundtrack.id }}">{{ soundtrack.text }}</a></li>
{% endfor %}
</ul>
{% endif %}

{% if shows | length > 0 %}
<h3>Performances</h3>
<ul class="block-grid two-up mobile">
{% for show in shows %}
<li>
{% if show.society %}<a href="{{ path('get_society', {identifier: show.society}) }}">{{ show.society.name }}</a>{% endif %}
<h5><a href="{{ path('get_show', {identifier: show.slug}) }}">{{ show.name }}</a></h5>
<p><em>{% include 'ActsCamdramBundle:Performance:show.html.twig' with {'performances': show.performances } %}</em></p>
{{ show.description | truncate(200) | camdram_markdown }}
</li>
{% endfor %}
</ul>
{% endif %}
</div>
</div>

{% endblock %}
11 changes: 11 additions & 0 deletions src/Acts/SocialApiBundle/Resources/config/defaults.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ google_simple:
arguments: [keyword, location, radius]
defaults: {sensor: 'false'}
response: {root: 'results'}
freebase_reconcile:
path: https://www.googleapis.com/freebase/v1/reconcile
arguments: [name, kind, prop, limit, confidence]
freebase_search:
path: https://www.googleapis.com/freebase/v1/search
arguments: [query, filter, limit]
response: {root: 'result'}
freebase_topic:
path: https://www.googleapis.com/freebase/v1/topic/{mid}
url_has_params: true
arguments: [mid]

github:
class: oauth2
Expand Down

0 comments on commit be08977

Please sign in to comment.