<commit_before># -*- encoding: utf-8 -*-
from __future__ import unicode_literals, print_function

import requests
import urllib2
from xml.etree import ElementTree
from pyquery import PyQuery as pq

from django.core.management.base import BaseCommand
from django.core.files import File
from django.core.files.temp import NamedTemporaryFile

from core.models import Story


class Command(BaseCommand):
    help = 'Fetch Django Girls stories from our blog'

    def handle(self, *args, **options):

        rss_url = 'http://blog.djangogirls.org/rss'

        response = requests.get(rss_url)
        rss = ElementTree.fromstring(response.content)

        for post in rss.iter('item'):
            title = post.find('title').text
            if 'Your Django Story: Meet' in title:
                name = title.replace('Your Django Story: Meet ', '')

                if not Story.objects.filter(name=name).exists():
                    post_url = post.find('link').text
                    post = pq(post.find('description').text)
                    image_url = post('img').attr.src

                    if image_url:
                        story = Story(name=name, post_url=post_url)

                        img = NamedTemporaryFile(delete=True)
                        img.write(urllib2.urlopen(image_url).read())
                        img.flush()

                        story.image.save(image_url.split('/')[-1], File(img))
                        story.save()

                        print('Story of %s has been fetched' % name)<commit_msg>Removed Python 2 dependencies from fetch stories management command<commit_after># -*- encoding: utf-8 -*-
from __future__ import unicode_literals, print_function

import requests
try:
    from urllib.request import urlopen
except ImportError:
    from urllib import urlopen
from xml.etree import ElementTree
from pyquery import PyQuery as pq

from django.core.management.base import BaseCommand
from django.core.files import File
from django.core.files.temp import NamedTemporaryFile

from core.models import Story


class Command(BaseCommand):
    help = 'Fetch Django Girls stories from our blog'

    def handle(self, *args, **options):

        rss_url = 'http://blog.djangogirls.org/rss'

        response = requests.get(rss_url)
        rss = ElementTree.fromstring(response.content)

        for post in rss.iter('item'):
            title = post.find('title').text
            if 'Your Django Story: Meet' in title:
                name = title.replace('Your Django Story: Meet ', '')

                if not Story.objects.filter(name=name).exists():
                    post_url = post.find('link').text
                    post = pq(post.find('description').text)
                    image_url = post('img').attr.src

                    if image_url:
                        story = Story(name=name, post_url=post_url)

                        img = NamedTemporaryFile(delete=True)
                        img.write(urlopen(image_url).read())
                        img.flush()

                        story.image.save(image_url.split('/')[-1], File(img))
                        story.save()

                        print('Story of %s has been fetched' % name)
{'fraction_non_alphanumeric': 0.10105393676379418, 'fraction_numerical': 0.0021698698078115313, 'mean_word_length': 2.0102611940298507, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '119772', 'n_tokens_mistral': 894, 'n_tokens_neox': 829, 'n_words': 224}