# -*- coding: utf-8 -*-
import operator
import datetime
import math

from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse, resolve
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.views.decorators.cache import never_cache
from django.db.models import Q, Max, Min
from django import db
from django.conf import settings
from django.template.defaultfilters import date as tmp_date

from user_registration.func import *
from kinoinfo_folder.func import low, capit, del_separator
from api.models import FilmsName
from api.views import get_film_data, film_poster2
from base.models import *
from base.func import create_kinoafisha_button
from articles.views import pagination as pagi
from movie_online.IR import check_int_rates

def get_bg(request, site=None):
    new_view = 1
    if '/dajaxice/' in request.path or '/user/' in request.path and '/login/' not in request.path:
        new_view = 0

    btype = '2'

    price = get_adv_price(btype)

    advert_img, advert_url, advert_id = ('', '', '')

    now = datetime.datetime.now()

    if request.current_site.domain in ('kinoinfo.ru', 'kinoafisha.ru'):
        filter = {'sites': request.current_site, 'btype': btype, 'user__personinterface__money__gte': price, 'balance__gte': price, 'deleted': False, 'dtime__lt': now}
        if site:
            filter['sites'] = site
        banners = SiteBanners.objects.filter(Q(country__pk=request.current_user_country_id) | Q(country=None), **filter).values('file', 'url', 'id', 'views', 'country', 'cities__pk', 'dtime').order_by('-cities__pk')

        banner = None
        get_country = False
        for i in banners:
            future = i['dtime'].date() + datetime.timedelta(days=13)
            if now.date() <= future:
                # если баннер для моего города
                if i['cities__pk'] == request.current_user_city_id:
                    banner = i
                    break
                # если баннер для моей страны
                elif i['country'] == request.current_user_country_id and not i['cities__pk']:
                    banner = i
                    get_country = True
                elif not get_country and not i['country'] and not i['cities__pk']:
                    banner = i

        if banner:
            advert_img, advert_url = (banner['file'], banner['url'])
            advert_id = banner['id']
            if new_view and not request.bot:
                set_adv_view(request, banner['id'])

    else:
        bg_filter = {'country__id': request.current_user_country_id, 'city__id': request.current_user_city_id, 'site': request.current_site, 'subdomain': request.subdomain}
        bg_alt_filter = {'country__id': request.current_user_country_id, 'city': None, 'site': request.current_site, 'subdomain': request.subdomain}
        bg = Background.objects.filter(Q(**bg_filter) | Q(**bg_alt_filter)).order_by('-date_adding')
        if bg:
            advert_img, advert_url = ('%s%s' % (settings.MEDIA_URL, bg[0].image), bg[0].url)

    return {'img': advert_img, 'url': advert_url, 'id': advert_id}

@never_cache
def search(request):

    query_orig = request.GET.get('query', '')
    category = request.GET.get('filter', '')
    objs = []
    msg = ''
    element = ''
    data = {}
    count = 0

    if query_orig:

        query = low(query_orig)

        slug = del_separator(query)
        if len(query) > 2:

            # фильмы
            if category == '1':
                data = []
                tmp_query = query.decode('utf-8').encode('ascii', 'xmlcharrefreplace')
                element = 'фильмов'
                try:

                    '''
                    Получаем ID фильмов по поисковому запросу
                    upd : проверка ч/з пересечение множеств показала,
                    что такой вариант идентичен предыдущему(конкатенации двух сетов)
                    upd : убрал поиск по слагу, т.к. формирование слага происходит посредством склейки
                    всех слов из тайтла, а это приводит к нерелевантным результатам поиска
                    (см. "San tau jin zi lei saam" фильм)
                    '''

                    ids = set(list(
                        FilmsName.objects.using('afisha').filter(
                            Q(name__icontains = query),
                            status=1
                        ).values_list('film_id', flat=True)))

                    objs = FilmsName \
                        .objects \
                        .using('afisha') \
                        .only(
                            'name',
                            'film_id',
                            'film_id__description',
                            'film_id__year',
                            'film_id__genre1',
                            'film_id__genre2',
                            'film_id__imdb',
                              ) \
                        .select_related('film_id') \
                        .filter(film_id__id__in=ids, status=1, type=2) \
                        .order_by('-film_id__year','name')

                    tmp = None
                    tmp_first = []
                    relevant = []
                    nonrelevant = []

                    for i in objs:

                        '''
                        Получаем постер
                        '''

                        temp_poster = Objxres.objects.using('afisha').select_related('extresid').filter(
                            objtypeid__in=[301, 300],
                            objpkvalue=i.film_id_id)

                        posters = {}
                        for p in temp_poster:
                            if posters.get(p.objpkvalue):
                                if p.objtypeid == 301:
                                    posters[p.objpkvalue]['poster'].append(p)
                                else:
                                    posters[p.objpkvalue]['slides'].append(p)
                            else:
                                posters[p.objpkvalue] = {'poster': [], 'slides': []}
                                if p.objtypeid == 301:
                                    posters[p.objpkvalue]['poster'].append(p)
                                else:
                                    posters[p.objpkvalue]['slides'].append(p)

                        if posters and posters[i.film_id_id]['poster']:
                            i.poster = film_poster2(posters[i.film_id_id]['poster'], 'big')

                        '''
                        Получаем мету
                        '''
                        meta = Film.objects.using('afisha').get(id=i.film_id_id)

                        '''
                        Из полученных метаданных получаем наименования жанров
                        '''
                        i.genres = AfishaGenre.objects.using('afisha').filter(
                            pk__in=[meta.genre1_id, meta.genre2_id, meta.genre3_id])

                        '''
                        Из полученных метаданных получаем рейтинг по IMDB
                        грязнохак - шаблонизатор django не понимает числовых циклов с условиями
                        '''
                        if(meta.imdb):
                            i.imdb = {}
                            imdb_val = meta.imdb
                            i.imdb['range'] = range(
                                0,
                                int(
                                    math.ceil(
                                        float(
                                            imdb_val.replace(',', '.')
                                        )
                                    )
                                )
                                ,1)
                            i.imdb['value'] = meta.imdb

                        '''
                        Получаем рейтинг взвешенный по системе
                        '''
                        int_rate, show_ir, show_imdb, rotten = check_int_rates(i.film_id_id)
                        i.rating = {'rate': int_rate, 'show_ir': show_ir, 'show_imdb': show_imdb, 'rotten': rotten}

                        '''
                        upd: переработка сортировки по релевантности, признак - наличие поисковой фразы в описании
                        '''
                        tmp = i.film_id_id

                        if(tmp not in tmp_first):

                            if(
                                        (meta.description is not None)
                                    and (meta.description.find(tmp_query))
                            ):
                                relevant.append(i)
                            else:
                                nonrelevant.append(i)

                            tmp_first.append(tmp)

                    data = relevant+nonrelevant

                    count = len(tmp_first)

                    if count == 1:
                        return HttpResponseRedirect(reverse('get_film', kwargs={'id': tmp}))

                except db.backend.Database._mysql.OperationalError:
                    pass
            # персоны
            elif category == '2':
                data = {'first': [], 'middle': [], 'last': []}

                element = 'персон'

                tmp_query = query.decode('utf-8').encode('ascii', 'xmlcharrefreplace')

                ids1 = set(list(NamePerson.objects.exclude(person__kid=None).filter(Q(name__iexact=tmp_query) | Q(name__istartswith=slug), status__in=(1,2)).values_list('person__id', flat=True)))

                ids2 = set(list(NamePerson.objects.exclude(person__id__in=ids1, person__kid=None).filter(name__icontains=slug, status=2).values_list('person__id', flat=True)))

                ids = set(list(ids1) + list(ids2))

                objs1 = list(NamePerson.objects.filter(person__id__in=ids, status=1, language__id=1).order_by('name').values('person__id', 'name', 'person__kid'))

                result_ids = set([i['person__id'] for i in objs1])

                objs2 = list(NamePerson.objects.exclude(person__id__in=result_ids).filter(person__id__in=ids, status=1, language__id=2).order_by('name').values('person__id', 'name', 'person__kid'))

                tmp = None

                tmp_first = []

                for objs in [objs1, objs2]:
                    for i in objs:
                        tmp = i['person__kid']
                        if long(i['person__id']) in ids1:
                            if low(i['name'].encode('utf-8')) == tmp_query:
                                if tmp not in tmp_first:
                                    data['first'].append((i['person__kid'], i['name']))
                                    tmp_first.append(tmp)
                            else:
                                if tmp not in tmp_first:
                                    data['middle'].append((i['person__kid'], i['name']))
                                    tmp_first.append(tmp)
                        else:
                            if tmp not in tmp_first:
                                data['last'].append((i['person__kid'], i['name']))
                                tmp_first.append(tmp)

                count = len(tmp_first)

                if count == 1:
                    return HttpResponseRedirect(reverse('get_person', kwargs={'id': tmp}))

            elif category == '3':
                # music
                if request.subdomain == 'music':
                    data = {'first': [], 'middle': [], 'last': [], 'artist': []}

                    element = ''

                    tmp_query = query.decode('utf-8').encode('ascii', 'xmlcharrefreplace')

                    ids1 = set(list(Composition.objects.filter(Q(name__name__iexact=tmp_query) | Q(name__name__istartswith=slug), name__status__in=(2,5)).values_list('id', flat=True)))

                    ids2 = set(list(Composition.objects.exclude(pk__in=ids1).filter(name__name__icontains=slug,  name__status__in=(2,5)).values_list('id', flat=True)))

                    ids = set(list(ids1) + list(ids2))

                    comp_rels = {}
                    for i in CompositionPersonRel.objects.filter(composition__pk__in=ids, type__name='исполнение').values('person', 'person__name__name', 'composition'):
                        comp_rels[i['composition']] = {'pid': i['person'], 'pname': i['person__name__name']}

                    objs1 = list(CompositionName.objects.filter(composition__pk__in=ids, status=2).order_by('name').values('composition__id', 'name'))

                    tmp = None

                    tmp_first = []

                    for i in objs1:
                        tmp = i['composition__id']
                        if long(i['composition__id']) in ids1:
                            if low(i['name'].encode('utf-8')) == query:
                                if tmp not in tmp_first:
                                    artsit = comp_rels.get(i['composition__id'])
                                    data['first'].append((i['composition__id'], i['name'], artsit))
                                    tmp_first.append(tmp)
                            else:
                                if tmp not in tmp_first:
                                    artsit = comp_rels.get(i['composition__id'])
                                    data['middle'].append((i['composition__id'], i['name'], artsit))
                                    tmp_first.append(tmp)
                        else:
                            if tmp not in tmp_first:
                                artsit = comp_rels.get(i['composition__id'])
                                data['last'].append((i['composition__id'], i['name'], artsit))
                                tmp_first.append(tmp)

                    count = len(tmp_first)

                    artists = Person.objects.filter(Q(name__name__iexact=tmp_query) | Q(name__name__istartswith=slug) | Q(name__name__icontains=slug), artist=True, name__status=4).values('id', 'name__name')

                    data['artist'] = artists

                    count += artists.count()
                # Кинотеатры
                else:

                    element = 'кинотеатров'

                    tmp_query = query.decode('utf-8').encode('ascii', 'xmlcharrefreplace')

                    ids1 = set(list(Organization.objects.exclude(kid=None).filter(Q(name__iexact=tmp_query) | Q(name__istartswith=slug)).values_list('id', flat=True)))
                    ids2 = set(list(Organization.objects.exclude(Q(id__in=ids1) | Q(kid=None)).filter(name__icontains=slug).values_list('id', flat=True)))

                    ids = set(list(ids1) + list(ids2))

                    buildings = list(Building.objects.filter(organization__id__in=ids, city__name__status=1).values('city', 'city__name__name', 'city__country', 'city__country__name', 'city__kid', 'organization', 'organization__name', 'organization__uni_slug'))

                    cities = {}
                    count = 0
                    tmp = None
                    for i in buildings:
                        count += 1
                        tmp = i['organization__uni_slug']
                        if not cities.get(i['city']):
                            cities[i['city']] = {
                                'id': i['city'],
                                'name': i['city__name__name'],
                                'country': i['city__country__name'],
                                'cinemas': []
                            }

                        cities[i['city']]['cinemas'].append({'name': i['organization__name'], 'slug': i['organization__uni_slug']})

                    cities = sorted(cities.values(), key=operator.itemgetter('name'))

                    data = {}
                    for i in cities:
                        if not data.get(i['country']):
                            data[i['country']] = []
                        data[i['country']].append(i)

                    if count == 1:
                        return HttpResponseRedirect(reverse('organization_cinema', kwargs={'id': tmp}))

        else:
            msg = 'Слишком короткий запрос'

    return render_to_response('search_result.html', {'objs': data, 'element': element, 'msg': msg, 'srch_category': category, 'query': query_orig, 'count': count},  context_instance=RequestContext(request))

@never_cache
def kinoafisha_old_button(request, id):
    if request.current_site.domain == 'kinoafisha.ru':
        try:
            film = Film.objects.using('afisha').get(idalldvd=int(id))
        except Film.DoesNotExist:
            raise Http404

        img = create_kinoafisha_button(film, id)

        with open(img['output_path'], 'rb') as f:
            return HttpResponse(f.read(), mimetype="image/png")

    raise Http404

@never_cache
def kinoafisha_button(request):
    from urlparse import urlparse, parse_qs
    from release_parser.imdb import create_film_by_imdb_id

    warning = ''
    img = ()

    if request.POST:
        url = request.POST.get('url','').strip()
        if url:
            imdb_id = None
            film_id = None
            if 'imdb.com/title/tt' in url:
                xurl = url.split('imdb.com/title/tt')[1].split('/')[0].replace('/','')
                xurl = xurl.split('?')[0]
                imdb_id = int(xurl)
            elif 'kinoafisha.ru/' in url:
                xurl = urlparse(url)
                if xurl.query:
                    params = parse_qs(xurl.query)
                    film_id = params.get('id1')
                    film_id = film_id[0] if film_id else None
                else:
                    film_id = xurl.path.replace('/film/','').replace('/','')

                try:
                    film_id = int(film_id)
                except ValueError:
                    film_id = None

            if imdb_id or film_id:
                if imdb_id:
                    try:
                        film = Film.objects.using('afisha').get(idalldvd=imdb_id)
                    except Film.DoesNotExist:
                        try:
                            film = create_film_by_imdb_id(imdb_id)
                        except:
                            film = None
                else:
                    try:
                        film = Film.objects.using('afisha').get(pk=film_id)
                    except Film.DoesNotExist:
                        film = None

                if film:
                    img = create_kinoafisha_button(film, imdb_id)
                else:
                    warning = 'Фильм не найден в базе'
            else:
                warning = 'Неверный URL'
        else:
            warning = 'Вы не ввели URL'
    return render_to_response('kinoafisha/button.html', {'warning': warning, 'img': img},  context_instance=RequestContext(request))

def disable_adv_bg_func():
    price = 1
    now = datetime.datetime.now()

    objs = SiteBanners.objects.select_related('user', 'user__personinterface').filter(btype='0', user__personinterface__money__gte=price, balance__gte=price)

    for i in objs:
        interface = i.user.personinterface
        if interface.money >= price and i.balance >= price:
            to = now + datetime.timedelta(days=1)
            if i.bg_disable_dtime_to.date() != to.date():
                interface.money -= price
                interface.save()
                i.bg_disable_dtime_to = to
                i.balance -= price
                i.spent += price
                i.save()

@never_cache
def get_torrent(request, id):

    price = 1

    try:
        torrent = Torrents.objects.exclude(path=None).get(pk=id)
    except Torrents.DoesNotExist:
        raise Http404

    try:
        access = TorrentsUsers.objects.get(torrent=torrent, profile=request.profile)
    except TorrentsUsers.DoesNotExist:
        access = False

    interface = request.profile.personinterface

    if not access:
        if interface.money >= price:

            interface.money -= price
            interface.save()

            access, created = TorrentsUsers.objects.get_or_create(
                torrent = torrent,
                profile = request.profile,
                defaults = {
                    'torrent': torrent,
                    'profile': request.profile,
                })

    if access:
        full_path = '%s%s' % (settings.MEDIA_ROOT, torrent.path)
        filename = full_path.split('/')[-1]
        with open(full_path, 'r') as f:
            response = HttpResponse(f.read(), mimetype='application/x-bittorrent')
            response['Content-Disposition'] = 'attachment; filename=%s' % filename
            access.got = True
            access.save()
            return response

    raise Http404

@never_cache
def new_torrents(request):
    from slideblok.views import releasedata
    from movie_online.IR import check_int_rates_inlist

    price = 100

    interface = request.profile.personinterface

    year, genre, country, rate = (None, None, None, None)
    rate_filter = [u'ВСЕ',]
    year_filter = [u'ВСЕ',]
    genre_filter = {0: {'id': 0, 'name': u'ВСЕ'}}
    country_filter = {0: {'id': 0, 'name': u'ВСЕ'}}

    set_filter = False
    access = False
    if interface.money >= price or request.user.is_superuser:
        access = True

        torrents_kids = list(Torrents.objects.exclude(path=None).distinct('film').values('id', 'film'))

        kids = [i['film'] for i in torrents_kids]

        opinions = {'good': [], 'bad': []}
        for i in NewsFilms.objects.filter(kid__in=kids, message__visible=True, message__autor=request.profile, rate_1__gt=0).values('kid', 'rate'):
            if i['rate'] >= 4:
                opinions['good'].append(i['kid'])
            else:
                opinions['bad'].append(i['kid'])

        kids = set(kids) - set(opinions['bad'])

        exist_kids = []
        for i in list(Film.objects.using('afisha').filter(pk__in=kids, date__gte=datetime.date(1900,1,1)).values('year', 'genre1', 'genre2', 'genre3', 'genre1__name', 'genre2__name', 'genre3__name', 'country', 'country2', 'country__name', 'country2__name', 'id')):
            if i['year']:
                year_filter.append(int(i['year']))

            for j in ((i['genre1'], i['genre1__name']), (i['genre2'], i['genre2__name']), (i['genre3'], i['genre3__name'])):
                if j[0] and not genre_filter.get(j[0]):
                    genre_filter[int(j[0])] = {'id': int(j[0]), 'name': j[1]}

            for j in ((i['country'], i['country__name']), (i['country2'], i['country2__name'])):
                if j[0] and not country_filter.get(j[0]):
                    country_filter[int(j[0])] = {'id': int(j[0]), 'name': j[1]}

            exist_kids.append(i['id'])

        year_filter = sorted(set(year_filter), reverse=True)

        rates = check_int_rates_inlist(exist_kids)

        rates_tmp = set([i['int_rate'] for i in rates.values() if i['int_rate']])

        for i in rates_tmp:
            rate_filter.append(i)

        if request.POST:
            if 'filter' in request.POST:
                try: year = int(request.POST.get('year'))
                except ValueError: pass
                try: genre = int(request.POST.get('genre'))
                except ValueError: pass
                try: country = int(request.POST.get('country'))
                except ValueError: pass
                try: rate = int(request.POST.get('rate'))
                except ValueError: pass

        if year not in year_filter:
            year = year_filter[0]
        if genre not in genre_filter:
            genre = genre_filter[0]['id']
        if country not in country_filter:
            country = country_filter[0]['id']
        if rate not in rate_filter:
            rate = rate_filter[0]

        if year != u'ВСЕ' or rate != u'ВСЕ' or genre != 0 or country != 0:
            set_filter = True

        genre_filter = sorted(genre_filter.values(), key=operator.itemgetter('name'))
        country_filter = sorted(country_filter.values(), key=operator.itemgetter('name'))

        filter = {'pk__in': kids, 'date__gte': datetime.date(1900,1,1)}

        if year != u'ВСЕ':
            filter['year__exact'] = year

        queries = []
        if genre:
            for q in [Q(genre1__pk=genre), Q(genre2__pk=genre), Q(genre3__pk=genre)]:
                queries.append(q)
        if country:
            for q in [Q(country__pk=country), Q(country2__pk=country)]:
                queries.append(q)

        if queries:
            query = queries.pop()
            for item in queries:
                query |= item

            torrents_kids = list(Film.objects.using('afisha').filter(query, **filter).values('pk', 'date'))
        else:
            torrents_kids = list(Film.objects.using('afisha').filter(**filter).values('pk', 'date'))

        kids = [i['pk'] for i in torrents_kids]

        tusers = list(TorrentsUsers.objects.filter(profile=request.profile, torrent__film__in=kids, got=True).values_list('torrent__film', flat=True))

        for i in torrents_kids:
            if not i['date']:
                i['date'] = datetime.datetime(3000,1,1)

        torrents_kids = sorted(torrents_kids, key=operator.itemgetter('date'), reverse=True)

        page = request.GET.get('page')
        try:
            page = int(page)
        except (ValueError, TypeError):
            page = 1

        p, page = pagi(page, torrents_kids, 20)

        torrents_films_dict = {}
        for i in p.object_list:
            torrents_films_dict[i['pk']] = {}

        data = []

        for i in releasedata(torrents_films_dict, {}, persons=True, likes=True, trailers=True, reviews=True, poster_size='big'):
            i['got'] = True if i['id'] in tusers else False
            if not i['release_date']:
                i['release_date'] = datetime.datetime(3000,1,1)

            if i['id'] in opinions['good']:
                i['opinion'] = True

            if rate == u'ВСЕ' or int(rate) == int(i['rate']):
                data.append(i)

        data = sorted(data, key=operator.itemgetter('release_date'), reverse=True)
    else:
        p = None
        page = 1
        data = []

    tmplt = 'kinoafisha/new_torrents.html'

    url_name = resolve(request.path_info).url_name

    return render_to_response(tmplt, {'data': data, 'page': page, 'p': p, 'url_name': url_name, 'year_filter': year_filter, 'genre_filter': genre_filter, 'country_filter': country_filter, 'year': year, 'genre': genre, 'country': country, 'access': access, 'rate_filter': rate_filter, 'rate': rate, 'set_filter': set_filter}, context_instance=RequestContext(request))

@never_cache
def best_torrents(request):
    from slideblok.views import releasedata

    price = 100

    interface = request.profile.personinterface

    access = False
    if interface.money >= price:
        access = True

    else:
        pass

    tmplt = 'kinoafisha/best_torrents.html'

    url_name = resolve(request.path_info).url_name

    return render_to_response(tmplt, {'data': data, 'page': page, 'p': p, 'url_name': url_name, 'year_filter': year_filter, 'genre_filter': genre_filter, 'country_filter': country_filter, 'year': year, 'genre': genre, 'country': country, 'access': access}, context_instance=RequestContext(request))

@only_superuser
@never_cache
def torrents_listing(request, source):
    from slideblok.views import releasedata
    from news.views import cut_description
    from movie_online.IR import check_int_rates_inlist
    from release_parser.func import get_file_modify_datetime

    source = ImportSources.objects.get(pk=source)

    today = datetime.datetime.now().date()
    yesterday = today - datetime.timedelta(days=1)

    file_add = 'xml' if source.url == 'http://rutracker.org/' else 'html'
    file_path = '%s__%s__films.xml' % (file_add, source.dump)
    parser_time = get_file_modify_datetime(settings.SUCCESS_LOG_PATH, file_path)
    parser_time = parser_time + datetime.timedelta(hours=3)

    release, new, rate = (None, None, None)

    release_filter = {
        0: {'id': 0, 'name': u'ВСЕ'},
        1: {'id': 1, 'name': u'Кинопрокат'},
        2: {'id': 2, 'name': u'Прочие'},
    }

    new_filter = {
        0: {'id': 0, 'name': u'ВСЕ'},
        1: {'id': 1, 'name': u'Новые'},
    }

    rate_filter = [u'ВСЕ',]

    if request.POST:
        if 'checker' in request.POST:
            checker = request.POST.getlist('checker')
            if checker:
                SourceFilms.objects.filter(source_obj=source, pk__in=checker).update(rel_ignore=True)
            return HttpResponseRedirect(reverse('torrents_listing', kwargs={'source': source.pk}))
        else:
            release = request.POST.get('release')
            new = request.POST.get('new')
            rate = request.POST.get('rate')

    kids = list(SourceFilms.objects.filter(source_obj=source, rel_ignore=False).values_list('kid', flat=True))

    rates = check_int_rates_inlist(kids)
    rates_tmp = set([i['int_rate'] for i in rates.values() if i['int_rate']])
    for i in rates_tmp:
        rate_filter.append(i)

    if rate:
        try:
            rate = int(rate)
        except ValueError:
            rate = u'ВСЕ'
    if release:
        release = int(release)
    if new:
        new = int(new)
    sess_filter = request.session.get('torrents_listing_filter',{})

    if rate == None and release == None and new == None:
        sess_filter = request.session.get('torrents_listing_filter',{})
        if sess_filter:
            rate = sess_filter['rate']
            release = sess_filter['release']
            new = sess_filter['new']
        else:
            new = 1

    if not new:
        source_films = SourceFilms.objects.filter(source_obj=source, rel_ignore=False)
    elif new == 1:
        source_films = SourceFilms.objects.filter(source_obj=source, extra='new', rel_ignore=False)

    tmp_date = datetime.datetime(3000,1,1)

    films = {}
    for i in source_films:
        new = True if i.extra == 'new' else False
        films[i.kid] = {'source_id': i.source_id, 'new': new, 'pk': i.id, 'release': tmp_date, 'kid': i.kid}

    for i in list(Film.objects.using('afisha').filter(pk__in=films.keys(), date__gte=datetime.datetime(1900,1,1)).values('id', 'date')):
        films[i['id']]['release'] = i['date']

    films_sorted = sorted(films.values(), key=operator.itemgetter('release'), reverse=True)

    page = request.GET.get('page')
    try:
        page = int(page)
    except (ValueError, TypeError):
        page = 1

    p, page = pagi(page, films_sorted, 100)

    films_ids = {}
    for i in p.object_list:
        films_ids[i['kid']] = {}

    torrents = {}
    for i in Torrents.objects.filter(film__in=films_ids.keys()).exclude(path=None):
        if not torrents.get(i.film):
            torrents[i.film] = {'0': None, '1': None, '2': None}
        q = i.quality_avg if i.quality_avg else '1'
        torrents[i.film][q] = i

    data_tmp = releasedata(films_ids, {}, persons=False, likes=False, trailers=False, reviews=False, poster_size='small')

    upd = []

    data = []
    for i in data_tmp:
        torrent = torrents.get(i['id'])
        f = films.get(i['id'])
        txt_cut = cut_description(i['descript'], True, 150)
        i['descript_cut'] = txt_cut
        i['descript'] = ''
        i['new'] = f['new']
        i['source_id'] = f['pk']
        i['torrent'] = torrent

        if not i['release_date']:
            i['release_date'] = datetime.datetime(3000,1,1)

        if source.url == 'http://cinemate.cc/':
            i['source_url'] = u'http://cinemate.cc/movie/%s/' % f['source_id']
        elif source.url == 'http://rutracker.org/':
            i['source_url'] = u'http://rutracker.org/forum/viewtopic.php?t=%s' % f['source_id']

        if f['new']:
            upd.append(f['pk'])

        next = True

        if release == 1 and i['release_date'].year == 3000:
            next = False
        elif release == 2 and i['release_date'].year < 3000:
            next = False

        if next:
            if rate:
                if rate == u'ВСЕ' or rate == int(i['rate']):
                    data.append(i)
            else:
                data.append(i)

    films = sorted(data, key=operator.itemgetter('release_date'), reverse=True)

    SourceFilms.objects.filter(pk__in=upd).update(extra=None)

    request.session['torrents_listing_filter'] = {'rate': rate, 'new': new, 'release': release}

    return render_to_response('kinoafisha/torrents_listing.html', {'data': films, 'source': source, 'p': p, 'page': page, 'release_filter': release_filter.values(), 'release': release, 'new_filter': new_filter.values(), 'new': new, 'rate_filter': rate_filter, 'rate': rate, 'parser_time': parser_time, 'today': today, 'yesterday': yesterday}, context_instance=RequestContext(request))

