-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (60 loc) · 2.57 KB
/
main.py
File metadata and controls
74 lines (60 loc) · 2.57 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
# SOLD OUT 2 Market Infomation Bot for Twitter
# Author: Kjuman Enobikto
# License: MIT License
import json
import os
import time
from configparser import ConfigParser
from requests_oauthlib import OAuth1Session
from SO2MI.Client import client
from SO2MI.Log import logger
if os.path.isfile("config.ini"):
config = ConfigParser()
config.read("config.ini")
consumerKey = config["keys"]["consumerKey"]
consumerSecret = config["keys"]["consumerSecret"]
accessToken = config["keys"]["accessToken"]
accessTokenSecret = config["keys"]["accessTokenSecret"]
tagStr = config["misc"]["hashtagStr"]
accStr = config["misc"]["accountStr"]
else:
consumerKey = os.environ.get("consumerKey")
consumerSecret = os.environ.get("consumerSecret")
accessToken = os.environ.get("accessToken")
accessTokenSecret = os.environ.get("accessTokenSecret")
tagStr = os.environ.get("hashtagStr")
accStr = os.environ.get("accountStr")
stream = "https://stream.twitter.com/1.1/statuses/filter.json"
tweet = "https://api.twitter.com/1.1/statuses/update.json"
if __name__ == "__main__":
# メイン関数定義
def mainfunc():
print("Logged in!")
tracker = {"track": f"#{tagStr}"}
while True:
resGet = session.post(stream, params=tracker, stream=True)
for line in resGet.iter_lines():
if line.decode("utf-8") == "Exceeded connection limit for user":
logger("制限中です")
time.sleep(1000)
continue
if line.decode("utf-8") != "":
content = json.loads(line.decode("utf-8"))
command = content["text"]
tweetId = content["id"]
logger(f"{content['user']['name']} (@{content['user']['screen_name']}): {content['text']}")
if "RT" not in command:
reply = client(command)
if reply != "":
mention = {"status": reply, "in_reply_to_status_id": tweetId, "auto_populate_reply_metadata": True}
resPost = session.post(tweet, params=mention)
try:
logger(resPost["errors"][0]["message"], "error")
except:
logger("投稿完了しました")
pass
# セッション生成
session = OAuth1Session(consumerKey, consumerSecret, accessToken, accessTokenSecret)
# 実行
mainfunc()