트위터 피드, 이미지 자동 업로더

 

1. 파이썬 가상환경설정 (virtualenv)

$virtualenv --python=python3.6 ./twt_img

$cd ./twt_img

$source bin/activate

2. 파이썬 라이브러리 설치 환경 파일 (requirements.txt)

---

beautifulsoup4

requests

tweepy

lxml

image

---

3. 파이썬 라이브러리 설치

$pip3 install -r requirements.txt

4. 트위트 토큰 설정 파일 (config.ini)

---

[version]

sdate = 2021.08.02

edate = 2021.10.01

[twitter@lonbekim]

consumer_key = GmnIDUxxxxi1gWU7dOxxxxD

consumer_secret = vbWaUKyp9qqbxxxxOlirOnbBghnxxxxxs7Xjiu8Ml0IALnw2

access_token = 3238214298-sBKRuDxxxx1o0dwdv6xxxxWXfqergPJmMGCo6

access_secret = OQcsxxxxgIZyusxxxxU0sKXmKP1sg4yyqIVvg4Pk

---

5. 실행 파일 (posting.py)

---

# -*- coding: utf-8 -*-

import os

import re

import time

import tweepy

import requests

import configparser

from bs4 import BeautifulSoup

from datetime import datetime

CNF_INI = '/home/ubuntu/twt_img/config.ini'

ERR_URI = '/home/ubuntu/twt_img/'

config = configparser.ConfigParser()

config.read(CNF_INI)

CKEY = config['twitter@lonbekim']['consumer_key']

CSECRET = config['twitter@lonbekim']['consumer_secret']

ATOKEN = config['twitter@lonbekim']['access_token']

ASECRET = config['twitter@lonbekim']['access_secret']

def send_twitter():

message = '@lonbekim\n#앤비팁스\n#케이브업\n'

auth = tweepy.OAuthHandler(CKEY, CSECRET)

auth.set_access_token(ATOKEN, ASECRET)

api = tweepy.API(auth)

#api.update_status(message)

photo = '/home/ubuntu/twt_image/caveup1.jpg'

api.update_with_media(status=message, filename=photo)

try:

send_twitter()

except Exception as e:

with open(ERR_URI+'error.log','a') as file:

file.write('{} You got an error: {}\n'.format(datetime.today().strftime('%Y-%m-%d %H:%M:%S'),str(e)))

---

6. 실행 명령

$python3 posting.py

7. 확인 (트위트 계정에 이미지 피드 업로드 확인)



댓글

이 블로그의 인기 게시물

[LINUX] CentOS 부팅시 오류 : UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY

[MSSQL] 데이터베이스가 사용 중이어서 배타적으로 액서스할 수 없습니다

구글코랩) 안전Dream 실종아동 등 검색 오픈API 소스를 공유합니다. (구글드라이브연동, 이미지 수집 소스)