@never_cache
def booking(request):

    timer = time.time()

    today = datetime.datetime.today().date()

    def get_timeline():
        next_day = today + datetime.timedelta(days=1)
        timeline = [{'time': datetime.datetime(today.year, today.month, today.day, 9, 0), 'minutes': False}]
        while True:
            time_to = timeline[-1]['time'] + datetime.timedelta(minutes=15)
            if time_to.date() == next_day:
                break
            minutes = False if time_to.minute == 0 else True
            timeline.append({'time': time_to, 'minutes': minutes})
        return timeline

    today = datetime.date.today()
    date_from = today

    # Получаем релизы по стране
    country = 2

    if request.POST:
        country = int(request.POST.get('country', 2))
        date_from = request.POST.get('date_from', str(date_from))
        date_from = datetime.datetime.strptime(date_from, '%Y-%m-%d')

    releases = []

    if country == 2:
        releases = list(Film.objects.using('afisha').filter(date__gte=today).order_by('date').values('id', 'date'))
    elif country == 43:
        for i in UkrainianReleases.objects.filter(release__gte=today).order_by('release'):
            releases.append({'id': i.kid, 'date': i.release})

    kids = set([i['id'] for i in releases])

    names = {}
    for i in FilmsName.objects.using('afisha').filter(film_id__pk__in=kids, status=1, type__in=(1, 2)):
        if not names.get(i.film_id_id):
            names[i.film_id_id] = {}
        names[i.film_id_id][i.type] = i.name

    for i in releases:
        name = names.get(i['id'])
        i['name'] = name[2] if name.get(2) else name[1]

    # Проверяем права
    if request.user.is_superuser:
        access = True
    else:
        access = request.user.groups.filter(name='Букер').exists()

    articles = []
    days_range = []
    halls = {}
    data = {}

    if access:

        # Получаем заметки букера
        try:
            osm = OrgSubMenu.objects.get(page_type='1', booker_profile=request.profile)
        except OrgSubMenu.DoesNotExist:
            pass
        else:
            articles = osm.news.all().order_by('-dtime')

        # все кинотеатры из настроек букера
        cinemas = {}
        cinemas_kids = []
        for i in Cinema.objects.filter(bookingsettings__profile=request.profile).distinct('pk').values('id', 'code', 'bookercinemas__permission'):
            cinemas[i['id']] = {'id': i['id'], 'kid': i['code'], 'name': '', 'access': i['bookercinemas__permission']}
            cinemas_kids.append(i['code'])

        for i in list(NameCinema.objects.filter(cinema__code__in=cinemas_kids, status=1).values('name', 'cinema')):
            cinemas[i['cinema']]['name'] = i['name']

        # залы
        for i in list(NameHall.objects.filter(status=1, hall__cinema__code__in=cinemas_kids).distinct('hall').values('name', 'hall', 'hall__cinema')):
            cinema = cinemas.get(i['hall__cinema'])
            halls[i['hall']] = {'hall_id': i['hall'], 'cinema': cinema, 'cinema_name': cinema['name'], 'name': i['name'], 'dates': {}, 'date_range': {}}

        # диапозон дат сеансов
        days = BookingSchedules.objects.filter(hall__cinema__code__in=cinemas_kids, dtime__gte=today).values_list('dtime', flat=True)
        days_range = list(set([i.date() for i in days]))
        days_range.sort()

        # сеансы от ... и далее
        date_to = date_from + datetime.timedelta(days=1)
        schedules = BookingSchedules.objects.select_related('hall').filter(hall__cinema__code__in=cinemas_kids, dtime__gte=date_from)

        # фильмы
        schedules_ids = [i.id for i in schedules]
        source_films_data = list(SourceFilms.objects.filter(bookingschedules__pk__in=schedules_ids).values('kid', 'bookingschedules'))
        source_kids = set([i['kid'] for i in source_films_data])

        for i in FilmsName.objects.using('afisha').filter(film_id__pk__in=source_kids, status=1, type__in=(1, 2)).exclude(film_id__id__in=names.keys()):
            if not names.get(i.film_id_id):
                names[i.film_id_id] = {}
            names[i.film_id_id][i.type] = i.name

        source_films = {}
        for i in source_films_data:
            if not source_films.get(i['bookingschedules']):
                source_films[i['bookingschedules']] = []
            name = names.get(i['kid'])
            name = name[2] if name.get(2) else name[1]
            source_films[i['bookingschedules']].append({'kid': i['kid'], 'name': name})

        # удобно складываем все данные
        for i in schedules:
            if not data.get(i.hall_id):
                #cinema = cinemas.get(i.hall.cinema_id)
                hall = halls.get(i.hall_id)
                data[i.hall_id] = {'hall_id': i.hall_id, 'cinema': hall['cinema'], 'name': hall['name'], 'dates': {}, 'date_range': {}}
            films = source_films.get(i.id)

            if not data[i.hall_id]['dates'].get(i.dtime.date()):
                data[i.hall_id]['dates'][i.dtime.date()] = {'date': i.dtime.date(), 'ids': [], 'times': []}

            data[i.hall_id]['dates'][i.dtime.date()]['times'].append({'time': i.dtime, 'films': films, 'tmp': i.temp, 'id': i.id})
            data[i.hall_id]['dates'][i.dtime.date()]['ids'].append('%s%s' % (i.dtime.time(), films))
            data[i.hall_id]['dates'][i.dtime.date()]['ids'].sort()

        # формируем диапозон дат с одинкаовыми сеансами
        for i in data.values():
            i['dates'] = sorted(i['dates'].values(), key=operator.itemgetter('date'))

            old_date_times = []
            sch_date_from = None
            sch_date_to = None
            for j in i['dates']:
                set_dates = True
                if old_date_times:
                    if old_date_times == j['ids']:
                        sch_date_to = j['date']
                        set_dates = False

                if set_dates:
                    sch_date_from = j['date']
                    sch_date_to = j['date']

                old_date_times = j['ids']

                if not i['date_range'].get(sch_date_from):
                    i['date_range'][sch_date_from] = {'from': sch_date_from, 'to': None, 'times': j['times']}
                i['date_range'][sch_date_from]['to'] = sch_date_to

        for i in data.values():
            i['date_range'] = sorted(i['date_range'].values(), key=operator.itemgetter('from'))
            for j in i['date_range']:
                j['times'] = sorted(j['times'], key=operator.itemgetter('time'))

    halls = sorted(halls.values(), key=operator.itemgetter('cinema_name'))

    timeline = get_timeline()
    url_name = resolve(request.path_info).url_name
    try:
        date_from = date_from.date()
    except AttributeError:
        pass

    timer = '%5.2f' % (time.time()-timer)
    return render_to_response('kinoafisha/booking.html', {'url_name': url_name, 'timeline': timeline, 'country': country, 'releases': releases, 'articles': articles, 'access': access, 'days_range': days_range, 'data': data.values(), 'date_from': date_from, 'timer': timer, 'cached_page': '', 'halls': halls}, context_instance=RequestContext(request))

@never_cache
def booking_article_add(request):
    from letsgetrhythm.views import view_func
    if request.POST:

        if request.user.is_superuser:
            access = True
        else:
            access = request.user.groups.filter(name='Букер').exists()

        articles = []
        if access:
            osm, created = OrgSubMenu.objects.get_or_create(
                page_type = '1',
                booker_profile = request.profile,
                defaults = {
                    'name': 'Booking заметки',
                    'page_type': '1',
                    'booker_profile': request.profile,
                })

            data = view_func(request, osm.id, None, 'booking', access)

            return HttpResponseRedirect(reverse('booking'))

    raise Http404

