Commit 2888929c authored by Luis García's avatar Luis García
Browse files

Twitter mining

parent a371643f
......@@ -29,6 +29,7 @@ def TwitterMining(consultation):
query = "%23"+consultation.slug
query = '#{slug1} #{slug2} #{slug2}'.format(slug1=consultation.phase.slug, slug2=consultation.slug, slug3=question.question.spatial_data.all()[0].slug)
results = api.search(q=query, result_type='recent')
print (results)
for tweet in results:
location = False
if tweet.coordinates:
......@@ -38,5 +39,27 @@ def TwitterMining(consultation):
box = tweet.place.bounding_box
location = Polygon.from_bbox((box.origin()[0], box.origin()[1], box.corner()[0], box.corner()[1])).centroid
if location:
input = SocialInput(description='')
input.save()
\ No newline at end of file
description = tweet.text
descriptionTB = TextBlob(tweet.text)
#Try to translate
try:
descriptionTB = descriptionTB.translate(to='en')
except:
pass
polarity = descriptionTB.sentiment.polarity
media_url = []
#Get the url for the images
if 'media' in tweet.entities.keys():
for media in tweet.entities['media']:
if media['type'] == 'photo':
media_url.append(media['media_url'])
if len(media_url):
#Download the image
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
r = requests.get(media_url[0])
file_dir = BASE_DIR + "/media/social_media_photos/twitter/" + datetime.now().strftime("%b-%d-%Y-%H-%M-%S.%f") + ".jpg"
with open(file_dir, "wb+") as image:
image.write(r.content)
SocialInput.objects.create(description=description, username=tweet.user.screen_name, survey=survey, network='twitter', polarity=polarity, created=tweet.created_at, geometry=location, question=question, photo=File(image))
else:
SocialInput.objects.create(description=description, username=tweet.user.screen_name, survey=survey, network='twitter', polarity=polarity, created=tweet.created_at, geometry=location, question=question)
\ No newline at end of file
......@@ -79,7 +79,7 @@ class ConsultationViewSet(DestroyToInvisibleMixin, OwnerFieldMixin, ModelViewSet
request.POST._mutable = mutable
return super(ConsultationViewSet, self).create(request, *args, **kwargs)
@action(detail=True, methods=['post'], permission_classes=[IsSuperOrReadOnly])
@action(detail=True, methods=['get'], permission_classes=[IsSuperOrReadOnly])
def finish_consultation(self, request, pk=None):
#Revisar esta parte
status = StatusMethod('project_consultation').last()
......
......@@ -22,4 +22,6 @@ instaloader==4.2.9
drf-yasg==1.17.0
drf-extensions==0.5.0
django-cors-headers==3.1.1
django-allauth==0.41.0
\ No newline at end of file
django-allauth==0.41.0
django-rest-auth==0.9.5
django-extra-fields==2.0.3
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment