-
Notifications
You must be signed in to change notification settings - Fork 662
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
252 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
0 22 * * 1,3 cd /root/serenata-de-amor && docker-compose -f docker-compose.yml -f docker-compose.prod.yml run --rm django python manage.py tweet >/dev/null 2>&1 | ||
0 17 * * 2,4 cd /root/serenata-de-amor && docker-compose -f docker-compose.yml -f docker-compose.prod.yml run --rm django python manage.py tweet >/dev/null 2>&1 | ||
0 13 * * 5 cd /root/serenata-de-amor && docker-compose -f docker-compose.yml -f docker-compose.prod.yml run --rm django python manage.py tweet >/dev/null 2>&1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
jarbas/chamber_of_deputies/management/commands/socialmedia.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import csv | ||
import os | ||
|
||
from jarbas.core.management.commands import LoadCommand | ||
from jarbas.chamber_of_deputies.models import SocialMedia | ||
|
||
|
||
class Command(LoadCommand): | ||
help = 'Load congresspeople social media accounts' | ||
count = 0 | ||
|
||
def handle(self, *args, **options): | ||
self.path = options['dataset'] | ||
if not os.path.exists(self.path): | ||
raise FileNotFoundError(os.path.abspath(self.path)) | ||
|
||
if options.get('drop', False): | ||
self.drop_all(SocialMedia) | ||
|
||
print('Saving social media accounts') | ||
with open(self.path) as fobj: | ||
bulk = (SocialMedia(**line) for line in csv.DictReader(fobj)) | ||
SocialMedia.objects.bulk_create(bulk) | ||
print('Done!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from django.core.management.base import BaseCommand | ||
|
||
from jarbas.chamber_of_deputies.twitter import SocialMedia, Twitter | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Tweet the next suspicion at @RosieDaSerenata' | ||
|
||
def handle(self, *args, **options): | ||
congressperson_ids_with_twitter = SocialMedia.objects. \ | ||
values_list('congressperson_id', flat=True) \ | ||
.distinct('congressperson_id') \ | ||
.order_by('congressperson_id') | ||
|
||
twitter = Twitter(congressperson_ids_with_twitter) | ||
if twitter.reimbursement: | ||
twitter.publish() |
31 changes: 31 additions & 0 deletions
31
jarbas/chamber_of_deputies/migrations/0005_create_social_media_model.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Generated by Django 2.0.8 on 2018-09-13 03:25 | ||
|
||
import django.contrib.postgres.fields | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('chamber_of_deputies', '0004_alter_field_names_following_toolbox_renamings'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='SocialMedia', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('congressperson_name', models.CharField(blank=True, default='', max_length=255)), | ||
('congressperson_id', models.IntegerField(blank=True, db_index=True, null=True)), | ||
('twitter_profile', models.CharField(blank=True, default='', max_length=255)), | ||
('secondary_twitter_profile', models.CharField(blank=True, default='', max_length=255)), | ||
('facebook_page', models.CharField(blank=True, default='', max_length=255)), | ||
], | ||
), | ||
migrations.AddField( | ||
model_name='reimbursement', | ||
name='social_media', | ||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='chamber_of_deputies.SocialMedia'), | ||
), | ||
] |
20 changes: 20 additions & 0 deletions
20
jarbas/chamber_of_deputies/migrations/0006_change_on_delete_social_media_to_set_null.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Generated by Django 2.0.8 on 2018-09-13 04:42 | ||
|
||
import django.contrib.postgres.fields | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('chamber_of_deputies', '0005_create_social_media_model'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='reimbursement', | ||
name='social_media', | ||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='chamber_of_deputies.SocialMedia'), | ||
) | ||
] |
34 changes: 34 additions & 0 deletions
34
jarbas/chamber_of_deputies/migrations/0007_auto_20180913_0209.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Generated by Django 2.0.8 on 2018-09-13 05:09 | ||
|
||
import django.contrib.postgres.fields | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('chamber_of_deputies', '0006_change_on_delete_social_media_to_set_null'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='reimbursement', | ||
name='document_id', | ||
field=models.IntegerField(db_index=True, verbose_name='Número do Reembolso'), | ||
), | ||
migrations.AlterField( | ||
model_name='reimbursement', | ||
name='issue_date', | ||
field=models.DateField(blank=True, db_index=True, null=True, verbose_name='Data de Emissão'), | ||
), | ||
migrations.AlterField( | ||
model_name='reimbursement', | ||
name='numbers', | ||
field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=128, verbose_name='Números dos Ressarcimentos'), default=list, size=None), | ||
), | ||
migrations.AlterField( | ||
model_name='reimbursement', | ||
name='supplier', | ||
field=models.CharField(max_length=256, verbose_name='Fornecedor'), | ||
), | ||
] |
22 changes: 22 additions & 0 deletions
22
jarbas/chamber_of_deputies/migrations/0008_remove_related_field_social_media.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Generated by Django 2.0.8 on 2018-09-13 05:46 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('chamber_of_deputies', '0007_auto_20180913_0209'), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name='reimbursement', | ||
name='social_media', | ||
), | ||
migrations.AlterField( | ||
model_name='reimbursement', | ||
name='congressperson_id', | ||
field=models.IntegerField(blank=True, db_index=True, null=True, verbose_name='Identificador Único do Parlamentar'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import twitter | ||
|
||
from django.conf import settings | ||
from django.core.exceptions import ObjectDoesNotExist | ||
|
||
from jarbas.chamber_of_deputies.models import Reimbursement, SocialMedia, Tweet | ||
|
||
|
||
class Twitter: | ||
"""Twitter target. Maintains the logic for posting suspicious | ||
reimbursements in a Twitter account. | ||
""" | ||
|
||
LINK = 'https://jarbas.serenata.ai/layers/#/documentId/{}' | ||
TEXT = ( | ||
'🚨Gasto suspeito de Dep. @{} ({}). ' | ||
'Você pode me ajudar a verificar? ' | ||
'{} #SerenataDeAmor na @CamaraDeputados' | ||
) | ||
|
||
def __init__(self, congressperson_ids_with_twitter): | ||
self.credentials = ( | ||
settings.TWITTER_CONSUMER_KEY, | ||
settings.TWITTER_CONSUMER_SECRET, | ||
settings.TWITTER_ACCESS_TOKEN, | ||
settings.TWITTER_ACCESS_SECRET | ||
) | ||
self.reimbursement = Reimbursement.objects \ | ||
.next_tweet(congressperson_ids_with_twitter) | ||
self._message = '' | ||
|
||
@property | ||
def message(self): | ||
"""Proper tweet message for the given reimbursement.""" | ||
if self._message: | ||
return self._message | ||
|
||
try: | ||
social_media = SocialMedia.objects \ | ||
.get(congressperson_id=self.reimbursement.congressperson_id) | ||
except (ObjectDoesNotExist, SocialMedia.MultipleObjectsReturned): | ||
return None | ||
|
||
profile = social_media.twitter_profile or \ | ||
social_media.secondary_twitter_profile | ||
if not profile: | ||
return None | ||
|
||
self._message = self.TEXT.format( | ||
profile, | ||
self.reimbursement.state, | ||
self.LINK.format(self.reimbursement.document_id) | ||
) | ||
return self.message | ||
|
||
def publish(self): | ||
"""Post the update to Twitter's timeline.""" | ||
if not self.reimbursement: | ||
return None | ||
|
||
api = twitter.Api(*self.credentials) | ||
tweet = api.PostUpdate(self.message) | ||
Tweet.objects.create(reimbursement=self.reimbursement, status=tweet.id) |