@never_cache
def booking_get_excel_doc(request):
    from openpyxl import Workbook
    from openpyxl.cell import get_column_letter
    from openpyxl.styles import Font

    if request.user.is_superuser:
        access = True
    else:
        access = request.user.groups.filter(name='Букер').exists()

    data = {}
    halls = {}
    if access and request.POST:
        '''
        halls_ids = [3552, 1970, 2539]
        date_from = datetime.date(2016, 4, 25)
        date_to = datetime.date(2016, 5, 5)
        date_to = date_to + datetime.timedelta(days=1)
        print_temp = False
        all_dates = False
        '''

        halls_ids = request.POST.getlist('ex_halls')
        date_from = request.POST.get('ex_date_from')
        date_to = request.POST.get('ex_date_to')
        all_dates = request.POST.get('ex_date_all')
        print_temp = request.POST.get('ex_temp')

        date_from = datetime.datetime.strptime(str(date_from), '%Y-%m-%d')
        date_to = datetime.datetime.strptime(str(date_to), '%Y-%m-%d')
        all_dates = True if all_dates else False
        print_temp = True if print_temp else False

        # все кинотеатры из настроек букера
        cinemas = {}
        cinemas_kids = []
        for i in Cinema.objects.filter(bookingsettings__profile=request.profile, bookercinemas__permission='1').distinct('pk').values('id', 'code', 'bookercinemas__permission'):
            cinemas[i['id']] = {'id': i['id'], 'kid': i['code'], 'name': '', 'access': i['bookercinemas__permission']}
            cinemas_kids.append(i['code'])

        for i in list(NameCinema.objects.filter(cinema__code__in=cinemas_kids, status=1).values('name', 'cinema')):
            cinemas[i['cinema']]['name'] = i['name']

        # залы
        for i in list(NameHall.objects.filter(status=1, hall__cinema__code__in=cinemas_kids).distinct('hall').values('name', 'hall', 'hall__cinema')):
            cinema = cinemas.get(i['hall__cinema'])
            halls[i['hall']] = {'hall_id': i['hall'], 'cinema': cinema, 'cinema_name': cinema['name'], 'name': i['name'], 'dates': {}, 'date_range': {}}

        filter = {'hall__id__in': halls_ids, 'temp': print_temp}
        if all_dates:
            filter['dtime__gte'] = datetime.date.today()
        else:
            filter['dtime__gte'] = date_from
            filter['dtime__lt'] = date_to

        schedules = BookingSchedules.objects.select_related('hall').filter(**filter)

        schedules_ids = [i.id for i in schedules]
        source_films_data = list(SourceFilms.objects.filter(bookingschedules__pk__in=schedules_ids).values('kid', 'bookingschedules'))
        source_kids = set([i['kid'] for i in source_films_data])

        names = {}
        for i in FilmsName.objects.using('afisha').filter(film_id__pk__in=source_kids, status=1, type__in=(1, 2)).exclude(film_id__id__in=names.keys()):
            if not names.get(i.film_id_id):
                names[i.film_id_id] = {}
            names[i.film_id_id][i.type] = i.name

        # фильмы
        source_films = {}
        for i in source_films_data:
            if not source_films.get(i['bookingschedules']):
                source_films[i['bookingschedules']] = []
            name = names.get(i['kid'])
            name = name[2] if name.get(2) else name[1]
            source_films[i['bookingschedules']].append({'kid': i['kid'], 'name': name})

        # удобно складываем все данные
        for i in schedules:
            if not data.get(i.hall_id):
                #cinema = cinemas.get(i.hall.cinema_id)
                hall = halls.get(i.hall_id)
                data[i.hall_id] = {'hall_id': i.hall_id, 'cinema': hall['cinema'], 'name': hall['name'], 'dates': {}, 'date_range': {}}
            films = source_films.get(i.id)

            if not data[i.hall_id]['dates'].get(i.dtime.date()):
                data[i.hall_id]['dates'][i.dtime.date()] = {'date': i.dtime.date(), 'ids': [], 'times': []}

            data[i.hall_id]['dates'][i.dtime.date()]['times'].append({'time': i.dtime, 'films': films, 'tmp': i.temp, 'id': i.id})
            data[i.hall_id]['dates'][i.dtime.date()]['ids'].append('%s%s' % (i.dtime.time(), films))
            data[i.hall_id]['dates'][i.dtime.date()]['ids'].sort()

        # формируем диапозон дат с одинкаовыми сеансами
        for i in data.values():
            i['dates'] = sorted(i['dates'].values(), key=operator.itemgetter('date'))

            old_date_times = []
            sch_date_from = None
            sch_date_to = None
            for j in i['dates']:
                set_dates = True
                if old_date_times:
                    if old_date_times == j['ids']:
                        sch_date_to = j['date']
                        set_dates = False

                if set_dates:
                    sch_date_from = j['date']
                    sch_date_to = j['date']

                old_date_times = j['ids']

                if not i['date_range'].get(sch_date_from):
                    i['date_range'][sch_date_from] = {'from': sch_date_from, 'to': None, 'times': j['times']}
                i['date_range'][sch_date_from]['to'] = sch_date_to

        wb = Workbook()

        for index, i in enumerate(data.values()):
            if index == 0:
                ws = wb.active
            else:
                ws = wb.create_sheet()

            ws.title = u'%s, %s' % (i['cinema']['name'][:13], i['name'][:13])

            count = 0
            for j in i['date_range'].values():
                count += len(j['times']) + 1

            last_row = 0
            for j in sorted(i['date_range'].values(), key=operator.itemgetter('from')):
                last_row += 1

                cells = 'A%s:B%s' % (last_row, last_row)

                ws.merge_cells(cells)
                ws["A%s" % last_row].font = Font(name='Calibri', size=12, bold=True)
                ws["A%s" % last_row] = '%s - %s' % (j['from'], j['to'])

                j['times'] = sorted(j['times'], key=operator.itemgetter('time'))

                for row in range(len(j['times'])):
                    last_row += 1

                    t = tmp_date(j['times'][row]['time'], 'H:i')
                    t = t.encode('utf-8')
                    tfilms = ''
                    for f in j['times'][row]['films']:
                        if tfilms:
                            tfilms += u', '
                        tfilms += f['name']

                    for ind, col in enumerate((t, tfilms)):
                        ws.cell(column=ind+1, row=last_row, value=col)

        file_name = 'booking_schedules_%s.xlsx' % request.user.id
        file_path = '%s/%s' % (settings.BOOKING_EXCEL_PATH, file_name)

        wb.save(filename=file_path)

        with open(file_path, 'r') as f:
            file = f.read()

        response = HttpResponse(file, mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
        response['Content-Disposition'] = 'attachment; filename=%s' % file_name
        return response

        '''
        doc = ''

        for i in data.values():
            doc += u'<b>%s / %s</b><br />' % (i['cinema']['name'][:13], i['name'][:13])

            for j in sorted(i['date_range'].values(), key=operator.itemgetter('from')):

                doc += u'- - - - - %s - %s<br />' % (j['from'], j['to'])

                for t in sorted(j['times'], key=operator.itemgetter('time')):

                    doc += u'%s, ' % t['time'].time()
                    for f in t['films']:
                        doc += u'%s, ' % f['name']
                    doc += u'<br />'

        return HttpResponse(str(doc.encode('utf-8')))
        '''

    raise Http404

def yandex(request):
    return render_to_response('yandex_35c18ae022681480.html')


from django import forms
from django.core import validators

class MyForm(forms.Form):

	def check_text_length(value):
		if len(value) > 20:

			raise forms.ValidationError('Text length exceeded')

	name 	= forms.CharField(validators = [validators.MaxLengthValidator(20)])
	email 	= forms.EmailField()
	confirm_email = forms.EmailField(label = 'Confirm Email')
	text 	= forms.CharField(widget = forms.Textarea, validators = [check_text_length])
	botcatcher = forms.CharField(widget = forms.HiddenInput, required = False)

	def clean_botcatcher(self):

		clean_botcatcher = self.cleaned_data['botcatcher']

		if len(clean_botcatcher) > 0:

			raise forms.ValidationError('GOTCHA BOT!!')

		return clean_botcatcher

	def clean(self):

		all_clean_data = super().clean()

		print(all_clean_data)

		if all_clean_data['email'] != all_clean_data['confirm_email']:

			raise forms.ValidationError('Emails do not match')


#!/usr/bin/env python3.5

# with daemon.DaemonContext():
# app = hwhome.Main(True)

import sys
import os
import time
import argparse
import logging
import daemon
from daemon import pidfile
import hwhome
import lockfile
import settings

debug_p = True

def do_something(logf):
  ### This does the "work" of the daemon
  logger = logging.getLogger('robot_daemon')
  logger.setLevel(logging.INFO)
  fh = logging.FileHandler(logf)
  fh.setLevel(logging.INFO)
  formatstr = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
  formatter = logging.Formatter(formatstr)
  fh.setFormatter(formatter)
  logger.addHandler(fh)
  # logger.debug("this is a DEBUG message")
  # logger.info("this is an INFO message")
  # logger.error("this is an ERROR message")
  settings.logger = logger
  app = hwhome.Main(True, logger)

def start_daemon(pidf, logf):
  ### This launches the daemon in its context
  ### XXX pidfile is a context
  with daemon.DaemonContext(
    working_directory='/home/pi/workspace/python/hwPy',
    # umask=0o002,
    # pidfile=pidfile.TimeoutPIDLockFile(pidf)
    # pidfile=lockfile.FileLock('/var/run/robot_daemon.pid')
    ) as context:
    do_something(logf)

if __name__ == "__main__":
  settings.init()
  parser = argparse.ArgumentParser(description="Robot daemon in Python")
  parser.add_argument('-p', '--pid-file', default='/var/run/robot_daemon.pid')
  parser.add_argument('-l', '--log-file', default='/var/log/robot_daemon.log')
  args = parser.parse_args()

  start_daemon(pidf=args.pid_file, logf=args.log_file) # PRODUCTION

  # do_something(logf=args.log_file) # DEV


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Speciallan

import cv2
import json
import numpy as np
import os
from xml.etree import ElementTree
import matplotlib.pyplot as plt
import random
from shutil import copyfile
import os
import xml.etree.cElementTree as ET
from xml.dom import minidom

#保存xml格式的
def save_annotations(filepath, filename, image, boxes):
    #保存图像
    cv2.imwrite(os.path.join(os.path.join(filepath, "JPEGImages"), filename + ".jpg"), image)

    #保存画框图像
    for i in range(len(boxes)):
        cv2.rectangle(image,
                      (boxes[i]['xmin'],#xmin
                       boxes[i]['ymin']), #ymin
                      (boxes[i]['xmax'],#xmax
                       boxes[i]['ymax']), #ymax
                      (0,255,0),
                      1)
    cv2.imwrite(os.path.join(os.path.join(filepath, "DrawBoxes"), filename + ".jpg"), image)

    #保存labels
    root = ET.Element("annotation")
    ET.SubElement(root, "folder").text = "images"
    ET.SubElement(root, "filename").text = filename + ".jpg"
    size = ET.SubElement(root, "size")
    ET.SubElement(size, "width").text = str(image.shape[0])
    ET.SubElement(size, "height").text = str(image.shape[1])
    ET.SubElement(size, "depth").text = str(image.shape[2])
    for box in boxes:
        object = ET.SubElement(root, "object")
        ET.SubElement(object, "name").text = box["name"]
        ET.SubElement(object, "pose").text = "Unspecified"
        ET.SubElement(object, "truncated").text = "0"
        ET.SubElement(object, "difficult").text = "0"
        bndbox = ET.SubElement(object, "bndbox")
        ET.SubElement(bndbox, "xmin").text = str(box["xmin"])
        ET.SubElement(bndbox, "ymin").text = str(box["ymin"])
        ET.SubElement(bndbox, "xmax").text = str(box["xmax"])
        ET.SubElement(bndbox, "ymax").text = str(box["ymax"])

    xmlstr = minidom.parseString(ET.tostring(root)).toprettyxml(indent="    ")
    myfile = open(os.path.join(os.path.join(filepath, "Annotations"), filename + ".xml"), "w")
    myfile.write(xmlstr)

#获得全部的图片路径
def get_all_img_path(original_path):
    img_path=[]
    annotation_path=[]
    for files in os.listdir(original_path):
        img_path.append(original_path+files+"/"+files+".jpg")
        annotation_path.append(original_path+files+"/"+"annotation_ship.xlsx")
    return [img_path,annotation_path]

#获得label文件
def get_label(path):
    #获得xls文件
    workbook=xlrd.open_workbook(path)
    worksheet=workbook.sheet_by_name("Work")

    data=[]
    for i in range(1,worksheet.nrows,1):#获取该表总行数
        one_data=[]
        for j in range(1,5,1):#列
            one_data.append(int(worksheet.cell_value(i,j)))
        data.append(one_data)

    return data#返回xmin ymin xmax ymax

#制作目标检测数据集
def make_datasets(all_img_path,annotation_path,save_path,size,overlap):
    #新建需要保存的文件夹
    if os.path.exists(save_path)==False:os.makedirs(save_path)
    if os.path.exists(save_path+"JPEGImages")==False:os.makedirs(save_path+"JPEGImages")
    if os.path.exists(save_path+"Annotations")==False:os.makedirs(save_path+"Annotations")
    if os.path.exists(save_path+"DrawBoxes")==False:os.makedirs(save_path+"DrawBoxes")
    #对每一张图像制作数据集
    for i in range(7,9,1):
        all_get_box=0#累计所有能得到的box
        #获得label
        labels=get_label(annotation_path[i])
        #读入图像
        print("读入"+all_img_path[i])
        img=cv2.imread(all_img_path[i])

        #按步长依次制作
        for y in range(0,img.shape[0]-size,size-overlap):#
            for x in range(0,img.shape[1]-size,size-overlap):
                #针对左上角为x、y的图像进行labels分配
                boxes=[]
                for t in range(len(labels)):
                    #对label进行大小值转换
                    the_xmin=-1
                    the_xmax=-1
                    the_ymin=-1
                    the_ymax=-1
                    #进行大小写整合
                    if labels[t][0]<labels[t][2]:
                        the_xmin=labels[t][0]
                        the_xmax=labels[t][2]
                    elif labels[t][2]<labels[t][0]:
                        the_xmin=labels[t][2]
                        the_xmax=labels[t][0]
                    if labels[t][1]<labels[t][3]:
                        the_ymin=labels[t][1]
                        the_ymax=labels[t][3]
                    elif labels[t][3]<labels[t][1]:
                        the_ymin=labels[t][3]
                        the_ymax=labels[t][1]
                    #判断是否在内部
                    if(the_xmin>=x and
                            the_ymin>=y and
                            the_xmax<=x+size and
                            the_ymax<=y+size and #在这个框内
                            the_xmin!=-1 and
                            the_ymin!=-1 and
                            the_xmax!=-1 and
                            the_ymax!=-1):#满足严格大小
                        label_temp=dict([('xmin',the_xmin-x),
                                         ('ymin',the_ymin-y),
                                         ('xmax',the_xmax-x),
                                         ('ymax',the_ymax-y),
                                         ('name','ship')])
                        boxes.append(label_temp)
                #保存有label的图像
                if len(boxes)!=0:
                    #统计完成保存
                    this_img=img[y:y+size,x:x+size,:]
                    save_annotations(save_path,
                                     all_img_path[i][17:19]+"_"+str(y)+"_"+str(x),
                                     this_img, boxes)

                    #统计相关数据并输出
                    all_get_box=all_get_box+len(boxes)
                    print(str(all_img_path[i][17:19])+" "+str(y)+"_"+str(x)+" box:"+str(all_get_box)+"/"+str(len(labels)))

    #从这里开始
    return True

#统计相关的分布
def show_statistics(all_img_path,annotations_path):
    all_w=[]#所有的宽度
    all_h=[]#所有的高度
    sum_label=0#所有的label个数
    one_line=0#统计只有一根线的个数
    w_h_ratio=[]#高宽比

    #开始统计
    for i in range(0,len(annotations_path),1):
        print(annotations_path[i])
        print("读入图像:"+all_img_path[i])
        #        img=cv2.imread(all_img_path[i])#获得图像
        boxes=get_label(annotations_path[i])#获得label
        sum_label=sum_label+len(boxes)#所有label个数累计求和
        for j in range(len(boxes)):
            xmin=0
            ymin=0
            w=0
            h=0
            #计算高宽
            if boxes[j][2]-boxes[j][0]>0:
                xmin=boxes[j][0]
                w=boxes[j][2]-boxes[j][0]
            else:
                xmin=boxes[j][2]
                w=boxes[j][0]-boxes[j][2]
            if boxes[j][3]-boxes[j][1]>0:
                ymin=boxes[j][1]
                h=boxes[j][3]-boxes[j][1]
            else:
                ymin=boxes[j][3]
                h=boxes[j][1]-boxes[j][3]
            #统计
            all_w.append(w)
            all_h.append(h)
            if h!=0:
                w_h_ratio.append(w/h)#比例
            #一根线
            if(w==0 or h==0):
                one_line=one_line+1

            #根据条件，把其固定要求的画框显示下来
    #            if os.path.exists("temp/")==False:os.makedirs("temp/")
    #            if (w==1 or h==1):
    #                overlap=100
    #                cv2.rectangle(img,(xmin,ymin),(xmin+w,ymin+h),(0,255,0),1)
    #                this_img=img[ymin-overlap:ymin+h+overlap,
    #                             xmin-overlap:xmin+w+overlap,:]
    #                cv2.imwrite("temp/"+str(all_img_path[i][17:19])+" "+str(j)+".jpg",this_img)

    #画出统计图
    def draw_data(data,bins,title):
        all_w=np.array(data)
        plt.title(title)
        plt.hist(all_w,bins=bins,color='green',edgecolor='black',histtype='bar')
        plt.grid(linestyle='--', color="gray")
        plt.show()

    #画图
    draw_data(all_w,50,"all_w")
    draw_data(all_h,50,"all_h")
    draw_data(w_h_ratio,50,"w_h_ratio")
    print("one line num: "+str(one_line))
    print("all label "+str(sum_label))

    return all_w

#-------------------------------上次的---------------------------------

#获得全部文件名list
def get_all_file_name(path):
    name=[]
    for files in os.listdir(path):
        files=files.replace(".jpg","")
        name.append(files)
    return name

#输入label.xml的连接输出boxes的位置
#def get_label(path):
#    tree = ElementTree.parse(path)
#    root = tree.getroot()
#    bounding_boxes = []
#    for object_tree in root.findall('object'):
#        class_name = object_tree.find('name').text
#        for bounding_box in object_tree.iter('bndbox'):
#            # 得到归一化坐标
#            xmin = int(bounding_box.find('xmin').text)
#            ymin = int(bounding_box.find('ymin').text)
#            xmax = int(bounding_box.find('xmax').text)
#            ymax = int(bounding_box.find('ymax').text)
#        bounding_box ={'class':class_name,'xmin':xmin,'ymin':ymin,'xmax':xmax,'ymax':ymax}
#        bounding_boxes.append(bounding_box)
#    return bounding_boxes

#制作分类数据集
def make_classify_datasets(classify_path,img_path,annotations_path,max_size):
    #建立目录
    if os.path.exists(classify_path+"pos/")==False:os.makedirs(classify_path+"pos/")
    if os.path.exists(classify_path+"neg/")==False:os.makedirs(classify_path+"neg/")

    #首先获得所有原始图像的名字
    pos_name_list=get_all_file_name(classify_path+"/from")

    #统计label分布
    show_statistics(pos_name_list,annotations_path)

    #对每一张图像进行处理
    num_use=0#能使用的数量
    for i in range(len(pos_name_list)):
        print(str(i)+"/"+str(len(pos_name_list)))
        boxes=get_label(annotations_path+pos_name_list[i]+".xml")#getbox
        img=cv2.imread(img_path+pos_name_list[i]+".jpg")
        for j in range(len(boxes)):
            #满足要求就统计
            this_w=boxes[j]['xmax']-boxes[j]['xmin']
            this_h=boxes[j]['ymax']-boxes[j]['ymin']
            new_x=int(boxes[j]['xmin']+this_w/2)
            new_y=int(boxes[j]['ymin']+this_h/2)
            if(this_w<=max_size and#宽度小于max_size
                    this_h<=max_size and#高度小于max_size
                    int(new_x-max_size/2)>=0 and#左上角在范围内
                    int(new_y-max_size/2)>=0 and#左上角在范围内
                    int(new_x+max_size/2)<=img.shape[0] and#右下角在范围内
                    int(new_y+max_size/2)<=img.shape[1]#右下角在范围内
            ):
                num_use=num_use+1
                print(num_use)
                #正例保存
                pos_img=img[int(new_y-max_size/2):int(new_y+max_size/2),
                        int(new_x-max_size/2):int(new_x+max_size/2),:]
                cv2.imwrite(classify_path+"pos/"+pos_name_list[i]+"_"+str(j)+".jpg",pos_img)

                #随机选择一个区域负例
                can_get_neg=False
                wait_time=100000
                while can_get_neg==False and wait_time>=0:#循环生成
                    neg_x=int(random.random()*10000)%(img.shape[1]-max_size)
                    neg_y=int(random.random()*10000)%(img.shape[1]-max_size)
                    #判断这个是否满足要求
                    can_use=True
                    for tt in range(len(boxes)):
                        if (neg_x>=boxes[tt]['xmin']-max_size and
                                neg_y>=boxes[tt]['ymin']-max_size and
                                new_x<=boxes[tt]['xmax'] and
                                new_y<=boxes[tt]['ymax']):
                            can_use=False
                            break
                    #满足就保存，不满足就继续
                    if can_use==True:
                        neg_img=img[neg_y:neg_y+max_size,
                                neg_x:neg_x+max_size,:]
                        cv2.imwrite(classify_path+"neg/"+pos_name_list[i]+"_"+str(j)+".jpg",neg_img)
                        can_get_neg=True
                    wait_time=wait_time-1

#训练模型
def train_model(classify_path,num_max,max_size):
    pos_img=np.ones((num_max,max_size,max_size,3))
    neg_img=np.ones((num_max,max_size,max_size,3))
    i=0
    for img in os.listdir(classify_path+"pos"):
        print("read pos "+str(i)+"/"+str(num_max))
        pos_img[i]=cv2.imread(classify_path+"pos/"+img)
        i=i+1
    i=0
    for img in os.listdir(classify_path+"neg"):
        print("read neg "+str(i)+"/"+str(num_max))
        neg_img[i]=cv2.imread(classify_path+"neg/"+img)
        i=i+1

    img_data=np.concatenate((pos_img,neg_img))
    #数据预处理
    img_data=img_data.astype('float32')/255

    #制作标签
    labels=np.concatenate((np.ones((num_max,)),np.zeros((num_max,))))

    #打乱训练集数据
    indices=np.arange(img_data.shape[0])
    np.random.shuffle(indices)
    img_data= img_data[indices]
    labels= labels[indices]

    #搭建网络框架
    model=models.Sequential()
    model.add(layers.Conv2D(32,(3,3),activation='relu',input_shape=(max_size,max_size,3)))
    model.add(layers.MaxPool2D((2,2)))
    model.add(layers.Conv2D(64,(3,3),activation='relu'))
    model.add(layers.MaxPool2D((2,2)))
    model.add(layers.Conv2D(128,(3,3),activation='relu'))
    model.add(layers.MaxPool2D((2,2)))
    model.add(layers.Flatten())
    model.add(layers.Dense(1,activation='sigmoid'))
    model.summary()

    #编译
    model.compile(optimizer='rmsprop',
                  loss='binary_crossentropy',
                  metrics=['accuracy'])

    #拟合

    model.fit(img_data[0:int(2*num_max*0.7)],
              labels[0:int(2*num_max*0.7)],
              epochs=15,
              batch_size=16,
              validation_data=(img_data[int(2*num_max*0.7):2*num_max],labels[int(2*num_max*0.7):2*num_max]))

    #保存模型
    model.save("model.h5")

#错误分析
def wrong_analyse(classify_path,max_size):
    #建立目录
    if os.path.exists(classify_path+"wrong/")==False:os.makedirs(classify_path+"wrong/")

    model=models.load_model("model.h5")
    model.summary()

    if os.path.exists(classify_path+"wrong/is0but1")==False:os.makedirs(classify_path+"wrong/is0but1")
    if os.path.exists(classify_path+"wrong/is1but0")==False:os.makedirs(classify_path+"wrong/is1but0")
    #正例负例依次预测
    sum_wrong=0
    for img in os.listdir(classify_path+"pos"):
        pos_img=cv2.imread(classify_path+"pos/"+img)
        pos_img_use=np.reshape(pos_img,(1,max_size,max_size,3))/255
        if model.predict(pos_img_use)[0][0]<0.5:
            print("true 1 predict "+str(model.predict(pos_img_use)[0][0]))
            sum_wrong=sum_wrong+1
            cv2.imwrite(classify_path+"wrong/is1but0/"+img,pos_img)

    for img in os.listdir(classify_path+"neg"):
        neg_img=cv2.imread(classify_path+"neg/"+img)
        neg_img_use=np.reshape(neg_img,(1,max_size,max_size,3))/255
        if model.predict(neg_img_use)[0][0]>0.5:
            print("true 0 predict "+str(model.predict(neg_img_use)[0][0]))
            sum_wrong=sum_wrong+1
            cv2.imwrite(classify_path+"wrong/is0but1/"+img,neg_img)
    print("sum wrong = "+str(sum_wrong))

#滑动窗口预测
def win_predict(img_path,annotations_path,pre_name,max_size,threshold):
    model=models.load_model("model.h5")
    model.summary()
    #读入labels
    boxes=get_label(annotations_path+pre_name+".xml")
    #读入图像
    img=cv2.imread(img_path+pre_name+".jpg")
    draw_img=cv2.imread(img_path+pre_name+".jpg")
    #循环窗口生成
    for x in range(0,img.shape[0]-max_size,int(max_size/8)):
        for y in range(0,img.shape[0]-max_size,int(max_size/8)):
            #对该模块进行预测
            this_img=np.reshape(img[y:y+max_size,x:x+max_size,:],(1,max_size,max_size,3))/255
            this_label=model.predict(this_img)[0][0]
            print(str(y)+":"+str(x)+" confidence:"+str(this_label))

            if this_label>=threshold:
                print(this_label)
                #画框
                cv2.rectangle(draw_img,(x,y),(x+max_size,y+max_size),(0,255,255),1)
                #添加文本
                cv2.putText(draw_img,format(this_label,'.4f'),(x,y), cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,255,255),1)

    #画出真正的labels框
    for j in range(len(boxes)):
        #画框
        cv2.rectangle(draw_img,
                      (boxes[j]['xmin'],boxes[j]['ymin']),
                      (boxes[j]['xmax'],boxes[j]['ymax']),
                      (255,255,0),
                      1)

    #保存输出
    cv2.imwrite("final.jpg",draw_img)

