I was implementing RSS regular tweets with ifttt, but I made it myself because the restrictions were strict.
https://hub.docker.com/r/hashito/tweetrss
docker run -it --rm \
-e RSS_URL=https://news.google.com/rss/search?hl=ja&gl=JP&ceid=JP:ja&q=twitter \
-e CONSUMER_KEY=x \
-e CONSUMER_SECRET=x \
-e ACCESS_TOKEN=x \
-e ACCESS_TOKEN_SECRET=x \
-e 'ADD_TEXT="#rss2tweet"' \
hashito/tweetrss
From above
You can do it by inserting.
If you want to change the Tweet cycle, play with SCAN_SPAN
and TWEET_DELAY
. (Both are seconds)
Be careful as you get angry if you tweet too much ...
Also, if you have a cash value internally and rebuild it, you will forget how far you read RSS last time.
Let's create a file like the one below and mount it.
contents
{}
-v {you chash file}:/root/cash
(It's awkward around here ... I'll fix it someday)
The source is simple. Read RSS and Tweet it regularly. RSS retains the ID and does not target the same ID. If the same ID is not received for a certain period of time, the ID will be discarded.
import feedparser
import json
import os
import time
import datetime
import json
from requests_oauthlib import OAuth1Session
RSS_URL=os.environ["RSS_URL"]
CONSUMER_KEY=os.environ["CONSUMER_KEY"]
CONSUMER_SECRET=os.environ["CONSUMER_SECRET"]
ACCESS_TOKEN=os.environ["ACCESS_TOKEN"]
ACCESS_TOKEN_SECRET=os.environ["ACCESS_TOKEN_SECRET"]
CASH_FILE=os.environ["CASH_FILE"]
ADD_TEXT=os.environ["ADD_TEXT"]
SCAN_SPAN=int(os.environ["SCAN_SPAN"])
TWEET_DELAY=int(os.environ["TWEET_DELAY"])
def cash_read():
with open(CASH_FILE) as f:
return json.loads(f.read())
def cash_write(data):
with open(CASH_FILE,mode="w") as f:
f.write(json.dumps(data,ensure_ascii=False))
def read_rss():
return feedparser.parse(RSS_URL)
def send_tweet(text):
twitter = OAuth1Session(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET) #Authentication process
url = "https://api.twitter.com/1.1/statuses/update.json" #Tweet post endpoint
params = {"status" : text}
res = twitter.post(url, params = params) #post send
return res.status_code == 200
if(__name__ == '__main__'):
cash=cash_read()
print("cash:",cash)
while(1):
tm=datetime.datetime.now().timestamp()
rss=read_rss()
for i in rss["entries"]:
if(not i["id"] in cash):
print("tweet:",f"{i['title']} {i['link']} {ADD_TEXT}")
send_tweet(f"{i['title']} {i['link']} {ADD_TEXT}")
time.sleep(TWEET_DELAY)
cash[i["id"]]=tm
cash_write(cash)
for k in list(cash.keys()):
if((cash[k]+60)<tm):
print("delete:",k,cash[k],tm)
cash.pop(k)
time.sleep(SCAN_SPAN)
docker file
This is also simple.
Install requirements.txt
using the image of python3.
It's like preparing environment variables and starting them.
FROM python:3
COPY . /root/
RUN pip install -r /root/requirements.txt
ENV RSS_URL=https://news.google.com/rss/search?hl=ja&gl=JP&ceid=JP:ja&q=twitter \
CONSUMER_KEY=x \
CONSUMER_SECRET=x \
ACCESS_TOKEN=x \
ACCESS_TOKEN_SECRET=x \
CASH_FILE=/root/cash \
ADD_TEXT="#rss2tweet" \
SCAN_SPAN=600 \
TWEET_DELAY=150
CMD ["python","/root/main.py"]
Recommended Posts