-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeed_pocket.py
More file actions
225 lines (198 loc) · 7.86 KB
/
feed_pocket.py
File metadata and controls
225 lines (198 loc) · 7.86 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import requests, json, feedparser, os, random
import datetime as dt
import peewee
from peewee import *
from twitter import *
import urllib.parse as urlparse
from sys import argv
if 'CLEARDB_DATABASE_URL' not in os.environ:
from playhouse.sqlite_ext import SqliteExtDatabase
access_token = os.environ["access_token"]
consumer_key = os.environ["consumer_key"]
twitter_access_token = os.environ["twitter_access_token"]
twitter_access_secret = os.environ["twitter_access_secret"]
twitter_key = os.environ["twitter_key"]
twitter_secret = os.environ["twitter_secret"]
# Maximum number of unread articles to be shown in your Pocket
max_articles = 100
countries = [
{
"name": "Czech",
"twitter_query": '("Covid" AND "algoritmus") OR ("politika" AND "algoritmus") OR ("rozhodnutí" AND "algoritmus") OR ("mesto" AND "algoritmus") OR "prediktivní policie" OR "rozpoznávání tváře" -Čína -Pixel -Samsung',
"rss_feeds": []
},
{
"name": "Danish",
"twitter_query": '("Covid" AND "algoritme") OR ("afgørelse" AND "algoritme") OR ("politik" AND "algoritme") OR ("by" AND "algoritme") OR "forudsigeligt politi" OR "ansigtsgenkendelse" -kina -Pixel -Samsung',
"rss_feeds": []
},
{
"name": "Dutch",
"twitter_query": '("besluit" AND "algoritme") OR ("politiek" AND "algoritme") OR ("stad" AND "algoritme") OR "voorspellende politie" OR "Gezichtsherkenning" OR "risicoprofilering" -china -Pixel -Samsung',
"rss_feeds": [
"https://bijvoorbaatverdacht.nl/feed/"
]
},
{
"name": "English",
"twitter_query": '("Covid" AND "automated") OR "iborderctrl" OR "automated decision making" OR "automated decision-making" OR "algocracy" OR "algorithmic governance" OR ("AI" and "job interview") -china -Pixel -Samsung',
"rss_feeds": []
},
{
"name": "French",
"twitter_query": '("algorithme" AND "Covid") OR ("automatisé" AND "Covid") OR ("algorithme" AND "travail") OR ("politique" AND "algorithme") OR ("social" AND "algorithme") OR ("ville" AND "algorithme") OR "police prédictive" OR "décision automatisée" -chine -Pixel -Samsung',
"rss_feeds": [
"https://technopolice.fr/feed/"
]
},
{
"name": "German",
"twitter_query": '("algorithmus" AND "Covid") OR ("algorithmus" AND "arbeitsmarkt") OR ("algorithmus" AND "politik") OR ("algorithmus" AND "stadt") OR ("algorithmus" AND "sozial") OR ("entscheidung" AND "algorithmus") -china -Pixel -Samsung',
"rss_feeds": [
"https://algorithmenethik.de/feed/"
,"https://politicalinfluencers.wordpress.com/feed/"
]
},
{
"name": "Hungarian",
"twitter_query": '("automatizált" AND "Covid") OR ("automatizált" AND "algoritmus") OR ("döntés" AND "algoritmus") OR ("város" AND "algoritmus") OR "prediktív rendőrség" OR "arcfelismerő" -Kína -Pixel -Samsung',
"rss_feeds": []
},
{
"name": "Italian",
"twitter_query": '("algoritmo" AND "Covid") OR ("algoritmo" AND "decisione") OR ("algoritmo" AND "politica") OR ("algoritmo" AND "città") OR "polizia predittiva" -cina -Pixel -Samsung',
"rss_feeds": []
},
{
"name": "Polish",
"twitter_query": '("algorytm" AND "decyzja") OR ("algorytm" AND "polityka") OR ("algorytm" AND "miasto") OR "prognozowanie policji" OR "rozpoznawania rysów twarzy" -Chiny -Pixel -Samsung',
"rss_feeds": [
"https://epf.org.pl/en/projects/feed/"
# "https://www.sztucznainteligencja.org.pl/tematy/ludzie/spoleczenstwo/feed/"
]
},
{
"name": "Romanian",
"twitter_query": '("algoritmul" AND "decizie") OR ("algoritmul" AND "politică") OR ("oraș" AND "algoritmul") OR "previziune poliție" OR "recunoaşterea facială" -china -Pixel -Samsung',
"rss_feeds": []
},
{
"name": "Slovak",
"twitter_query": '("politický" AND "algoritmus") OR ("rozhodnutie" AND "algoritmus") OR ("mesto" AND "algoritmus") OR "prediktívne policajné" -Čína -Pixel -Samsung',
"rss_feeds": []
},
{
"name": "Serbian",
"twitter_query": '("алгоритам" AND "политичка") OR ("вештачка интелигенција" AND "град") OR ("Србија" AND "вештачка интелигенција") OR "предвидивање полиције" -Кина -Pixel -Samsung',
"rss_feeds": [
"https://www.sharefoundation.info/en/category/news/feed/"
]
},
{
"name": "Spanish",
"twitter_query": '("decisión" AND "algoritmo") OR ("política" AND "algoritmo") OR ("ciudad" AND "algoritmo") OR "policía predictiva" -china -Pixel -Samsung',
"rss_feeds": []
},
{
"name": "Swedish",
"twitter_query": '("beslut" AND "algoritm") OR ("politik" AND "algoritm") OR ("stad" AND "algoritm") OR "prediktiv polis" OR "ansiktsigenkänning" -kina -Pixel -Samsung',
"rss_feeds": []
}
]
# List of websites that you do not want to read from.
# Removing Twitter ensures that you won't be given linked tweets.
blacklisted_urls = [
"twitter.com" # Embedded tweets
,"freie-welt.eu" # Probably a content farm
,"stohl.de" # Looks like automated link collection
,"tweetedtimes.com" # List of links
,"linkedin.com" # Never read something interesting there
,"newslocker.com" # Low quality aggregator
,"medrxiv.org" # Too many preprints
]
if 'CLEARDB_DATABASE_URL' in os.environ:
url = urlparse.urlparse(os.environ['CLEARDB_DATABASE_URL'])
db = peewee.MySQLDatabase(url.path[1:], host=url.hostname, user=url.username, passwd=url.password)
else:
db = SqliteExtDatabase('links.db')
class BaseModel(Model):
class Meta:
database = db
class Link(BaseModel):
url = CharField()
class Meta:
primary_key = CompositeKey('url')
db.connect(reuse_if_open=True)
db.create_tables([Link], safe=True)
def checkDuplicate(link):
try:
# Shorten link to 255 chars
link = link[:254]
Link.create(url = link)
return True
except IntegrityError:
return False
pass
def add(link, country_tag = None):
if checkDuplicate(link):
url = "https://getpocket.com/v3/add"
r = requests.post(url, data={"url": link, "tags": country_tag, "consumer_key": consumer_key, "access_token": access_token})
data = json.loads(r.text)
if data["status"] == 1:
print("Added %s. Word count %s" % (data["item"]["title"], data["item"]["word_count"]))
return data["item"]
else:
return False
else:
return False
def checkRSS():
successfully_parsed = 0
for country in countries:
country_tag = country["name"]
for rss_feed in country["rss_feeds"]:
d = feedparser.parse(rss_feed)
for entry in d.entries[0:5]:
add(entry.link, country_tag)
successfully_parsed += 1
return successfully_parsed
def checkTwitter():
auth = OAuth(twitter_access_token, twitter_access_secret, twitter_key, twitter_secret)
t = Twitter(auth=auth)
urls = []
for country in countries:
response = t.search.tweets(q="filter:links %s" % country["twitter_query"], count=200)
for tweet in response["statuses"]:
if "urls" in tweet["entities"]:
for url in tweet["entities"]["urls"]:
if not any(x in url["expanded_url"] for x in blacklisted_urls):
# Remove anything after the ? in the URL (can contain twitter or FB details)
final_url = url["expanded_url"].split('?', 1)[0]
# Reject domains, i.e. URLS with less than 4 / (because http://domain.tld/)
if final_url.count('/') <= 3:
pass
else:
urls.append({"url": final_url, "country_tag": country["name"]})
return urls
def countUnreads():
url = "https://getpocket.com/v3/get"
r = requests.post(url, data={"consumer_key": consumer_key, "access_token": access_token})
data = json.loads(r.text)
unread_items = data["list"]
return len(unread_items)
def addLinks():
urls = checkTwitter()
unread_items = countUnreads()
for url in urls:
if unread_items <= max_articles:
if add(url["url"], url["country_tag"]):
unread_items += 1
if __name__ == "__main__":
force = False
for arg in argv:
if arg == "force":
force = True
now = dt.datetime.utcnow()
current_hour = now.hour
if current_hour % 8 == 0 or force:
addLinks()
checkRSS()