#按要求制作不同的数据集
def make_different_datasets(pos_name_list):
    all_w=[]#统计全部的w
    all_h=[]#统计全部的h
    boxes_of_one_img=[]#统计每张图像的boxes数量
    white_area=[]#每张图二值化之后的白色区域做统计
    connected=[]#每张图的连通域统计
    for i in range(len(pos_name_list)):
        print(str(i)+"/"+str(len(pos_name_list)))
        boxes=get_label(annotations_path+pos_name_list[i]+".xml")#读入labels
        img=cv2.imread(img_path+pos_name_list[i]+".jpg")#读入图像

        boxes_of_one_img.append(len(boxes))#统计boxes数目
        _,binary = cv2.threshold(img[:,:,0],127,255 ,cv2.THRESH_BINARY)#二值化
        kernel = np.ones((5, 5), np.uint8)#膨胀核
        after_dilation = cv2.dilate(binary, kernel)#膨胀操作
        white_area.append((np.sum((binary/255)/3)))#统计二值图像中1面积
        connected_num,_ = cv2.connectedComponents(after_dilation,connectivity=8)#统计连通域个数
        connected.append(connected_num-1)

        for j in range(len(boxes)):
            all_w.append(boxes[j]['xmax']-boxes[j]['xmin'])#统计w
            all_h.append(boxes[j]['ymax']-boxes[j]['ymin'])#统计h

        #根据条件保存数据集
        save_datasets_path="new_datasets/over_3boxes/"
        if os.path.exists(save_datasets_path)==False:os.makedirs(save_datasets_path)
        if os.path.exists(save_datasets_path+"Annotations/")==False:
            os.makedirs(save_datasets_path+"Annotations/")
        if os.path.exists(save_datasets_path+"JPEGImages/")==False:
            os.makedirs(save_datasets_path+"JPEGImages/")
        if os.path.exists(save_datasets_path+"draw_boxes/")==False:
            os.makedirs(save_datasets_path+"draw_boxes/")

        if boxes_of_one_img[i]>=3:
            #保存xml文件
            copyfile(annotations_path+pos_name_list[i]+".xml",
                     save_datasets_path+"Annotations/"+pos_name_list[i]+".xml")#复制
            #图像保存
            copyfile(img_path+pos_name_list[i]+".jpg",
                     save_datasets_path+"JPEGImages/"+pos_name_list[i]+".jpg")#复制
            #画框保存
            for j in range(len(boxes)):
                cv2.rectangle(img,
                              (boxes[j]['xmin'],boxes[j]['ymin']),
                              (boxes[j]['xmax'],boxes[j]['ymax']),
                              (0,255,255,3))
            cv2.imwrite(save_datasets_path+"draw_boxes/"+pos_name_list[i]+".jpg",img)

    #--------画出统计图
    def draw_data(data,bins,title):
        all_w=np.array(data)
        plt.title(title)
        plt.hist(all_w,bins=bins,color='green',edgecolor='black',histtype='bar')
        plt.grid(linestyle='--', color="gray")
        plt.show()

    draw_data(all_w,50,"all_w")
    draw_data(all_h,50,"all_h")
    draw_data(boxes_of_one_img,14,"boxes_of_one_img")
    draw_data(white_area,50,"white_area")

    return connected


