# -*- coding: utf-8 -*-
"""方法:

如果random 取电影id, 先判断sended的状态, 如果为0, 说明之前还没发过,
然后查询是否还有相同电影id的条目, 如果有, 则取出其下载地址, 使用sina
短网址api 压缩多个下载地址, 发布微博, 然后使已发送成功的条目的sended字段置1.

"""


activate_this = 'venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

from sqlite3 import dbapi2 as sqlite3
from strs2weibo import retrieve_image, construct_status, weibo_upload


_db = 'data/production.db'


def fetch_data():
    with sqlite3.connect(_db) as conn:
        cursor = conn.cursor()
        cursor.execute("""
        select douban_title, download_url, douban_url, lpic_url, douban_id from movie
        where douban_id in (select douban_id from movie order by random() limit
        1)""")
        rows = cursor.fetchall()
        if len(rows) > 1:
            print '-' * 40
            douban_title = rows[0][0]
            douban_url = rows[0][2]
            lpic_url = rows[0][3]
            douban_id = rows[0][4]
            download_url = [row[1] for row in rows]
        else:
            douban_title, download_url, douban_url, lpic_url, douban_id = rows[0]
            download_url = [download_url]
    return douban_title, download_url, douban_url, lpic_url, douban_id


if __name__ == '__main__':
    topic = u'电影传送门'
    douban_title, download_url, douban_url, lpic_url, douban_id = fetch_data()
    lpic = retrieve_image(lpic_url)
    status = construct_status(topic, douban_title, douban_url, *download_url)
    response =  weibo_upload(status=status, pic=lpic)
    if response.get('created_at'):
        with sqlite3.connect(_db) as conn:
            cursor = conn.cursor()
            cursor.execute("UPDATE movie SET sended=1 WHERE douban_id=?",\
                    (douban_id,))
            print "\nWeibo sented successfully !\n","douban_id:", douban_id
    elif response.get('error_code'):
        print response.get('error_code'), response.get('error')
    else:
        print "Unknown Error!"


@@ -408,16 +408,17 @@
 '%0A%0A%0Adef 
+_
 fetch_da
@@ -423,16 +423,78 @@
 data():%0A
+    %22%22%22Fetch data from db for preparing tweeting message %22%22%22%0A%0A
     with
@@ -567,68 +567,38 @@
     
-cursor.execute(%22%22%22%0A        select douban_title, download_url
+query = %22%22%22SELECT douban_title
 , do
@@ -631,32 +631,40 @@
 _id 
-from movie%0A        where
+FROM%0A                movie WHERE
  dou
@@ -678,14 +678,14 @@
 in (
-select
+SELECT
  dou
@@ -695,56 +695,106 @@
 _id 
-from
+FROM
  movie 
-order by random() limit%0A        1)%22%22%22
+ORDER BY%0A                random() LIMIT 1) and sended=0%22%22%22%0A        cursor.execute(query
 )%0A  
@@ -1287,33 +1287,18 @@
 d%0A%0A%0A
-if __name__ == '__
+def 
 main
-__'
+()
 :%0A  
@@ -1382,16 +1382,17 @@
 an_id = 
+_
 fetch_da
@@ -2005,8 +2005,47 @@
 rror!%22%0A%0A
+%0Aif __name__ == '__main__':%0A    main()%0A

clean code