-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvote.py
103 lines (85 loc) · 4.17 KB
/
vote.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- coding: utf-8 -*-
import praw
import requests
from bs4 import BeautifulSoup
import pandas as pd
import bot_login
import json
import time
class VoteCount():
"""docstring for VoteCOunt"""
def __init__(self):
self.portali_dic = {"vecernji.hr": "Večernji",
"amp.index.hr": "Index",
"www.index.hr": "Index",
"www.jutarnji.hr": "Jutarnji",
"www.rtl.hr": "RTL",
"24sata.hr": "24 sata",
"hr.n1info.com": "N1 info",
"telegram.hr": "telegram",
"hrvatska-danas.com": "Hrvatska-Danas",
"dnevnik.hr": "Dnevnik.hr",
"slobodnadalmacija.hr": "Slobodna Dalmacija",
"glasistre.hr": "Glas Istre",
"nacional.hr": "Nacional",
"www.net.hr": "Net.hr",
"tportal.hr": "Tportal",
"sportnet.rtl.hr": "Sportnet RTL",
"maxportal.hr": "MaxPortal",
"bug.hr": "bug.hr"
"dalmatinskiportal.hr": "Dalmatinski Portal"
}
with open('user.json') as f:
self.user_dic = json.load(f)
# self.df = pd.DataFrame(
# columns=["sub_id", "submission url", "portal", "good vote", "bad vote"])
self.df = pd.read_csv("out.csv", header=0, index_col=0)
def listen(self):
for comment in r.subreddit("bot_protiv_clickbait").stream.comments(pause_after=2):
if comment is None:
time.sleep(600)
break
try:
if "!clickbait" in comment.body:
if comment.author not in self.user_dic[comment.link_id[3:]]:
# if comment.author not in self.user_df.loc[self.user_df.sub_id ==comment.link_id[3:]]:
self.df.loc[self.df.sub_id == comment.link_id[3:], ["bad vote"]] += 1
self.user_dic[comment.link_id[3:]].append(str(comment.author))
self.df.to_csv("out.csv")
with open('user.json', 'w') as f:
json.dump(self.user_dic, f)
if "!notclickbait" in comment.body:
if comment.author not in self.user_dic[comment.link_id[3:]]:
self.df.loc[self.df.sub_id == comment.link_id[3:], ["good vote"]] += 1
self.user_dic[comment.link_id[3:]].append(str(comment.author))
self.df.to_csv("out.csv")
with open('user.json', 'w') as f:
json.dump(self.user_dic, f)
except KeyError:
self.user_dic[comment.link_id[3:]] = []
continue
def append2df(self):
for submission in r.subreddit("bot_protiv_clickbait").stream.submissions(pause_after=2):
if submission is None:
time.sleep(600)
break
for key in self.portali_dic:
if key in submission.url:
if submission.id not in self.df["sub_id"].values:
s_goodvote = 0
s_badvote = 0
data = pd.Series({"sub_id": submission.id,
"submission url": submission.url,
"portal": self.portali_dic[key],
"good vote": s_goodvote,
"bad vote": s_badvote})
self.df.loc[len(self.df)] = data
self.user_dic[submission.id] = []
self.df.to_csv("out.csv")
with open('user.json', 'w') as f:
json.dump(self.user_dic, f)
r = bot_login.login()
v = VoteCount()
while True:
v.append2df()
v.listen()