from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from ..forms import StartupRegisterForm, UserUpdateForm, StartupUpdateForm
from django.apps import apps
import googlemaps

Startup = apps.get_model('users', 'Startup')
Industry = apps.get_model('users', 'Industry')
Sector = apps.get_model('users', 'Sector')
Status = apps.get_model('users', 'Status')
Job = apps.get_model('users', 'Job')

# Create your views here.
def register_startup(request):
    if request.method == 'POST':
        form = StartupRegisterForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            messages.success(request, 'Din startup-bruker har blitt opprettet! Du kan nå logge inn.  ')
            return redirect('news-home')
        else:
            messages.warning(request, 'Registrering ikke vellykket. Prøv igjen.  ')
    else:
        form = StartupRegisterForm()
    return render(request, 'startups/register_startup.html', {'form': form})

# Requires login for access to profile.
@login_required
def profile(request):
    all_jobs = Job.objects.all()
    return render(request, 'startups/profile.html', {'all_jobs': all_jobs})

# Requires login for access to profile.
# Fetches form for editing in edit.
@login_required
def edit(request):
    if request.method == 'POST':
        u_form = UserUpdateForm(request.POST, instance=request.user)
        p_form = StartupUpdateForm(request.POST,
                                   request.FILES,
                                   instance=request.user.startup_profile)

        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request,'Din profil har blitt endret')
            return redirect('/startups/profile')

    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = StartupUpdateForm(instance=request.user.startup_profile)

    context = {
        'u_form': u_form,
        'p_form': p_form
    }
    return render(request, 'startups/edit.html', context)

