@@ -0,0 +1,2959 @@
+#!/usr/bin/env python3%0A# coding: utf-8%0A%0Aimport os%0Aimport json%0Aimport logging%0A%0Aimport requests%0Afrom requests.adapters import HTTPAdapter%0A%0Aimport tweepy%0Afrom tweepy.models import Status%0A%0Aconfig_file = '/home/everpcpc/config/twitter.json'%0A%0Awith open(config_file, 'r', encoding='utf-8') as f:%0A    conf = json.load(f)%0A%0ADOWNLOAD_PATH = conf%5B'DOWNLOAD_PATH'%5D%0A%0Aauth = tweepy.OAuthHandler(conf%5B'CONSUMER_KEY'%5D, conf%5B'CONSUMER_SECRET'%5D)%0Aauth.set_access_token(conf%5B'ACCESS_TOKEN'%5D, conf%5B'ACCESS_TOKEN_SECRET'%5D)%0A%0Alogger = logging.getLogger('twitter')%0Alogging.basicConfig(level=logging.INFO,%0A                    format='%25(name)s - %25(levelname)s - %25(message)s')%0Alogging.getLogger('requests.packages.urllib3.connectionpool').setLevel(logging.WARN)%0A%0Asess = requests.Session()%0Asess.mount('https://', HTTPAdapter(max_retries=3))%0Aapi = tweepy.API(auth)%0A%0A%0Aclass PCStreamListener(tweepy.StreamListener):%0A    def on_data(self, raw_data):%0A        data = json.loads(raw_data)%0A        if 'event' in data:%0A            event = data%5B'event'%5D%0A            logger.info('get event %25s' %25 event)%0A            event_fn = getattr(self, 'on_%25s' %25 event, None)%0A            status = Status.parse(self.api, data%5B'target_object'%5D)%0A            if event_fn is None:%0A                logger.warn('%25s is not supported' %25 event)%0A            else:%0A                if event_fn(status) is False:%0A                    return False%0A        else:%0A            logger.info('new timeline item')%0A%0A    def on_favorite(self, status):%0A        self.process_image(status, 'download')%0A%0A    def on_unfavorite(self, status):%0A        self.process_image(status, 'delete')%0A%0A    def process_image(self, status, type_):%0A        medias = status.entities.get('media', %5B%5D)%0A        logger.info('%5B%25s%5D', status.text.replace('%5Cn', ' '))%0A        for media in medias:%0A            if media%5B'type'%5D == %22photo%22:%0A                url = media%5B'media_url_https'%5D%0A                filename = url.split('/')%5B-1%5D%0A                full_path = os.path.join(DOWNLOAD_PATH, filename)%0A                if type_ == 'download':%0A                    if os.path.exists(full_path):%0A                        logger.info(%22%25s exists%22 %25 filename)%0A                    else:%0A                        logger.info(%22--%3E Downloading %25s%22 %25 url)%0A                        r = sess.get(url, stream=True, timeout=5)%0A                        with open(full_path, 'wb') as f:%0A                            for chunk in r.iter_content(chunk_size=1024):%0A                                if chunk:%0A                                    f.write(chunk)%0A                elif type_ == 'delete':%0A                    if os.path.exists(full_path):%0A                        logger.info('--%3E Deleting %25s' %25 filename)%0A                        os.remove(full_path)%0A                    else:%0A                        logger.info('%25s absent, ignore.' %25 filename)%0A%0A%0Aif __name__ == '__main__':%0A    psl = tweepy.Stream(auth=api.auth, listener=PCStreamListener())%0A    psl.userstream(_with='everpcpc', async=True)%0A

add twitter_download_fav_pics.py