갈루아의 반서재

국가별로 WOE ID 를 조회하여 트위터를 통해 나타난 트렌드를 알아보고 세계 트렌드와 비교해보는 페이지를 만들어보자.



1. 먼저 국가별 WOE ID 조회에 필요한 패키지를 설치한다.


yweather 라는 패키지(https://pypi.python.org/pypi/yweather/0.1)를 이용하여 국가별 WOE ID 를  찾을 수 있는데, yweather 는 Yahoo! Weather RSS feed 인터페이스를 제공하기 위한 파이썬 모듈이다.


yweather 설치


1
2
3
4
5
6
7
8
9
envalpha)root@localhost:~/# pip install yweather
Collecting yweather
  Downloading yweather-0.1.1.tar.gz
Building wheels for collected packages: yweather
  Running setup.py bdist_wheel for yweather ... done
  Stored in directory: /root/.cache/pip/wheels/d3/21/ae/f755a5cb9780b07b825737a3b325c5e98afc18b17bd61b2762
Successfully built yweather
Installing collected packages: yweather
Successfully installed yweather-0.1.1
cs



2. django-countries 3.4.1 패키지 설치

위의 yweather 는 국가 이름을 WOE ID 로 변환해주는 패키지이고, 이제 필요한 것은 정형화된 국가 이름 목록이다. 이를 위해 폼에서 국가를 선택할 수 있도록 django-countries 3.4.1 패키지

(https://pypi.python.org/pypi/django-countries)를 설치한다.


django-countries 3.4.1설치


1
2
3
4
5
6
(envalpha)root@localhost:~/# pip install django-countries
Collecting django-countries
  Downloading django_countries-3.4.1-py2.py3-none-any.whl (444kB)
    100% |████████████████████████████████| 450kB 1.0MB/s
Installing collected packages: django-countries
Successfully installed django-countries-3.4.1
cs


settings.py > INSTALLED_APPS 에 설치한 django_countries 추가


1
2
3
4
5
6
7
8
9
10
11
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    'blog',
    'django_countries'
]
cs



3. 국가 선택을 위한 Form 을 만든다.


forms.py

1
2
3
4
from django_countries import countries
 
class GetWOEIDForm(forms.Form):
    country= forms.ChoiceField(countries)
cs


4. html 파일에 아래 form 을 삽입한다.


blog/twitter_woeid_form.html 파일에 아래 코드를 삽입

1
2
3
4
5
<form role = "form" action="" method="post">                    
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
cs


5. 이전 포스트에서 만든 파이썬 파일을 수정한다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from django.shortcuts import render_to_response, render, redirect
from blog.forms import GetWOEIDForm
import requests
import twitter
import yweather
   
CONSUMER_KEY = '*******************'
CONSUMER_SECRET =  '*******************'
OAUTH_TOKEN = '*******************'
OAUTH_TOKEN_SECRET =  '*******************'
auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,  CONSUMER_KEY, CONSUMER_SECRET)
twitter_api = twitter.Twitter(auth=auth)
 
def get_twitter_trend_form(request):
    form_class = GetWOEIDForm
 
    if request.method == 'POST':
        form = form_class(data=request.POST)
 
        if form.is_valid():
            WORLD_WOE_ID = 1
            countryname = request.POST.get('country','')
            client = yweather.Client()
            woeid = client.fetch_woeid(countryname)
    
            world_trends = twitter_api.trends.place(_id=WORLD_WOE_ID)
            country_trends = twitter_api.trends.place(_id=woeid)
         
            world_trends_set = set ([trend['name'for trend in world_trends[0]['trends']])
            country_trends_set = set ([trend['name'for trend in country_trends[0]['trends']]) 
            common_trends_set = world_trends_set.intersection(country_trends_set)
                    
            dic = { 'twitter_api': twitter_api,
                        'world_trends_set': world_trends_set,
                       'country_trends_set' : country_trends_set,
                       'common_trends_set' : common_trends_set,
                       'countryname' : countryname, 
                       'woeid': woeid, 
                   }
    
        return render_to_response('blog/twitter_trends.html',dic,)
    return render (request, 'blog/twitter_woeid_form.html', {'form': form_class,})    
cs



6. 결과를 보여줄 페이지 작성


blog/twitter_trends.html 파일에 아래 코드를 삽입

1
2
3
4
5
6
7
8
9
10
11
12
13
                <div class="panel panel-primary">
                    <div class="panel-heading">WORLD TRENDS</div>
                    <div class="panel-body">{{ world_trends_set }}</div>
                </div>
                <div class="panel panel-primary">
                    <div class="panel-heading">{{ countryname }} TRENDS (WOE ID = {{ woeid }})</div>
                    <div class="panel-body">{{ country_trends_set }}</div>
                </div>
                <div class="panel panel-info-dark">
                    <div class="panel-heading">COMMON TRENDS</div>
                    <div class="panel-body">{{ common_trends_set }}</div>
                </div>
 
cs



7. 결과물 화면