def browse(request):
    all_startups = Startup.objects.all()
    all_industry = Industry.objects.all()
    all_sectors = Sector.objects.all()
    all_status = Status.objects.all()
    startup_filter = StartupFilter(request.GET, queryset=all_startups)
    return render(request, 'startups/browse.html', {'filter':startup_filter,'all_startups': all_startups, 'all_industry': all_industry,
                                                    'all_sectors': all_sectors, 'all_status': all_status})

def visit(request, pk):
    context = {
        'startup': Startup.objects.get(pk=pk),
        'all_jobs': Job.objects.all()
    }
    return render(request, 'startups/visit.html', context)


##
##  Programación en Python
##  ===========================================================================
##
##  Imprima la cantidad de registros por letra para la primera columna del
##  archivo `data.csv`, ordenados alfabeticamente por la letra.
##
##  Rta/
##  A,8
##  B,7
##  C,5
##  D,6
##  E,14
##
##  >>> Escriba su codigo a partir de este punto <<<
##
#cleanfile
import os
lines = ""
with open('data.csv') as f:
    s = f.read();
    s = s.replace('\t',',')
    s = s.replace(' ',',')
    lines+=s;
fo = open('out.csv','w');
fo.write(lines)
fo.close()
data = [ x.split(',')[0] for x in open('out.csv').readlines()];
data.sort()
from itertools import groupby
res = [key+','+str(len(list(group))) for key,group in groupby(data)]
for x in res:
    print(x)
os.remove('out.csv')


from pyspark.sql import SparkSession
import os

os.environ["SPARK_HOME"] = "/usr/local/spark/"

spark = SparkSession \
    .builder \
    .appName("Python Spark SQL basic example") \
    .config("spark.some.config.option", "some-value")\
    .config('spark.jars.packages',"org.elasticsearch:elasticsearch-spark-20_2.11:5.5.2")\
    .getOrCreate()

es_conf = {"es.read.metadata": "true","es.read.field.as.array.include" : "id, tweet"}

tweets = spark.read.format("org.elasticsearch.spark.sql").options(**es_conf).load("tweets/tweet_table")

tweets.count()
print('a')


"""empty message

Revision ID: bb8d5b41a64c
Revises: b8ceddd90187
Create Date: 2018-08-28 16:19:07.518299

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'bb8d5b41a64c'
down_revision = 'b8ceddd90187'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('user', sa.Column('validated', sa.Boolean(), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('user', 'validated')
    # ### end Alembic commands ###


import numpy as np
import matplotlib.pyplot as plt
import mltools as ml

iris = np.genfromtxt("data/iris.txt", delimiter = None)

Y = iris[: , -1]
X = iris[: ,0: -1]

X,Y = ml.shuffleData(X, Y)

Xtr, Xte, Ytr, Yte = ml.splitData(X, Y, 0.75)

for k in [1, 5, 10, 50]:
	knn = ml.knn.knnClassify(Xtr[:,0:2], Ytr, k)
	ml.plot.plotClassify2D(knn, Xtr[:,0:2], Ytr)
	plt.title('k = %i'%k)
	plt.show()


import numpy as np
from overrides import overrides
from typing import List
from ..dataset_reader import DatasetReader
from ..compound_dataset_reader import CompoundDatasetReader, CompoundDatasetEpochIterator

class CombinedDatasetReaderIterator(CompoundDatasetEpochIterator):
	def __init__(self, reader):
		self.baseIterators = [CompoundDatasetEpochIterator(baseReader) for baseReader in reader.baseReaders]
		self.ix = -1
		self.Mappings = self.getMappings()
		self.len = len(self)

	def getMappings(self):
		Mappings = np.zeros((len(self), 2), dtype=np.uint32)
		ix = 0
		for i in range(len(self.baseIterators)):
			g = self.baseIterators[i]
			Range = np.arange(len(g))
			Mappings[ix : ix + len(g), 0] = np.repeat([i], len(g))
			Mappings[ix : ix + len(g), 1] = Range
			ix += len(g)
		return Mappings

	@overrides
	def __len__(self) -> bool:
		return sum([len(x) for x in self.baseIterators])

	@overrides
	def __getitem__(self, ix):
		readerIx, readerInnerIx = self.Mappings[ix]
		return self.baseIterators[readerIx].__getitem__(readerInnerIx)

	@overrides
	def __getattr__(self, key):
		X = [getattr(baseIterator, key) for baseIterator in self.baseIterators]
		return X

# @brief A composite dataset reader that has a base reader attribute which it can partially use based on the percent
#  defined in the constructor
class CombinedDatasetReader(CompoundDatasetReader):
	def __init__(self, baseReaders:List[DatasetReader]):
		# super().__init__(baseReader)
		assert len(baseReaders) > 1, "Must provide a list of DatasetReaders!"
		firstReader = baseReaders[0]
		assert isinstance(firstReader, DatasetReader)
		for reader in baseReaders[1 : ]:
			assert isinstance(reader, DatasetReader)
			assert reader.datasetFormat == firstReader.datasetFormat, "All readers must provide same DatasetFormat!"

		DatasetReader.__init__(self, dataBuckets=firstReader.datasetFormat.dataBuckets, \
			dimGetter=firstReader.datasetFormat.dimGetter, dimTransform=firstReader.datasetFormat.dimTransform)
		self.baseReaders = [CompoundDatasetReader(reader) for reader in baseReaders]
		self.baseReader = self.baseReaders

	@overrides
	def iterateOneEpoch(self):
		return CombinedDatasetReaderIterator(self)

	@overrides
	def __len__(self):
		return sum([len(reader) for reader in self.baseReaders])

	@overrides
	def __str__(self) -> str:
		summaryStr = "[CombinedDatasetReader]"
		summaryStr += "\n - Num datasets: %d" % (len(self.baseReaders))
		for i, reader in enumerate(self.baseReaders):
			summaryStr += "\n----------- %d -----------" % (i + 1)
			summaryStr += "\n%s" % str(reader)
		return summaryStr


import urllib.request
import urllib.parse
from bs4 import BeautifulSoup

defaultURL="https://openapi.naver.com/v1/search/kin.xml"
sort='&sort=sim'
start='&start=1'
display='&display=100'
search=str(input("검색어 : "))
query='?query='+urllib.parse.quote_plus(search)
fullURL = defaultURL + query + display + start + sort
print(fullURL)

file=open("c:\\python34\\crawl\\jisikin\\%s.txt"%search,"w",encoding='utf-8')
headers={
    'Most':'openapi.naver.com',
    'User-Agent':'curl/7.43.0',
    'Accept':'*/*',
    'Content-Type':'application/xml',
    'X-Naver-Client-Id':'BqGt9k2AGN36MSJWqznB',
    'X-Naver-Client-Secret':'_CJLTTvzol'
    }

req=urllib.request.Request(fullURL,headers=headers)
f=urllib.request.urlopen(req)
resultXML=f.read()
xmlsoup=BeautifulSoup(resultXML,'html.parser')
items=xmlsoup.find_all('item')

for item in items:
    file.write('-------------------------------\n')
    file.write(' 시간 : ')
    file.write(' 제목 : '+ item.title.get_text(strip=1)+'\n')
    file.write(' 내용 : '+ item.description.get_text(strip=1)+'\n')
    file.write(' 링크 : '+ item.link.get_text(strip=1)+'\n')#본문의 링크를 가져오기 위해
    file.write('-------------------------------\n')

file.close()