-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython3ports.py
120 lines (102 loc) · 3.97 KB
/
python3ports.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
# Copyright Josef Assad 2012
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import urllib2
import BeautifulSoup
import sqlite3
from sqlite3 import IntegrityError
import pdb
from twitter.api import Twitter, TwitterHTTPError
from twitter.oauth import OAuth
from optparse import OptionParser
import config
usage= u'''usage: %prog [options] arg
This twitter bot is intended to be run from a task scheduler like cron.
A line in crontab like the following should work:
python /home/user/bin/python3ports/python3ports.py -u
The script has two mutually exclusive switches. The first time the script is run, use -i.
This creates the sqlite3 database and populates it with existing python3 ports.
Subsequently, -u for updating is to be used.
'''
# Obligatory configuration.
# db is the path to the sqlite3 db file
db = u''
# the following four strings are obtained from twitter
# for API usage
twitter_token=config.twitter_token
twitter_token_secret=config.twitter_token_secret
twitter_consumer_key=config.twitter_consumer_key
twitter_consumer_secret=config.twitter_consumer_secret
def fetch_p3packages():
python3_pkgs_url = "http://pypi.python.org/pypi?:action=browse&c=533&show=all"
conn = sqlite3.connect(db)
c = conn.cursor()
list_of_package_dics = []
p3_data = urllib2.urlopen(python3_pkgs_url).read()
p3_soup = BeautifulSoup.BeautifulSoup(p3_data)
p3_table = p3_soup.find("table", "list")
p3_rows = p3_table.findAll("tr")
for i in p3_rows[1:-1]:
cells = i.findAll("td")
package_dict = {}
package_dict['name'] = cells[0].a.renderContents()
package_dict['link'] = u'http://www.python.org' + cells[0].a['href']
list_of_package_dics.append(package_dict)
return list_of_package_dics
def update():
twit = Twitter(auth=OAuth(
token=twitter_token,
token_secret=twitter_token_secret,
consumer_key=twitter_consumer_key,
consumer_secret=twitter_consumer_secret))
conn = sqlite3.connect(db)
c = conn.cursor()
for i in fetch_p3packages():
try:
p_name = unicode(i['name'])
try:
c.execute("INSERT INTO packages(name) values (?)", (p_name,))
except UnicodeDecodeError:
print p_name
try:
twit.statuses.update(status="%s has been ported to #python3 %s" % (i['name'], i['link']))
except TwitterHTTPError:
# can't be arsed to handle properly right now, there's an F1 race on. Cross fingers.
pass
except IntegrityError:
pass
#print "skipping %s" % i['name']
conn.commit()
def initialise():
conn = sqlite3.connect(db)
c = conn.cursor()
c.execute('''CREATE TABLE packages (name text primary key)''')
for i in fetch_p3packages():
try:
c.execute("INSERT INTO packages(name) values (?)", (i['name'],))
except IntegrityError:
pass
#print "skipping %s", i['name']
conn.commit()
def main():
parser = OptionParser(usage)
parser.add_option("-i", "--initialise", action="store_true", dest="initialise")
parser.add_option("-u", "--update", action="store_true", dest="update")
(options, args) = parser.parse_args()
if options.initialise: initialise()
elif options.update: update()
else: parser.error("bad argument")
if __name__ == "__main__":
main()